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 = [len(s) > 0 for s in edges] 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() for _ in range(H)] S[a][i].add(j) cnt = 0 for edges in S.values(): n = mpc(H, W) cnt += n print(cnt)