$i\in S, j\in S$, 则割掉的边是$b$$d$, 代价是两人不可同理(science)(注意单人文理的代价已经在刚才算过了，所以不要再算！)可得$b+d=BB_{i,j}$, 同理(reason)可得$a+c=AA_{i,j}$.

$i\in S, j\in T$, 则割掉的边是$b,c,e$ (一定注意没有$f$), 代价是二人不可同文或同理(science), 可得$b+c+e=AA_{i,j}+BB_{i,j}$, 同理(reason)可得$a+d+f=AA_{i,j}+BB_{i,j}$.

#### 代码

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int INF = 4e8;

namespace MaxFlow
{
const int N = 1e4+2;
const int M = 6e4;
struct Edge
{
int v,w,nxt,rev;
} e[(M<<1)+3];
int fe[N+3],te[N+3];
int que[N+3];
int dep[N+3];
int n,en,s,t;
{
en++; e[en].v = v; e[en].w = w;
e[en].nxt = fe[u]; fe[u] = en; e[en].rev = en+1;
en++; e[en].v = u; e[en].w = 0;
e[en].nxt = fe[v]; fe[v] = en; e[en].rev = en-1;
}
bool bfs()
{
for(int i=1; i<=n; i++) dep[i] = 0;
int head = 1,tail = 1; que[tail] = s; dep[s] = 1;
{
for(int i=fe[u]; i; i=e[i].nxt)
{
if(dep[e[i].v]==0 && e[i].w>0)
{
dep[e[i].v] = dep[u]+1;
tail++; que[tail] = e[i].v;
}
}
}
return dep[t]!=0;
}
int dfs(int u,int cur)
{
if(u==t) return cur;
int rst = cur;
for(int i=te[u]; i; i=e[i].nxt)
{
if(dep[e[i].v]==dep[u]+1 && rst>0 && e[i].w>0)
{
int flow = dfs(e[i].v,min(rst,e[i].w));
if(flow>0)
{
e[i].w -= flow; e[e[i].rev].w += flow; rst -= flow;
if(e[i].w>0) te[u] = i;
if(rst==0) return cur;
}
}
}
if(rst==cur) dep[u] = 0;
return cur-rst;
}
int dinic(int _n,int _s,int _t)
{
n = _n,s = _s,t = _t;
int ret = 0;
while(bfs())
{
for(int i=1; i<=n; i++) te[i] = fe[i];
ret += dfs(s,INF);
}
return ret;
}
}
using MaxFlow::dinic;

const int N = 100;
int a[N+3][N+3];
int b[N+3][N+3];
int aa1[N+3][N+3];
int aa2[N+3][N+3];
int bb1[N+3][N+3];
int bb2[N+3][N+3];
int n,m;

int getid(int x,int y) {return (x-1)*m+y+2;}

int main()
{
scanf("%d%d",&n,&m); int ans = 0;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++) scanf("%d",&a[i][j]),ans += a[i][j];
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++) scanf("%d",&b[i][j]),ans += b[i][j];
}
for(int i=1; i<n; i++)
{
for(int j=1; j<=m; j++) scanf("%d",&aa1[i][j]),ans += aa1[i][j];
}
for(int i=1; i<n; i++)
{
for(int j=1; j<=m; j++) scanf("%d",&bb1[i][j]),ans += bb1[i][j];
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<m; j++) scanf("%d",&aa2[i][j]),ans += aa2[i][j];
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<m; j++) scanf("%d",&bb2[i][j]),ans += bb2[i][j];
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
int x = getid(i,j);
if(i<n)
{
int y = getid(i+1,j);
}
if(j<m)
{
int y = getid(i,j+1);