#include using namespace std; using ll = long long; constexpr ll INF = 1e15; struct Dinic { using T = ll; struct Edge { int to; T cap; int rev; //行き先、容量、逆辺 Edge(int to, T cap, int rev) : to(to), cap(cap), rev(rev) {} }; vector< vector > G; vector level, iter; private: void bfs(int s) { fill(level.begin(), level.end(), -1); queue que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (const Edge& e : G[v]) { if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } T dfs(int v, int t, T f) { if (v == t) return f; for (int& i = iter[v]; i < G[v].size(); ++i) { Edge& e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { T 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; } public: Dinic(int V) : G(V), level(V, -1), iter(V, 0) {} void add_edge(int from, int to, T cap) { G[from].emplace_back(to, cap, G[to].size()); G[to].emplace_back(from, 0, G[from].size() - 1); } T max_flow(int s, int t) { T flow = 0; while (true) { bfs(s); if (level[t] < 0) return flow; fill(iter.begin(), iter.end(), 0); T f; while ((f = dfs(s, t, INF)) > 0) { flow += f; } } } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int H, W; cin >> H >> W; Dinic d(H + W + 2); int src = H + W; int dst = src + 1; vector< vector > g(H, vector(W)); for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { cin >> g[i][j]; } } ll ans = 0; for (int i = 0; i < H; ++i) { ll r; cin >> r; ll sum = 0; for (int j = 0; j < W; ++j) { sum += g[i][j]; } if (sum <= r) { ans += r - sum; } else { d.add_edge(src, i, sum - r); } } for (int j = 0; j < W; ++j) { ll c; cin >> c; ans += c; d.add_edge(H + j, dst, c); } for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { d.add_edge(i, H + j, g[i][j]); } } ans -= d.max_flow(src, dst); cout << ans << "\n"; return 0; }