import java.util.Scanner; import java.util.Set; import java.util.HashSet; import java.util.List; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; public class No1479 { private static class Values { public Set u, v; public List pos; public Values() { u = new HashSet(); v = new HashSet(); pos = new ArrayList(); } } private static List> edges; private static int[] matched; private static boolean dfs(int v, Set visited) { for (int u : edges.get(v)) { if (visited.contains(u)) { continue; } visited.add(u); if (matched[u] == -1 || dfs(matched[u], visited)) { matched[u] = v; return true; } } return false; } private static int mpc(int xn, int yn) { matched = new int[yn]; Arrays.fill(matched, -1); int left = xn, right = 0; List ledges = new ArrayList(); for (int v = 0; v < xn ; v++) { if (!dfs(v, new HashSet())) { left--; ledges.add(v); } } for (int v : ledges) { for (int u : edges.get(v)) { right++; if (matched[u] >= 0) { left--; } } } return left + right; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int H = scan.nextInt(); int W = scan.nextInt(); HashMap S = new HashMap(); for (int i=0; i < H; i++) { for (int j = 0; j < W; j++) { int a = scan.nextInt(); if (a == 0) { continue; } if (S.containsKey(a)) { Values v = S.get(a); v.u.add(i); v.v.add(j); v.pos.add(new Integer[] {i, j}); } else { Values v = new Values(); v.u.add(i); v.v.add(j); v.pos.add(new Integer[] {i, j}); S.put(a, v); } } } scan.close(); int cnt = 0; for (Values v : S.values()) { List hl = new ArrayList(v.u); List wl = new ArrayList(v.v); int h = hl.size(); int w = wl.size(); edges = new ArrayList>(); for (int i = 0; i < h; i++) { edges.add(new HashSet()); } for (Integer[] i : v.pos) { edges.get(hl.indexOf(i[0])).add(wl.indexOf(i[1])); } cnt += mpc(h, w); } System.out.println(cnt); } }