#include using namespace std; typedef pair P; using ll = long long; const ll INF = 1e16; struct edge{ int to; ll cap; int rev; }; vector G[610]; bool used[610]; void add_edge(int from, int to, ll cap){ G[from].push_back({to, cap, (int)G[to].size()}); G[to].push_back({from, 0LL, (int)G[from].size() - 1}); } ll dfs(int v, int t, ll f){ if(v == t) return f; used[v] = true; for(int i = 0; i < G[v].size(); i++){ edge &e = G[v][i]; if(!used[e.to] && e.cap > 0){ ll d = dfs(e.to, t, min(f, e.cap)); if(d > 0){ e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } ll max_flow(int s, int t){ ll flow = 0; for(;;){ memset(used, 0, sizeof(used)); ll f = dfs(s, t, INF); if(f == 0) return flow; flow += f; } } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector > G(H, vector(W)); for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ cin >> G[i][j]; } } vector R(H), C(W), S(H); int ans = 0; for(int i = 0; i < H; i++){ cin >> R[i]; for(int j = 0; j < W; j++){ S[i] += G[i][j]; } ans += max(R[i] - S[i], 0LL); } for(int i = 0; i < W; i++){ cin >> C[i]; ans += C[i]; } for(int i = 0; i < H; i++){ add_edge(0, i + 1, max(S[i] - R[i], 0LL)); } for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ add_edge(i + 1, H + j + 1, G[i][j]); } } for(int i = 0; i < W; i++){ add_edge(H + i + 1, H + W + 1, C[i]); } cout << ans - max_flow(0, H + W + 1) << endl; }