from collections import deque import sys input = sys.stdin.readline def main(): H, W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] dire = [(-1,0),(0,1),(1,0),(0,-1)] explored = set() ans = 0 for h in range(H): for w in range(W): if A[h][w] == 0 or (h, w) in explored: continue ans += 1 frontier = deque() frontier.append((h, w)) explored.add((h, w)) while frontier: y, x = frontier.popleft() for d, r in dire: suc_y = y+d suc_x = x+r if suc_y >= H or suc_x >= W or \ suc_y < 0 or suc_x < 0 or \ (suc_y, suc_x) in explored or \ A[suc_y][suc_x] == 0: continue frontier.append((suc_y, suc_x)) explored.add((suc_y, suc_x)) print(ans) main()