실버2 문제
import sys
sys.setrecursionlimit(10000)
while True:
w, h = map(int, input().split())
if (w == 0 and h == 0):
break
arr = []
cnt = 0
for _ in range(h):
arr.append(list(map(int, input().split())))
def dfs(s, g):
if (s < 0 or s >= h or g < 0 or g >= w):
return False
if arr[s][g] == 1:
arr[s][g] = 0
dfs(s-1, g-1)
dfs(s-1, g)
dfs(s-1, g+1)
dfs(s, g-1)
dfs(s, g+1)
dfs(s+1, g-1)
dfs(s+1, g)
dfs(s+1, g+1)
return True
return False
for i in range(h):
for j in range(w):
if (dfs(i, j) == True):
cnt += 1
print(cnt)