import re from collections import deque from sys import stdin, stdout input = lambda: stdin.readline().rstrip() write = stdout.write def main(): regex = re.compile(r'1+') ponds = 0 for i in range(H): r = 0 while 1: obj = regex.search(A[i], pos=r) if obj is None: break l, r = obj.start(), obj.end() if Flag[i][l] is None: bfs(i, l) ponds += 1 print(ponds) def bfs(h, w): g = deque([(h, w)]) Flag[h][w] = True while g: x, y = g.popleft() for i, j in {(1, 0), (-1, 0), (0, 1), (0, -1)}: xi, yj = x + i, y + j if 0 <= xi < H and 0 <= yj < W: if A[xi][yj] == '1' and Flag[xi][yj] is None: g.append((xi, yj)) Flag[xi][yj] = True return H, W = map(int, input().split()) A = stdin.read().replace(' ', '').splitlines() Flag = [[None] * W for _ in [0] * H] main()