importsysinput=sys.stdin.readlinedefbfs(x,y,cnt):globalvisitedvisited[x][y]=cntq=[(x,y)]whileq:x,y=q.pop(0)foriinrange(8):nx=x+dx[i]ny=y+dy[i]if0<=nx<Hand0<=ny<Wandboard[nx][ny]==1:ifnotvisited[nx][ny]:visited[nx][ny]=cntq.append((nx,ny))if__name__=='__main__':while1:W,H=map(int,input().split())ifW==0andH==0:# 프로그램 종료
breakboard=[list(map(int,input().split()))for_inrange(H)]# 상하좌우 + 대각선 4방향
dx=[-1,1,0,0,-1,-1,1,1]dy=[0,0,-1,1,1,-1,1,-1]visited=[[0]*Wfor_inrange(H)]cnt=0forxinrange(H):foryinrange(W):ifboard[x][y]andvisited[x][y]==0:cnt+=1bfs(x,y,cnt)print(cnt)