https://www.acmicpc.net/problem/4963
import sys
sys.setrecursionlimit(10000)
dx=[0,0,1,-1,1,1,-1,-1]
dy=[1,-1,0,0,1,-1,-1,1]
def find(a,b):
check[a][b]=True
for k in range(8):
x=a+dx[k]
y=b+dy[k]
if x>=0 and x<h and y>=0 and y<w:
if arr[x][y]==1 and check[x][y]==False: find(x,y)
while True:
w,h=map(int,input().split())
if w==0 and h==0: break
arr=[[0 for _ in range(w)] for _ in range(h)]
for i in range(h):
st=list(map(int,input().split()))
for j in range(w):
arr[i][j]=st[j]
check = [[False for _ in range(w)] for _ in range(h)]
count=0
for i in range(h):
for j in range(w):
if arr[i][j]==1 and check[i][j]==False:
find(i,j)
count+=1
print(count)