#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> #define LL long long int using namespace std; const int maxn=100005,maxm=10000005,INF=2000000000; inline int read(){ int out=0,flag=1;char c=getchar(); while(c<48||c>57) {if(c=='-') flag=-1;c=getchar();} while(c>=48&&c<=57) {out=out*10+c-48;c=getchar();} return out*flag; } int head[maxn],nedge=0; struct EDGE{ int to,f,next; }edge[maxm]; inline void build(int a,int b,int w){ edge[nedge]=(EDGE){b,w,head[a]}; head[a]=nedge++; edge[nedge]=(EDGE){a,0,head[b]}; head[b]=nedge++; } int color[105][105],N,M,S,T,X[4]={0,0,-1,1},Y[4]={1,-1,0,0}; bool vis[maxn]; int d[maxn],cur[maxn]; bool bfs(){ fill(vis,vis+maxn,false); queue<int> q; q.push(S); vis[S]=true; d[S]=0; int u,to; while(!q.empty()){ u=q.front(); q.pop(); for(int k=head[u];k!=-1;k=edge[k].next) if(!vis[to=edge[k].to]&&edge[k].f){ d[to]=d[u]+1; vis[to]=true; q.push(to); } } return vis[T]; } int dfs(int u,int minf){ if(u==T||!minf) return minf; int flow=0,f,to; if(cur[u]==-2) cur[u]=head[u]; for(int& k=cur[u];k!=-1;k=edge[k].next) if(d[to=edge[k].to]==d[u]+1&&(f=dfs(to,min(edge[k].f,minf)))){ edge[k].f-=f; edge[k^1].f+=f; flow+=f; minf-=f; if(!minf) break; } return flow; } int maxflow(){ int flow=0; while(bfs()){ fill(cur,cur+maxn,-2); flow+=dfs(S,INF); } return flow; } int main() { fill(head,head+maxn,-1); N=read(); M=read(); S=0; T=N*M+1; color[0][0]=1; int x; LL tot=0; for(int i=1;i<=N;i++) for(int j=1;j<=M;j++){ if((i%2&&j%2)||(i%2==0&&j%2==0)) color[i][j]=1; else color[i][j]=0; } for(int i=1;i<=N;i++) for(int j=1;j<=M;j++){ x=read(); tot+=x; if(color[i][j]) build(M*(i-1)+j,T,x); else{ build(S,M*(i-1)+j,x); for(int k=0;k<4;k++){ int nx=i+X[k],ny=j+Y[k]; if(nx>0&&ny>0&&nx<=N&&ny<=M) build(M*(i-1)+j,M*(nx-1)+ny,INF); } } } cout<<tot-maxflow()<<endl; return 0; }