August 20, 2021
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)
move = [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]
def dfs(x, y):
visit[x][y] = True
for dx, dy in move:
nx = x + dx
ny = y + dy
if 0 <= nx < h and 0 <= ny < w and not visit[nx][ny] and _map[nx][ny] == 1:
dfs(nx, ny)
while True:
w, h = map(int, input().split())
if w == 0 and h == 0:
break
_map = [list(map(int, input().split())) for _ in range(h)]
visit = [[False] * w for _ in range(h)]
count = 0
for i in range(h):
for j in range(w):
if _map[i][j] == 1 and not visit[i][j]:
count += 1
dfs(i, j)
print(count)