from collections import deque import sys input = sys.stdin.readline def readlines(n): for h in range(n): for w, bit in enumerate(input().split()): if bit == "1": yield h,w def main(): H, W = map(int, input().split()) A = set(readlines(H)) dire = [(-1,0),(0,1),(1,0),(0,-1)] size = len(A) ans = 0 for h,w in A.copy(): if size < 0: break if (h,w) not in A: continue ans += 1 frontier = deque() frontier.append((h, w)) while frontier: y, x = frontier.popleft() for d, r in dire: suc_y = y+d suc_x = x+r if (suc_y, suc_x) not in A: continue frontier.append((suc_y, suc_x)) A.remove((suc_y,suc_x)) size -= 1 print(ans) main()