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 copy

move = ((1, 0), (0, 1), (0, -1), (-1, 0), (1, 1), (-1, -1), (-1, 1), (1, -1))


def dfs(start):
    field[start[1]][start[0]] = '0'
    CP = [start]
    while CP:
        NP = []
        for cp in CP:
            for m in move:
                n_p = (cp[0]+m[0], cp[1]+m[1])
                if not(n_p[0] < 0 or n_p[1] < 0 or n_p[1] >= h or n_p[0] >= w) and field[n_p[1]][n_p[0]] == '1':
                    field[n_p[1]][n_p[0]] = '0'
                    NP.append(n_p)
        CP = NP


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    field = [list(input().split()) for _ in range(h)]
    count = 0
    for y in range(h):
        for x in range(w):
            if field[y][x] == '1':
                dfs((x, y))
                count += 1
    print(count)

Python: #00:45
import sys

sys.setrecursionlimit(10000000)


dx = [1, 0, -1, 0,-1,-1,1,1]
dy = [0, 1, 0, -1,-1,1,-1,1]



#左上からせめて深さ優先で探索する問題
while True:
    # 地図作る
    count = 0
    width,hight = map(int,input().split())
    if width ==0 :
        break
    d =[]
    for _ in range(hight):
        d.append(list(map(int,input().split())))
    
    def dfs(x, y):
    # いまたどり着いたはずなので1に塗りつぶす
        d[y][x]=0

    #4方向ループ
    #あくまで方向の表し方dxは
        for i in range(8):
            ny = y + dx[i]
            nx = x + dy[i]

        #行ったことがなくって、迷路の範囲内で、障害物がなければ、進む
            if 0 <= ny and ny < hight \
              and 0 <= nx and nx < width \
              and d[ny][nx] == 1:
                dfs(nx,ny) 

    

    for y in range(hight):
        for x in range(width):            
            if d[y][x] == 1:
                count = count + 1
                dfs(x,y)
    print(count)

Python: def erase(w, h, x, y):
    c[y][x] = '0'
    for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
        nx, ny = x + dx, y + dy
        if 0 <= nx <= w -1 and 0 <= ny <= h - 1:
            if c[ny][nx] == '1':
                erase(w, h, nx, ny)

while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [[i for i in input().split()] for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == '1':
                ans += 1
                erase(w, h, x, y)
    print(ans)

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

w, h = map(int,input().split())
c = [list(map(int,input().split())) for j in range(h)]
count = 0

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 < w and 0 <= ny <= h and c[nx][ny] == 1 ):
                dfs(nx, ny)
    
for i in range(h):
    for j in range(w):
        if (c[i][j] == 1):
            dfs(i,j)
            count += 1

print(count)

Python: k = [-1,0,1]
def search(x,y,m):
    if x<0 or x>=len(m[0]) or y<0 or y>=len(m): return
    if m[y][x]==0: return
    m[y][x] = 0
    for i in k:
        for j in k:
            search(x+i,y+j,m)

while True:
    c = 0
    w,h = map(int,input().split())
    if w==0: break
    m = [list(map(int,input().split())) for i in range(h)]
    for i in range(h):
        for j in range(w):
            if m[i][j]==1:
                search(j, i, m)
                c += 1
    print(c)

Python: import sys
from collections import deque
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]


def main():
    move = [[i, j] for i in range(-1,2) for j in range(-1,2) if not(i==j and i == 0)]
    try:
        while True:
            w, h = MI()
            if w == h and h == 0:
                sys.exit()
            d = Init(h, w, True)
            mylist = [LI() for i in range(h)]
            result = 0
            for i in range(h):
                for j in range(w):
                    if d[i][j] and mylist[i][j] == 1:
                        result+=1
                        dq = deque()
                        dq.append([i, j])
                        while dq:
                            x, y = dq.pop()
                            for x2, y2 in move:
                                x2 += x
                                y2 += y
                                if x2 >= 0 and x2 < h and y2 >= 0 and y2 < w:
                                    if mylist[x2][y2] == 1 and d[x2][y2]:
                                        dq.append([x2,y2])
                                        d[x2][y2] = False
                                    elif d[x2][y2]:
                                        d[x2][y2] = False
            print(result)
    except:
        sys.exit()
    
    
    
if __name__ == "__main__":
    main()

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

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

while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [[i for i in input().split()] for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == '1':
                ans += 1
                erase(w, h, x, y)
    print(ans)