1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 100000
14 #define maxm 500000
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define rep(x,y) for(int i=1;i<=x;i++)for(int j=1;j<=y;j++)
19 #define FOR for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)
20 #define mod 1000000007
21 using namespace std;
22 inline int read()
23 {
24 int x=0,f=1;char ch=getchar();
25 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
26 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
27 return x*f;
28 }
29 int n,m,s,t,maxflow,cnt=1,tot,a[105][105],b[105][105],mark[105][105],head[maxn],cur[maxn],h[maxn],q[maxn];
30 struct edge{int go,next,v;}e[maxm];
31 void ins(int x,int y,int z){e[++cnt].go=y;e[cnt].v=z;e[cnt].next=head[x];head[x]=cnt;}
32 void insert(int x,int y,int z){ins(x,y,z);ins(y,x,0);}
33 void ins2(int x,int y,int z){insert(x,y,z);insert(y,x,z);}
34 bool bfs()
35 {
36 for(int i=s;i<=t;i++)h[i]=-1;
37 int l=0,r=1;q[1]=s;h[s]=0;
38 while(l<r)
39 {
40 int x=q[++l];
41 for(int i=head[x];i;i=e[i].next)
42 if(e[i].v&&h[e[i].go]==-1)
43 {
44 h[e[i].go]=h[x]+1;q[++r]=e[i].go;
45 }
46 }
47 return h[t]!=-1;
48 }
49 int dfs(int x,int f)
50 {
51 if(x==t) return f;
52 int tmp,used=0;
53 for(int i=head[x];i;i=e[i].next)
54 if(e[i].v&&h[e[i].go]==h[x]+1)
55 {
56 tmp=dfs(e[i].go,min(e[i].v,f-used));
57 e[i].v-=tmp;if(e[i].v)cur[x]=i;
58 e[i^1].v+=tmp;used+=tmp;
59 if(used==f)return f;
60 }
61 if(!used) h[x]=-1;
62 return used;
63 }
64 void dinic()
65 {
66 maxflow=0;
67 while(bfs())
68 {
69 for (int i=s;i<=t;i++)cur[i]=head[i];maxflow+=dfs(s,inf);
70 }
71 }
72 int main()
73 {
74 freopen("input.txt","r",stdin);
75 freopen("output.txt","w",stdout);
76 n=read();m=read();
77 FOR mark[i][j]=(i-1)*m+j;
78 s=0;t=mark[n][m]+1;
79 FOR {int x=read();tot+=x;if((i+j)&1)insert(s,mark[i][j],x);else insert(mark[i][j],t,x);}
80 FOR {int x=read();tot+=x;if((i+j)&1)insert(mark[i][j],t,x);else insert(s,mark[i][j],x);}
81 FOR
82 {
83 int x=read();
84 if(i+1<=n)ins2(mark[i][j],mark[i+1][j],x),tot+=x;
85 if(i-1>=1)ins2(mark[i][j],mark[i-1][j],x),tot+=x;
86 if(j+1<=m)ins2(mark[i][j],mark[i][j+1],x),tot+=x;
87 if(j-1>=1)ins2(mark[i][j],mark[i][j-1],x),tot+=x;
88 }
89 dinic();
90 printf("%d\n",tot-maxflow);
91 return 0;
92 }