import sys
sys.setrecursionlimit(10**6)
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
def dfs(x,y):
if x>=w or x<=-1 or y>=h or y<=-1: # 이동 불가능하면 return False
return False
if board[y][x]==1: # 이동 가능한 지점
board[y][x]=0 # 방문 표시
for p in range(8):
new_x = x+dx[p] # 4방향에 대해
new_y = y+dy[p]
dfs(new_x, new_y) # 이동
return True # 이동 후 True
return False
while True:
w,h = map(int, input().split())
if w==0 and h==0:
break
else:
board = []
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
for i in range(h):
data = list(map(int, input().split()))
board.append(data)
count = 0
for c in range(h):
for p in range(w):
if dfs(p,c):
count+=1
print(count)
댓글남기기