def add_edge(u, v): global match G[u].append(v) G[v].append(u) def dfs(v): global match, G, used used[v] = True for i in range(len(G[v])): u = G[v][i] w = match[u] if w < 0 or not used[w] and dfs(w): match[v] = u match[u] = v return True return False def bipartite_matching(): global match, V, used res = 0 match = [-1] * V for v in range(V): if match[v] < 0: used = [False] * V if dfs(v): res += 1 return res def matches(h, w, edges): global G, V, match V = h + w G = [[] for _ in range(V)] for v in range(h): for u in edges[v]: add_edge(v, u+h) res = bipartite_matching() return match[h:h+w] def mpc(xn, yn): global edges matched = matches(xn, yn, edges) left = xn right = 0 redges = matched for v in range(xn): if v not in matched: left -= 1 for u in edges[v]: right += 1 if redges[u] >= 0: left -= 1 return left + right if __name__ == '__main__': H, W = map(int, input().split()) S = dict() for i in range(H): for j, a in enumerate(list(map(int, input().split()))): if a > 0: if a not in S: S[a] = [[], []] if i in S[a][0]: ai = S[a][0].index(i) else: ai = len(S[a][0]) S[a][0].append(i) if j in S[a][1]: aj = S[a][1].index(j) else: aj = len(S[a][1]) S[a][1].append(j) S[a].append((ai, aj)) cnt = 0 for a, pairs in S.items(): h = len(pairs[0]) w = len(pairs[1]) edges = [set() for _ in range(h)] for i, j in pairs[2:]: edges[i].add(j) n = mpc(h, w) cnt += n print(cnt)