Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

Python: import sys
sys.setrecursionlimit(10**6)


def dfs(h, w):
    if visited[h][w]:
        return 0
    elif grid[h][w] == 0:
        visited[h][w] = True
        return 0
    else:
        visited[h][w] = True
        for m in moves:
            nh, nw = h + m[0], w + m[1]
            if nh < 0 or nw < 0 or nh >= H or nw >= W:
                continue
            else:
                dfs(nh, nw)
        return 1


moves = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [-1, 1], [-1, -1], [1, -1]]


while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        sys.exit()

    grid = [list(map(int, input().split())) for _ in range(H)]

    visited = [[False] * W for _ in range(H)]

    cnt = 0
    for h in range(H):
        for w in range(W):
            cnt += dfs(h, w)

    print(cnt)

Python: import sys
sys.setrecursionlimit(3000)
def d(i,j):
    global count
    c[i][j]=1
    for k in range(3):
        for l in range(3):
            if 0<=i+k-1 and i+k<=h and 0<=j+l-1 and j+l<=w:
                if c[i+k-1][j+l-1]==0 and ma[i+k-1][j+l-1]==1:
                    d(i+k-1,j+l-1)
while(1):
    w,h=map(int,input().split())
    if w!=0 or h!=0:
        ma=[]
        for z in range(h):
            r=list(map(int,input().split()))
            ma.append(r)
        c=[[0]*w for e in range(h)]
        count=0
        for x in range(h):
            for y in range(w):
                if c[x][y]==0 and ma[x][y]==1:
                    d(x,y)
                    count+=1
        print(count)
    else:
        break
        break

Python: import collections,itertools,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
while 1:
    w,h = LI()
    if w==h==0:
        break
    ans = 0
    c = [LI() for _ in range(h)]
    seen = [[0]*w for _ in range(h)]
    stack = []
    dhdw = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
    def dfs(H,W):
        seen[H][W] = 1
        stack.append([H,W])
        while stack:
            sh,sw = stack.pop()
            for dh,dw in dhdw:
                nh,nw = sh+dh,sw+dw
                if not(0<=nh<=h-1) or not(0<=nw<=w-1) or seen[nh][nw] or c[nh][nw]==0:
                    continue
                seen[nh][nw] = 1
                stack.append([nh,nw])
    for H,W in itertools.product(range(h),range(w)):
        if seen[H][W] or c[H][W]==0:
            continue
        ans += 1
        dfs(H,W)
    print(ans)

Python: # coding: utf-8
import sys

stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip() # ignore trailing spaces
    

def main():
    w,h = na()
    if w==0 and h==0:
        return 0
    field = [na() for _ in range(h)]
    checked = [[0 for _ in range(w)] for __ in range(h)]
    ans = 0
    for i in range(h):
        for j in range(w):
            if checked[i][j] == 1 or field[i][j] == 0:
                continue
            ans += 1
            que = [(j,i)]
            while que != []:
                now = que.pop(0)
                x,y = now[0],now[1]
                checked[y][x] = 1
                for dx in range(-1,2,1):
                    for dy in range(-1,2,1):
                        tmpx = x+dx
                        tmpy = y+dy
                        if 0<=tmpx and tmpx<w and 0<=tmpy and tmpy<h:
                            if field[tmpy][tmpx] == 1 and checked[tmpy][tmpx] == 0:
                                checked[tmpy][tmpx] = 1
                                que.append((tmpx,tmpy))
    print(ans)
    return 1


if __name__ == '__main__':
    while main():
        continue

Python: #演習2-24
import sys
sys.setrecursionlimit(10**6)

def dfs(x,y):
    c[x][y] = 0
    for dx in range(-1,2):
        for dy in range(-1,2):
            nx = x + dx
            ny = y + dy
            
            if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == "1":
                dfs(nx,ny)
                

while 1:
    w,h = map(int,input().split())
    c = [input().split() for _ in range(h)]
    
    if w == 0 and h == 0:
        break
    cnt = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == "1":
                dfs(i,j)
                cnt += 1

    print(cnt)

Python: def fill(y,x):
    global w,h,l
    if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
        l[y][x+1]=0
        fill(y,x+1)
    if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
        l[y][x-1]=0
        fill(y,x-1)
    if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
        l[y+1][x]=0
        fill(y+1,x)
    if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
        l[y-1][x]=0
        fill(y-1,x)
    if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
        l[y+1][x+1]=0
        fill(y+1,x+1)
    if 0<=y-1<h and 0<=x+1<w and l[y-1][x-1]==1:
        l[y-1][x-1]=0
        fill(y-1,x-1)
    if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
        l[y+1][x-1]=0
        fill(y+1,x-1)
    if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
        l[y-1][x-1]=0
        fill(y-1,x-1)
    
while 1:
    w,h=map(int,input().split())
    if not w and not h:break
    l=[map(int,input().split()) for _ in range(h)]

    cnt=0
    while 1:
        cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
        if not cds:
            break
        else:
            y,x=cds[0]
        l[y][x]=0
        fill(y,x)
        cnt+=1
    print(cnt)

Python: from collections import deque
import sys
input = sys.stdin.readline

ldyx = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break

    MAP = [list(map(int, input().split())) for _ in range(h)]
    D = [[-1 for _ in range(w)] for _ in range(h)]    

    count = 1
    for j in range(h):
        for i in range(w):
            if D[j][i] == -1 and MAP[j][i] == 1:
                D[j][i] = count
                queue = deque([(j, i)])

                while queue:
                    (ny, nx) = queue.pop()
                    d = D[ny][nx]

                    # all neighbors
                    for (dy, dx) in ldyx:
                        new_y, new_x = ny + dy, nx + dx
                        if 0 <= new_x < w and 0 <= new_y < h:
                            if MAP[new_y][new_x] == 1:
                                if D[new_y][new_x] > -1:
                                    continue
                                D[new_y][new_x] = d
                                queue.append((new_y, new_x))

                # increase island number
                count += 1

    mv = 0
    for row in D:
        mv = max(mv, max(row))
    print(mv)