h, w = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(h)] used = [[False] * w for _ in range(h)] directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] ans = 0 for i in range(h): for j in range(w): if used[i][j] or A[i][j] == 0: continue ans += 1 stack = [(i, j)] while stack: ii, jj = stack.pop() for di, dj in directions: ni = ii + di nj = jj + dj if ni == -1 or ni == h or nj == -1 or nj == w: continue if A[ni][nj] == 0 or used[ni][nj]: continue used[ni][nj] = True stack.append((ni, nj)) print(ans)