#include using namespace std; #include using namespace atcoder; typedef long long ll; const long long INF = 1LL<<60; const double PI = acos(-1.0); /*const double PI = atan2(0.0,-1.0)*/ template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } #define rep(i, n) for(ll i = 0; i < (ll)(n); i++) #define rep1(i,aa,n) for(ll i = aa; i <= (ll)(n); i++) #define ALL(a) (a).begin(),(a).end() #define ar_init(aa,val) rep(i,aa.size()) aa[i]=val; #define v2d(aa,val1,val2,ini) vector> aa(val1,vector(val2,ini)); #define pqe priority_queue #define pqeg priority_queue,greater> #define lcin(...) ll __VA_ARGS__;CINT(__VA_ARGS__) #define eout(...) COU(__VA_ARGS__); #define sout(...) SCOU(__VA_ARGS__); #define scin(...) string __VA_ARGS__;CINT(__VA_ARGS__) #define lb(aa,val) lower_bound(ALL(aa),val) #define pb push_back #define mkp make_pair ll kaijou(ll e){ll r=1;rep(i,e){r*=(i+1);}return r;} void CINT(){} template void CINT(Head&& head,Tail&&... tail){ cin>>head; CINT(move(tail)...); } void COU(){} template void COU(Head&& head,Tail&&... tail){ cout< void SCOU(Head&& head,Tail&&... tail){ cout< T IN(){T x;cin>>x;return (x);} using pii = pair; using v2 = vector>; using v1 = vector; using v2p = vector>; using v1p = vector; const ll mo=1000000007; //const ll mo=998244353; v2 mat_pow(v2 en,ll n){ ll si=en.size(); //n==0のときreturn単位行列 if(n==0){ rep(i,en.size()) rep(k,en.size()){ if(i==k){ en[i][k]=1; }else{ en[i][k]=0; } } return en; } auto dfs = [&](auto self,ll ko) -> v2 { if(ko==1){ return en; } auto e=self(self,ko/2); v2d(res,si,si,0); rep(i,res.size()){ rep(k,res.size()){ rep(m,res.size()){ res[i][k]+=e[i][m]*e[m][k]%mo; res[i][k]%=mo; } } } v2d(rr,si,si,0); if(ko%2){ rep(i,res.size()){ rep(k,res.size()){ rep(m,res.size()){ rr[i][k]+=res[i][m]*en[m][k]%mo; rr[i][k]%=mo; } } } return rr; } return res; }; return dfs(dfs,n); } ll mod_pow(ll N,ll n){ if(n==0) return 1; ll hh; hh=n%2; int tt=mod_pow(N,n/2); if(hh==0)return tt%mo*tt%mo; else {return tt%mo*tt%mo*N%mo;} } int main(){ //cout< graph(900+h+w+10); v2d(gg,h,w,0); v1 rr(h); v1 cc(w); ll s=900+h+w+5; ll t=900+h+w+4; rep(i,h)rep(k,w){ cin>>gg[i][k]; } rep(i,h)cin>>rr[i]; rep(i,w)cin>>cc[i]; rep(i,h){ rep(k,w){ ll a=gg[i][k]; graph.add_edge(s,300*i+k,a); graph.add_edge(300*i+k,t,0); graph.add_edge(300*i+k,900+i,INF); graph.add_edge(300*i+k,900+h+k,INF); } } ll sum=0; rep(i,h){ ll a=rr[i]; sum+=a; graph.add_edge(s,900+i,0); graph.add_edge(900+i,t,a); } rep(k,w){ ll a=cc[k]; sum+=a; graph.add_edge(s,900+h+k,0); graph.add_edge(900+h+k,t,a); } eout(sum-graph.flow(s,t)); }