from collections import deque H, W = map(int, input().split()) m = [] for i in range(H): m.append(list(map(int, input().split()))) xx = [1, 0, -1, 0] yy = [0, 1, 0, -1] q = deque() cnt = 0 seen = [[False]*W for _ in range(H)] for i in range(H): for j in range(W): if seen[i][j] == False and m[i][j] == 1: seen[i][j] = True q.append((i, j)) cnt += 1 while q: x, y = q.popleft() for px, py in zip(xx, yy): cx = x+px cy = y+py if 0<=cx