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 write(str(ponds) + '\n') def bfs(h, w): g = deque([(h, w)]) Flag[h][w] = True apnd, popl = g.append, g.popleft while g: x, y = popl() 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: apnd((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()