#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<iostream> #define maxn 10010 #define maxm 100010 #define inf 1000000000 using namespace std; int dx[4]={0,1,0,-1}; int dy[4]={1,0,-1,0}; int head[maxn],to[maxm],c[maxm],next[maxm],q[maxn],d[maxn]; int a[110][110]; int n,m,num,ans,s,t,sum; int calc(int x,int y) { return (x-1)*m+y; } void addedge(int x,int y,int z) { num++;to[num]=y;c[num]=z;next[num]=head[x];head[x]=num; num++;to[num]=x;c[num]=0;next[num]=head[y];head[y]=num; } void build() { s=0;t=n*m+1; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if ((i+j)&1) { addedge(s,calc(i,j),a[i][j]); for (int k=0;k<4;k++) { int xx=i+dx[k],yy=j+dy[k]; if (xx<1 || xx>n || yy<1 || yy>m) continue; addedge(calc(i,j),calc(xx,yy),inf); } } else addedge(calc(i,j),t,a[i][j]); } bool bfs() { memset(d,-1,sizeof(d)); int l=0,r=1; q[1]=s;d[s]=0; while (l<r) { int x=q[++l]; for (int p=head[x];p;p=next[p]) if (c[p] && d[to[p]]==-1) { d[to[p]]=d[x]+1; q[++r]=to[p]; } } if (d[t]==-1) return 0; else return 1; } int find(int x,int low) { if (x==t || low==0) return low; int totflow=0; for (int p=head[x];p;p=next[p]) if (c[p] && d[to[p]]==d[x]+1) { int a=find(to[p],min(c[p],low)); c[p]-=a;c[p^1]+=a; totflow+=a;low-=a; if (low==0) return totflow; } if (low) d[x]=-1; return totflow; } void Dinic() { while (bfs()) ans+=find(s,inf); } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { scanf("%d",&a[i][j]); sum+=a[i][j]; } num=1; build(); Dinic(); printf("%d\n",sum-ans); return 0; }