RT,不知道为啥挂了......样例都不对......
//#include<bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<stack>
#include<vector>
//#include<conio.h>
#include<string>
#include<map>
#include<cstdlib>
#include<queue>
#include<math.h>
#include<time.h>
#include<set>
#include<cstdio>
#include<stdio.h>
#include<algorithm>
using namespace std;
using std::cin;
using std::cout;
using std::endl;
namespace IN{
const int MAX_INPUT = 1000000;
#define getc() (p1==p2&&(p2=(p1=buf)+inbuf->sgetn(buf,MAX_INPUT),p1==p2)?EOF:*p1++)
char buf[MAX_INPUT],*p1,*p2;
template<typename T>inline bool read(T &x) {
static std::streambuf *inbuf=cin.rdbuf();
x=0;
register int f=0,flag=false;
register char ch=getc();
while(!isdigit(ch)){
if (ch=='-') f=1;
ch=getc();
}
if(isdigit(ch)) x=x*10+ch-'0',ch=getc(),flag=true;
while(isdigit(ch)) {
x=x*10+ch-48;
ch=getc();
}
x=f?-x:x;
return flag;
}
template<typename T,typename ...Args>inline bool read(T& a,Args& ...args) {
return read(a)&&read(args...);
}
#undef getc
}
namespace OUT{
template<typename T>inline void put(T x){
static std::streambuf *outbuf=cout.rdbuf();
static char stack[21];
static int top=0;
if(x<0){
outbuf->sputc('-');
x=-x;
}
if(!x){
outbuf->sputc('0');
outbuf->sputc('\n');
return;
}
while(x){
stack[++top]=x%10+'0';
x/=10;
}
while(top){
outbuf->sputc(stack[top]);
--top;
}
outbuf->sputc('\n');
}
inline void putc(const char ch){
static std::streambuf *outbuf=cout.rdbuf();
outbuf->sputc(ch);
}
inline void putstr(string s){
for(register int i=0;i<s.length();i++) putc(s[i]);
}
template<typename T>inline void put(const char ch,T x){
static std::streambuf *outbuf=cout.rdbuf();
static char stack[21];
static int top = 0;
if(x<0){
outbuf->sputc('-');
x=-x;
}
if(!x){
outbuf->sputc('0');
outbuf->sputc(ch);
return;
}
while(x){
stack[++top]=x%10+'0';
x/=10;
}
while(top){
outbuf->sputc(stack[top]);
--top;
}
outbuf->sputc(ch);
}
template<typename T,typename ...Args> inline void put(T a,Args ...args){
put(a);put(args...);
}
template<typename T,typename ...Args> inline void put(const char ch,T a,Args ...args){
put(ch,a);put(ch,args...);
}
}
using namespace IN;
using namespace OUT;
#define N 1005*1005
#define int long long
#define inf (1ll<<60ll)
int n,m,s=1,t,head[N],cnt=1,d[N],now[N],ans;
queue<int> q;
struct edge{
int v,nxt,val;
}e[N<<2];
inline int hash(int aaa,int bbb){
return (aaa-1)*m+bbb;
}
inline void add2(int u,int v,int val){
e[++cnt]=(edge){v,head[u],val},head[u]=cnt;
}
inline void add(int u,int v,int val){
add2(u,v,val),add2(v,u,0);
}
inline bool bfs(){
while(!q.empty()) q.pop();
memset(d,0,sizeof(d));
d[s]=1,q.push(s),now[s]=head[s];
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=head[x];i;i=e[i].nxt){
int v=e[i].v;
if(d[v]||!e[i].val) continue;
q.push(v),now[v]=head[v],d[v]=d[x]+1;
}
}
return d[t];
}
int dfs(int x,int sum){
if(x==t) return sum;
int re=0;
for(int i=now[x];i&∑i=e[i].nxt){
now[x]=i;
int v=e[i].v;
if(e[i].val&&(d[v]==d[x]+1)){
int k=dfs(v,min(sum,e[i].val));
if(!k) d[v]=inf;
re+=k,sum-=k,e[i].val-=k,e[i^1].val+=k;
}
}
return re;
}
signed main(signed argc, char const *argv[]){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
read(n,m),t=hash(n,m);
for(int i=1;i<=n;i++)
for(int j=1,x;j<m;j++)
read(x),add(hash(i,j),hash(i,j+1),x);
for(int i=1;i<n;i++)
for(int j=1,x;j<=m;j++)
read(x),add(hash(i,j),hash(i+1,j),x);
for(int i=1;i<n;i++)
for(int j=1,x;j<m;j++)
read(x),add(hash(i,j),hash(i+1,j+1),x);
while(bfs()) ans+=dfs(s,inf);
put(ans);
return 0;
}