import sys
input=sys.stdin.readline
sys.setrecursionlimit(5000000)
def dfs(x, y):
if 0 <= x < h and 0 <= y < w:
if ls[x][y] == 1:
ls[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
return False
else:
return False
while True:
cnt=0
w,h=map(int,input().split())
if w==0 and h==0:
break
ls=[]
for i in range(h):
ls.append(list(map(int,input().split())))
for i in range(h):
for j in range(w):
if ls[i][j]==1:
dfs(i,j)
cnt+=1
print(cnt)
'알고리즘 > 백준' 카테고리의 다른 글
백준-5063번(TGN)-python3 (1) | 2023.11.26 |
---|---|
백준-5014번(스타트링크)-python3 (6) | 2023.11.26 |
백준-4949번(균형잡힌 세상)-python3 (0) | 2023.11.26 |
백준-4948번(베르트랑 공준)-python3 (1) | 2023.11.26 |
백준-4673번(셀프 넘버)-python3 (0) | 2023.11.26 |