#include using namespace std; template struct Dinic { struct Edge { int to, rev; T cap; }; const T inf = numeric_limits::max(); const int n; vector< vector > g; vector dist, i; Dinic(int _n) : n(_n), g(n), dist(n), i(n) {} void add_edge(int from, int to, T cap) { assert(from != to); assert(cap >= 0); if (!cap) return; g[from].emplace_back(Edge{to, (int)g[to].size(), cap}); g[to].emplace_back(Edge{from, (int)g[from].size() - 1, 0}); } void bfs(int s) { fill(begin(dist), end(dist), -1); queue que; dist[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (const auto& e : g[v]) { if (dist[e.to] != -1 or !e.cap) continue; dist[e.to] = dist[v] + 1; que.push(e.to); } } } T dfs(int v, int s, T f) { if (v == s) return f; for (; i[v] < (int)g[v].size(); ++i[v]) { Edge& e = g[v][i[v]]; if (dist[e.to] >= dist[v] or !g[e.to][e.rev].cap) continue; T d = dfs(e.to, s, min(f, g[e.to][e.rev].cap)); if (d > 0) { g[e.to][e.rev].cap -= d; e.cap += d; return d; } } return 0; } T max_flow(int s, int t) { assert(s != t); T res = 0; while (true) { bfs(s); if (dist[t] == -1) return res; fill(begin(i), end(i), 0); while (true) { T f = dfs(t, s, inf); if (!f) break; res += f; } } } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w; cin >> h >> w; int s = h + w, t = s + 1; Dinic g(t + 1); long long res = 0; vector sc(w); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { int c; cin >> c; g.add_edge(i, h + j, c); sc[j] += c; } } for (int i = 0; i < h; ++i) { int a; cin >> a; res += a; g.add_edge(s, i, a); } for (int j = 0; j < w; ++j) { int b; cin >> b; res += b; g.add_edge(s, h + j, b); g.add_edge(h + j, t, sc[j]); } res -= g.max_flow(s, t); cout << res << '\n'; }