#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=3e4+5,M=2e5+5,INF=1e9;
inline int read(){
char c=getchar();int x=0,f=1;
while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();}
while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();}
return x*f;
}
int n,m,num,s,t,a,b,c;
struct edge{
int v,c,f,ne;
}e[M<<1];
int cnt,h[N];
inline void ins(int u,int v,int c){//printf("ins %d %d %d\n",u,v,c);
cnt++;
e[cnt].v=v;e[cnt].c=c;e[cnt].f=0;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].c=0;e[cnt].f=0;e[cnt].ne=h[v];h[v]=cnt;
}
int q[N],head,tail,vis[N],d[N];
bool bfs(){
memset(vis,0,sizeof(vis));
memset(d,0,sizeof(d));
head=tail=1;
d[s]=0;vis[s]=1;
q[tail++]=s;
while(head!=tail){
int u=q[head++];
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(!vis[v]&&e[i].c>e[i].f){
vis[v]=1;
d[v]=d[u]+1;
q[tail++]=v;
if(v==t) return true;
}
}
}
return false;
}
int cur[N];
int dfs(int u,int a){
if(u==t||a==0) return a;
int flow=0,f;
for(int &i=cur[u];i;i=e[i].ne){
int v=e[i].v;
if(d[v]==d[u]+1&&(f=dfs(v,min(a,e[i].c-e[i].f)))>0){
flow+=f;
e[i].f+=f;
e[((i-1)^1)+1].f-=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int dinic(){
int flow=0;
while(bfs()){
for(int i=s;i<=t;i++) cur[i]=h[i];
flow+=dfs(s,INF);
}
return flow;
}
inline int id(int i,int j){return (i-1)*m+j;}
int ans;
int main(){
//freopen("in.txt","r",stdin);
n=read();m=read();
num=n*m;
s=0;t=num*3+1;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a=read(),ins(s,id(i,j),a),ans+=a;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) b=read(),ins(id(i,j),t,b),ans+=b;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){
c=read();int k=id(i,j)+num;ans+=c;
ins(s,k,c);
ins(k,id(i,j),INF);
if(i-1>=1) ins(k,id(i-1,j),INF);
if(j+1<=m) ins(k,id(i,j+1),INF);
if(i+1<=n) ins(k,id(i+1,j),INF);
if(j-1>=1) ins(k,id(i,j-1),INF);
}
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){
c=read();int k=id(i,j)+num+num;ans+=c;
ins(k,t,c);
ins(id(i,j),k,INF);
if(i-1>=1) ins(id(i-1,j),k,INF);
if(j+1<=m) ins(id(i,j+1),k,INF);
if(i+1<=n) ins(id(i+1,j),k,INF);
if(j-1>=1) ins(id(i,j-1),k,INF);
}
printf("%d",ans-dinic());
}