const int maxn=15,maxm=15; int n,m; int A[maxn][maxm],B[maxn][maxm]; int ocr[maxn][maxm];
int dfs(int step) { if(step==n*m+1) return 0; int re=0; if(step&1)re=-0x3f3f3f3f; else re=0x3f3f3f3f; for(register int i=1;i<=n;++i) for(register int j=1;j<=m;++j) if(!ocr[i][j] && ocr[i-1][j] && ocr[i][j-1]) { ocr[i][j]=1; if(step&1)re=max(re,dfs(step+1)+A[i][j]); else re=min(re,dfs(step+1)-B[i][j]); ocr[i][j]=0; } return re; }
int main() { read(n);read(m); for(register int i=1;i<=n;++i) for(register int j=1;j<=m;++j) read(A[i][j]); for(register int i=1;i<=n;++i) for(register int j=1;j<=m;++j) read(B[i][j]); for(register int i=0;i<=n;++i)ocr[i][0]=1; for(register int i=0;i<=m;++i)ocr[0][i]=1; printf("%d",dfs(1)); return 0; }
int n,m; int A[maxn][maxm],B[maxn][maxm]; int ocr[maxn][maxm]; map<ull,int> rec;
ull Hash() { ull re=0; for(register int i=1;i<=n;++i) for(register int j=1;j<=m;++j) re=re*3ull+ocr[i][j]; return re; }
int dfs(int step) { if(step==n*m+1) return 0; int re=0; ull h=Hash(); if(rec.find(h)!=rec.end()) return rec[h]; if(step&1)re=-0x3f3f3f3f; else re=0x3f3f3f3f; for(register int i=1;i<=n;++i) for(register int j=1;j<=m;++j) if(!ocr[i][j] && ocr[i-1][j] && ocr[i][j-1]) { ocr[i][j]=1; if(step&1)re=max(re,dfs(step+1)+A[i][j]); else re=min(re,dfs(step+1)-B[i][j]); ocr[i][j]=0; } return rec[h]=re; }
int main() { read(n);read(m); for(register int i=1;i<=n;++i) for(register int j=1;j<=m;++j) read(A[i][j]); for(register int i=1;i<=n;++i) for(register int j=1;j<=m;++j) read(B[i][j]); for(register int i=0;i<=n;++i)ocr[i][0]=1; for(register int i=0;i<=m;++i)ocr[0][i]=1; printf("%d",dfs(1)); return 0; }
#include <cstdio> #include <iostream> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull;
int main() { read(n);read(m); for(register int i=1;i<=n;++i) for(register int j=1;j<=m;++j) read(A[i][j]); for(register int i=1;i<=n;++i) for(register int j=1;j<=m;++j) read(B[i][j]); for(register int i=0;i<=n;++i)ocr[i][0]=1; for(register int i=0;i<=m;++i)ocr[0][i]=1; printf("%d",dfs(1,-inf,inf,0,0)); return 0; }