H,W = map(int,input().split()) A = [list(map(int,input().split())) for _ in range(H)] count = 0 stack = [] for h in range(H): for w in range(W): if A[h][w] == 1: count += 1 stack.append((h,w)) A[h][w] = 0 while stack: x,y = stack.pop() for i,j in [(0,1),(0,-1),(1,0),(-1,0)]: if 0 <= x + i < H and 0 <= y + j < W: if A[x+i][y+j]: A[x+i][y+j] = 0 stack.append((x+i,y+j)) print(count)