from collections import deque H, W = map(int,input().split()) L = [list(map(int,input().split())) for _ in range(H)] def bfs(i, j): L[i][j] = 0 Q = deque() Q.append((i, j)) while Q: i, j = Q.popleft() dy = [0, 1, 0, -1] dx = [1, 0, -1, 0] for k in range(4): y = i + dy[k] x = j + dx[k] if not(0 <= y < H and 0 <= x < W): continue if L[y][x] == 1: L[y][x] = 0 Q.append((y, x)) ans = 0 for i in range(H): for j in range(W): if L[i][j] == 1: ans += 1 bfs(i, j) print(ans)