h,w = input().split() h,w = int(h),int(w) lst = [] cnt = 0 def tansaku(x,y): if lst[x][y]==0: return lst[x][y]=0 tansaku(x+1,y) tansaku(x-1,y) tansaku(x,y+1) tansaku(x,y-1) for a in range(h): l = [0] for b in input().split(): l.append(int(b)) l.append(0) lst.append(l) lst.insert(0,[0]*(w+2)) lst.append([0]*(w+2)) for a in range(1,h+1): for b in range(1,w+1): if lst[a][b]==1: cnt+=1 tansaku(a,b) print(cnt)