#include using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) #define range(a) a.begin(), a.end() struct Dinic { struct edge { int v; long long c; }; vector es; vector> g; vector dist; vector index; Dinic(int n) : g(n), dist(n), index(n) {} void add(int u, int v, long long c) { g[u].push_back(es.size()); g[v].push_back(es.size() + 1); es.push_back(edge{v, c}); es.push_back(edge{u, 0}); } long long calc(int s, int t) { long long ans = 0; while (bfs(s, t)) { for (;;) { long long d = dfs(s, t, 1e18); if (d == 0) break; ans += d; } } return ans; } bool bfs(int s, int t) { fill(dist.begin(), dist.end(), -1); fill(index.begin(), index.end(), 0); queue q; q.push(t); dist[t] = 0; while (!q.empty()) { int u = q.front(); q.pop(); for (int i : g[u]) { int v = es[i].v; if (es[i ^ 1].c > 0 && dist[v] == -1) { dist[v] = dist[u] + 1; q.push(v); } } } return dist[s] != -1; } long long dfs(int u, int t, long long f) { if (u == t) return f; for (int &it = index[u]; it < g[u].size(); it++) { int i = g[u][it]; int v = es[i].v; if (es[i].c > 0 && dist[v] < dist[u]) { long long d = dfs(v, t, min(f, es[i].c)); if (d > 0) { es[i ^ 0].c -= d; es[i ^ 1].c += d; return d; } } } return 0; } }; int main() { int H, W; cin >> H >> W; vector> G(H, vector(W)); rep(i, H) rep(j, W) cin >> G[i][j]; vector R(H), C(W); rep(i, H) cin >> R[i]; rep(j, W) cin >> C[j]; vector row(H), col(W); rep(i, H) rep(j, W) row[i] += G[i][j]; rep(j, W) rep(i, H) col[j] += G[i][j]; Dinic mf(H + W + 2); ll total = accumulate(range(R), 0LL) + accumulate(range(C), 0LL); int s = H + W; int t = s + 1; rep(i, H) mf.add(i, t, row[i]); rep(j, W) mf.add(j + H, t, col[j]); rep(i, H) mf.add(s, i, 2*R[i]); rep(j, W) mf.add(s, j + H, 2*C[j]); rep(i, H) rep(j, W) { mf.add(i, j + H, G[i][j]); mf.add(j + H, i, G[i][j]); } cout << total - mf.calc(s, t)/2 << endl; }