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: import sys
sys.setrecursionlimit(10 ** 6)

def dfs(here_w, here_h):
    if 0 <= here_w < width and 0 <= here_h < height:
        if target[here_h][here_w]:
            target[here_h][here_w] = 0
            for x, y in all_move:
                dfs(here_w + x, here_h + y)
                
all_move = [[1, 1], [1, 0], [1, -1], [0, 1], [0, -1], [-1, 1], [-1, 0], [-1, -1]]
while True:
    width, height = map(int, input().split(" "))
    if not width:
        break
    target = []
    for h in range(height):
        temp = [int(n) for n in input().split(" ")]
        target.append(temp)
    # print(width, height, temp)
    answer = 0
    for h in range(height):
        for w in range(width):
            if target[h][w]:
                answer += 1
                dfs(w, h)
    print(answer)

Python: UNSEARCHED = 0
SEARCHED = -1
LAND = 1
SEA = 0
result = []

while(True):
    str = input().split(" ")
    w = int(str[0])
    h = int(str[1])
    if w == 0 and h == 0:
        break
    area = [[0 for i in range(w)] for i in range(h)]
    status = [[UNSEARCHED for i in range(w)] for i in range(h)]
    index = 0
    
    for i in range(h):
        str = input().split(" ")
        area[i] = [int(str[l]) for l in range(w)]
    
    for i in range(h):
        for l in range(w):
            if status[i][l] == UNSEARCHED and area[i][l] == LAND:
                index += 1
                status[i][l] = index
    
                # (y,x)
                stack = [(i+1, l), (i-1, l), (i, l+1), (i, l-1), (i+1, l+1), (i+1, l-1), (i-1, l-1), (i-1, l+1)]
                while(len(stack) > 0):
                    s = stack.pop(0)
                    
                    if not (0 <= s[0] < h and 0 <= s[1] < w):
                        continue
    
                    if status[s[0]][s[1]] == UNSEARCHED and area[s[0]][s[1]] == LAND :
                        status[s[0]][s[1]] = index
                        stack.append((s[0], s[1]))
                        stack.append((s[0], s[1]+1))
                        stack.append((s[0], s[1]-1))
                        stack.append((s[0]+1, s[1]))
                        stack.append((s[0]-1, s[1]))
                        stack.append((s[0]+1, s[1]+1))
                        stack.append((s[0]+1, s[1]-1))
                        stack.append((s[0]-1, s[1]+1))
                        stack.append((s[0]-1, s[1]-1))
                    elif area[s[0]][s[1]] == SEA:
                        status[s[0]][s[1]] = SEARCHED
    
            elif status[i][l] == UNSEARCHED:
                status[i][l] = SEARCHED
    
    result.append(index)

for i in result:
    print(i)

Python: # -*- coding: utf-8 -*-


def search(n, m, tile, N, M):
    for dy, dx in zip([-1, -1, -1, 0, 0, 1, 1, 1], [-1, 0, 1, -1, 1, -1, 0, 1]):
        if 0 <= dy + n < N and 0 <= dx + m < M:
            if tile[dy + n][dx + m] == '1':
                tile[dy + n][dx + m] = '2'
                search(dy + n, dx + m, tile, N, M)


while True:
    M, N = map(int, input().split())
    if M == 0 and N == 0:
        break
    tile = [input().split() for n in range(N)]
    ans = 0
    for n in range(N):
        for m in range(M):
            if tile[n][m] == '1':
                tile[n][m] = '2'
                search(n, m, tile, N, M)
                ans += 1
    print(ans)

Python: import sys
def dfs(tiles, W, H, x, y):
    tiles[y][x] = '0'
    for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
        if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
            dfs(tiles, W, H, x+dx, y+dy)
    return

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [input().split() for h in range(H)]
        cnt = 0
        for h in range(H):
            for w in range(W):
                if tiles[h][w] == '1':
                    cnt += 1
                    dfs(tiles, W, H, w, h)
        print(cnt)

if __name__ == '__main__':
    sys.setrecursionlimit(10000)
    main()

Python: # coding: utf-8
import sys
sys.setrecursionlimit(2147483647)
def check(p):
    global field
    if(field[p[1]][p[0]]=='1'):
        field[p[1]][p[0]]='0'
    if(0<=p[0]+1<w and field[p[1]][p[0]+1]=='1'):
        check((p[0]+1,p[1]))
    if(0<=p[0]-1<w and field[p[1]][p[0]-1]=='1'):
        check((p[0]-1,p[1]))
    if(0<=p[1]+1<h and field[p[1]+1][p[0]]=='1'):
        check((p[0],p[1]+1))
    if(0<=p[1]-1<h and field[p[1]-1][p[0]]=='1'):
        check((p[0],p[1]-1))
    if(0<=p[0]+1<w and 0<=p[1]-1<h and field[p[1]-1][p[0]+1]=='1'):
        check((p[0]+1,p[1]-1))
    if(0<=p[0]-1<w and 0<=p[1]-1<h and field[p[1]-1][p[0]-1]=='1'):
        check((p[0]-1,p[1]-1))
    if(0<=p[0]+1<w and 0<=p[1]+1<h and field[p[1]+1][p[0]+1]=='1'):
        check((p[0]+1,p[1]+1))
    if(0<=p[0]-1<w and 0<=p[1]+1<h and field[p[1]+1][p[0]-1]=='1'):
        check((p[0]-1,p[1]+1))
     
while 1:
    ans=0
    w,h=map(int,input().split())
    if w==0:
        break
    field=[]
    for i in range(h):
        s=''.join(input().split())
        field.append(list(s))
    for i in range(h):
        for j in range(w):
            if field[i][j]=='1':
                check((j,i))
                ans+=1
    print(ans)

Python: import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)


def dfs(h, w):
    board[h][w] = 0
    for dh in range(-1, 2):
        for dw in range(-1, 2):
            nh = h + dh
            nw = w + dw
            if 0 <= nh < H and 0 <= nw < W and board[nh][nw] == 1:
                dfs(nh, nw)


while True:
    W, H = map(int, input().split())
    if H == 0 and W == 0:
        break
    board = [list(map(int, input().split())) for _ in range(H)]
    ans = 0
    for h in range(H):
        for w in range(W):
            if board[h][w]:
                dfs(h, w)
                ans += 1
    print(ans)

Python: d = [-1,0,1]
def search(x,y,m):
    if not(0<=x<len(m[0]) and 0<=y<len(m)) or m[y][x]=='0': return
    m[y][x] = '0'
    for dy in d:
        for dx in d:
            search(x+dx,y+dy,m)
while True:
    w,h = map(int,input().split())
    if w==h==0: break
    m = [input().split() for _ in range(h)]
    c = 0
    for y in range(h):
        for x in range(w):
            if m[y][x]=='1':
                c += 1
                search(x,y,m)
    print(c)