def dfs(v, visited): global edges, matched for u in edges[v]: if u in visited: continue visited.add(u) if matched[u] == -1 or dfs(matched[u], visited): matched[u] = v return True return False def mpc(xn, yn): global matched, edges matched = [-1] * yn left = [True] * xn right = [False] * yn ledges = [] redges = matched for v in range(xn): if not dfs(v, set()): left[v] = False ledges.append(v) for v in ledges: for u in edges[v]: right[u] = True if redges[u] >= 0: left[redges[u]] = False return sum(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] = [set([i]), set([j])] S[a].append((i, j)) S[a][0].add(i) S[a][1].add(j) cnt = 0 for pairs in S.values(): hl = list(pairs[0]) wl = list(pairs[1]) h = len(hl) w = len(wl) edges = [set() for _ in range(h)] for i, j in pairs[2:]: edges[hl.index(i)].add(wl.index(j)) n = mpc(h, w) cnt += n print(cnt)