Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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)