#include using namespace std::literals::string_literals; using i64 = std::int_fast64_t; using std::cout; using std::cerr; using std::endl; using std::cin; template std::vector make_v(size_t a){return std::vector(a);} template auto make_v(size_t a,Ts... ts){ return std::vector(ts...))>(a,make_v(ts...)); } template struct Dinic{ struct edge{ int to; T cap; int rev; }; const T INF; std::vector> g; std::vector min_cost; std::vector itr; Dinic(int n) : INF(std::numeric_limits::max()){ g.resize(n); } void add_edge(int from, int to, T cap){ g[from].push_back(edge{to, cap, (int)g[to].size()}); g[to].push_back(edge{from, 0, (int)g[from].size() - 1}); } bool bfs(int s, int t){ min_cost.assign(g.size(), -1); std::queue qu; qu.push(s); min_cost[s] = 0; while(!qu.empty()){ auto p = qu.front(); qu.pop(); for(auto e:g[p]){ if(e.cap <= 0 or min_cost[e.to] != -1) continue; min_cost[e.to] = min_cost[p] + 1; qu.push(e.to); } } return min_cost[t] != -1; } T dfs(int idx, const int t, T flow){ if(idx == t) return flow; for(int &i = itr[idx]; i < g[idx].size(); i++){ edge &e = g[idx][i]; if(e.cap <= 0 or min_cost[idx] >= min_cost[e.to]) continue; T d = dfs(e.to, t, std::min(flow, e.cap)); if(d > 0){ e.cap -= d; g[e.to][e.rev].cap += d; return d; } } return 0; } T max_flow(int s, int t){ T flow = 0; while(bfs(s, t)){ itr.assign(g.size(), 0); T f = 0; while((f = dfs(s, t, INF)) > 0) flow += f; } return flow; } }; int main() { int h, w; scanf("%d%d", &h, &w); auto g = make_v(h, w); for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) scanf("%lld", &g[i][j]); std::vector a(h), b(w); for(int i = 0; i < h; i++) scanf("%d", &a[i]); for(int i = 0; i < w; i++) scanf("%d", &b[i]); Dinic dinic(2 * h * w + 10); const int S = 2 * h * w + 1, T = 2 * h * w + 2; for(int i = 0; i < h; i++) { dinic.add_edge(S, i, 0); dinic.add_edge(i, T, a[i]); } for(int i = 0; i < w; i++) { dinic.add_edge(S, i + h, 0); dinic.add_edge(i + h, T, b[i]); } for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { const int W = h + w + i * h + j; dinic.add_edge(S, W, g[i][j]); dinic.add_edge(W, i, g[i][j]); dinic.add_edge(W, j + h, g[i][j]); } } i64 ans = -dinic.max_flow(S, T); for(int i = 0; i < h; i++) ans += a[i]; for(int i = 0; i < w; i++) ans += b[i]; printf("%lld\n", ans); return 0; }