import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(2*10**5+10) write = lambda x: sys.stdout.write(x+"\n") debug = lambda x: sys.stderr.write(x+"\n") writef = lambda x: print("{:.12f}".format(x)) def sub(es): d = {} ns = {} for u,v in es: d.setdefault(u, 0) d.setdefault(v, 0) ns.setdefault(u, set()) ns.setdefault(v, set()) d[u] += 1 d[v] += 1 ns[u].add(v) ns[v].add(u) done = set() ans = 0 used = set() for u in ns.keys(): if u in done: continue q = [u] l = [] ll = set() rr = set() while q: u = q.pop() if d[u]==1: l.append(u) if u0 if v not in used: used.add(v) ans += 1 for u in ns[v]: d[u] -= 1 ns[u].discard(v) if d[u]==1: l.append(u) return ans h,w = list(map(int, input().split())) d = {} for i in range(h): l = list(map(int, input().split())) for j in range(w): v = l[j] if v==0: continue d.setdefault(v, []) d[v].append((i,j+h)) ans = 0 for k,es in d.items(): v = sub(es) ans += v # print(es,v) print(ans)