import sys readline=sys.stdin.readline from collections import defaultdict class UnionFind: def __init__(self,n): self.n=n self.parents=[-1]*n def Find(self,x): stack=[] while self.parents[x]>=0: stack.append(x) x=self.parents[x] for y in stack: self.parents[y]=x return x def Union(self,x,y): x=self.Find(x) y=self.Find(y) if x==y: return if self.parents[x]>self.parents[y]: x,y=y,x self.parents[x]+=self.parents[y] self.parents[y]=x def Size(self,x): return -self.parents[self.Find(x)] def Same(self,x,y): return self.Find(x)==self.Find(y) def Members(self,x): root = self.Find(x) return [i for i in range(self.n) if self.Find(i)==root] def Roots(self): return [i for i, x in enumerate(self.parents) if x<0] def Group_Count(self): return len(self.Roots()) def All_Group_Members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.Find(member)].append(member) return group_members H,W=map(int,readline().split()) A=list(map(int,readline().split())) ans=0 UF=UnionFind(H*W) for w in range(W-1): if A[w] and A[w+1]: UF.Union(w,w+1) ans-=A.count(0) for h in range(1,H): prev=A A=list(map(int,readline().split())) for w in range(W-1): if A[w] and A[w+1]: UF.Union(h*W+w,h*W+w+1) for w in range(W): if prev[w] and A[w]: UF.Union((h-1)*W+w,h*W+w) ans-=A.count(0) ans+=UF.Group_Count() print(ans)