기존에 풀던 dfs만 잘 활용하면 되서 간단한 문제이지만, python3로 푸니 런나임에러, pypy3에서만 정답처리 되었다. 아마 하나하나 dfs로 설정해주어서 그런것 같은데 좀 더 클린코딩하는 방법도 추후에 추가해야겠다.
while True:
w, h = map(int, input().split())
if w == 0 and h == 0:
break
graph = []
for i in range(h):
graph.append(list(map(int, input().split())))
def dfs(x, y):
if x < 0 or y < 0 or x >= h or y >= w:
return
if graph[x][y] == 1:
graph[x][y] = 2
dfs(x-1, y-1)
dfs(x-1, y)
dfs(x-1, y+1)
dfs(x, y-1)
dfs(x, y+1)
dfs(x+1, y-1)
dfs(x+1, y)
dfs(x+1, y+1)
return True
ans = 0
for i in range(h):
for j in range(w):
if dfs(i, j) == True:
ans+=1
print(ans)