Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

Python: def root(i):
    global roots
    if roots[i]==i:
        return i
    else:
        roots[i]=root(roots[i])
        return roots[i]

def unite(i, j):
    global roots
    ri=root(i)
    rj=root(j)
    #roots[max(ri, rj)]=min(ri, rj)
    if ri==rj: return
    roots[ri]=rj

directions=[(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]
while True:
    l=input().split()
    w=int(l[0])
    h=int(l[1])
    if w==0 and h==0: break
    map=[[False for _ in range(h+2)] for _ in range(w+2)]
    roots=[i for i in range((w+2)*(h+2))]
    for hi in range(h):
        l=input().split()
        for wi in range(w):
            map[wi+1][hi+1]=bool(int(l[wi]))

    for hi in range(1, h+1):
        for wi in range(1, w+1):
            if not map[wi][hi]: continue
            i=hi*(w+2)+wi
            for d in directions:
                if not map[wi+d[0]][hi+d[1]]: continue
                j=(hi+d[1])*(w+2)+(wi+d[0])
                unite(i, j)
    res=set()
    for hi in range(1, h+1):
        for wi in range(1, w+1):
            if not map[wi][hi]: continue
            i=hi*(w+2)+wi
            res.add(root(i))
    print(len(res))

Python: # -*- coding: utf-8 -*-
def dfs(x,y):
    c[x][y] = 0
    for dx in [-1,0,1]:
        for dy in [-1,0,1]:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
                dfs(nx, ny)
 
while True:
    w, h = map(int,input().split())
    if w == 0 and h == 0 :
        break
 
    c = [list(map(int,input().split())) for j in range(h)]
    count = 0
 
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1:
                dfs(i,j)
                count += 1
 
    print(count)

Python: # 25

import sys
import itertools

sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限
while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    c_l = [[int(x) for x in input().split()] for y in range(h)]
    s_l = [[True] * w for x in range(h)]
    v_l = list(itertools.product([-1,0,1],repeat=2))
    
    def dfs(y, x):
        if x < 0 or y < 0 or x >=w or y >= h:
            return False
        if not s_l[y][x]:
            return False
        s_l[y][x] = False
        _c = c_l[y][x]
        if _c == 0:
            return False
        for _v in v_l:
            dfs(y+_v[0],x+_v[1])
        return True

    ans = 0
    for i in range(h):
        for j in range(w):
            if s_l[i][j]:
                if dfs(i, j):
                    ans += 1
                    
    print(ans)

Python: def f(x,y):
    a[y][x]='0'
    for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:
        dx+=x;dy+=y
        if 0<=dx<w and 0<=dy<h and a[dy][dx]=='1':f(dx,dy)
while 1:
    w,h=map(int,input().split())
    if w==0:break
    a=[list(input().split()) for _ in[0]*h];b=0
    for i in range(w):
        for j in range(h):
            if a[j][i]=='1':
                b+=1;f(i,j)
    print(b)

Python: import queue

def proceess(start_x,start_y):
    #print("start_x : " + str(start_x) + ", start_y : " + str(start_y))
    #insland = []
    #insland.append([start_x,start_y])
    new_island = queue.Queue()
    map[start_x][start_y] = 0
    new_island.put([start_x,start_y])
    while True:
        ob = new_island.get()
        x = ob[0]
        y = ob[1]
        #print("x : " + str(x) + ", y : " + str(y))

        for i in [-1,0,1]:
            for j in [-1,0,1]:
                nx = x + i
                ny = y + j

                if nx >= 0 and ny >= 0 and nx < h and ny < w:
                    if map[nx][ny] == 1:
                        #insland.append([nx,ny])
                        #print("ny : " + str(ny) + ", nx : " + str(nx))
                        new_island.put([nx,ny])
                        map[nx][ny] = 0

        #print("end 8 count.")
        if new_island.empty():
            break

while True:
    map = []

    tmp = input().split(" ")
    w = int(tmp[0])
    h = int(tmp[1])

    if w == 0 and h == 0:
        break


    for j in range(h):
        nova = []
        arm = input()
        for k in arm.split(" "):
            #k.rstrip('\n')
            nova.append(int(k))
        map.append(nova)

    #print(map)

    count = 0
    for i in range(h):
        for j in range(w):
            if map[i][j] == 1:
                #print("i : " + str(i) + ", j : " + str(j))
                proceess(i,j)
                count += 1

    print(count)

Python: # -*- coding: utf-8 -*-
import sys
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()


def main():
    while True:
        W,H=map(int,input().split())
        if W==H==0:
            exit()
        C=[[] for _ in range(H+2)]
        C[0]=C[H+1]=[-1]*(W+2)
        used=[[False]*(W+2) for _ in range(H+2)]
        rands=[]
        for column in range(1,H+1):
            C[column]=[-1]+list(map(int,input().split()))+[-1]
            for row,x in enumerate(C[column]):
                if x==1:
                    rands.append((column,row))

        ans=0
        
        if rands:
            s=[]
            while rands:
                y,x=rands.pop()
                if used[y][x]:
                    continue
                s.append((y,x))
                while s:
                    y,x=s.pop()
                    if used[y][x]:
                        continue
                    used[y][x]=True
                    for dy in range(-1,2):
                        for dx in range(-1,2):
                            ny,nx=y+dy,x+dx
                            if dy==dx==0 or used[ny][nx]:
                                continue
                            if C[ny][nx]==1:
                                s.append((ny,nx))
                ans+=1
        print(ans)
                

if __name__ == '__main__':
    main()

Python: import sys
input = lambda: sys.stdin.readline().rstrip() 

def resolve():
    while True:
        w, h = map(int, input().split())
        if w==h==0:
            break
        c = [list(map(int, input().split())) for _ in range(h)]

        stk = []
        visited = [[False]*w for _ in range(h)]
        dir = ((0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1))

        ans = 0

        for i in range(h):
            for j in range(w):
                if c[i][j]==1 and not visited[i][j]:
                    stk.append((i, j))
                    while len(stk)>0:
                        a = stk.pop()
                        visited[a[0]][a[1]] = True
                        for d in dir:
                            ad = (a[0]+d[0], a[1]+d[1])
                            if 0<=ad[0]<h and 0<=ad[1]<w and c[ad[0]][ad[1]]==1 and not visited[ad[0]][ad[1]]:
                                stk.append(ad) 
                    ans += 1

        print(ans)

if __name__ == '__main__':
    resolve()