import sys readline = sys.stdin.readline INF = float('inf') class Dinic: def __init__(self, n): self.n = n self.links = [[] for _ in range(n)] self.depth = None self.progress = None def add_edge(self, _from, to, cap): self.links[_from].append([cap, to, len(self.links[to])]) self.links[to].append([0, _from, len(self.links[_from]) - 1]) def bfs(self, s): from collections import deque depth = [-1] * self.n depth[s] = 0 q = deque([s]) while q: v = q.popleft() for cap, to, rev in self.links[v]: if cap > 0 and depth[to] < 0: depth[to] = depth[v] + 1 q.append(to) self.depth = depth def dfs(self, v, t, flow): if v == t: return flow links_v = self.links[v] for i in range(self.progress[v], len(links_v)): self.progress[v] = i cap, to, rev = link = links_v[i] if cap == 0 or self.depth[v] >= self.depth[to]: continue d = self.dfs(to, t, min(flow, cap)) if d == 0: continue link[0] -= d self.links[to][rev][0] += d return d return 0 def max_flow(self, s, t): flow = 0 while True: self.bfs(s) if self.depth[t] < 0: return flow self.progress = [0] * self.n current_flow = self.dfs(s, t, float('inf')) while current_flow > 0: flow += current_flow current_flow = self.dfs(s, t, float('inf')) def main(): H, W = map(int, readline().split()) G = tuple(tuple(map(int, readline().split())) for _ in range(H)) R = tuple(map(int, readline().split())) C = tuple(map(int, readline().split())) V = H * W + H + W + 2 dinic = Dinic(V) s, t = V - 2, V - 1 add = dinic.add_edge for h in range(H): for w in range(W): v = W * h + w add(s, v, G[h][w]) add(v, h + H * W, INF) add(v, w + H * W + W, INF) for h in range(H): add(h + H * W, t, R[h]) for w in range(W): add(w + H * W + W, t, C[w]) flow = dinic.max_flow(s, t) print(sum(R) + sum(C) - flow) if __name__ == "__main__": main()