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: def solver():
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        return None
    islands = set()
    counter = 2
    c = [[0] + [int(x) for x in input().split()] + [0]  for _ in range(h)]
    c = [[0 for _ in range(w+2)]] + c + [[0 for _ in range(w+2)]]
    for i in range(1, h+1):
        for j in range(1, w+1):
            if c[i][j] > 0:
                neighbour = {c[i-1][j-1], c[i-1][j], c[i-1][j+1], c[i][j-1]}
                if max(neighbour) > 1:
                    c[i][j] = max(neighbour)
                    if len(neighbour - {0}) > 1:
                        islands -= {max(neighbour)}
                else:
                    c[i][j] = counter
                    islands |= {counter}
                    counter += 1
    return len(islands)

outputs = []
while(True):
    ans = solver()
    if ans == None:
        break
    outputs.append(ans)
print('\n'.join(map(str, outputs)))

Python: # Union Find
#0オリジン以下注意

#xの根を求める
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
    
def unite(x, y):
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きい方がx
        if par[x] > par[y]:
            x, y = y, x
        par[x] += par[y] #ここで集合の個数を更新している
        par[y] = x
        return True
    
#xとyが同じ集合に含まれているかどうかを判定(同じ集合にあればTrue)
def same(x, y):
    return find(x) == find(y)

#xが属する集合の個数
def size(x):
    return -par[find(x)]

#初期化
#根なら-size, 子なら親の頂点
################以下にコードを書く

while True:
    w, h = map(int, input().split())
    par = [-1]*(w*h)
    if (w, h) == (0, 0):
        break
    li = []
    for i in range(h):
        li.append(list(map(int, input().split())))
    for i in range(h):
        for j in range(w-1):
            if li[i][j] == li[i][j+1]:
                unite(w*i+j, w*i+j+1)
    for j in range(w):
        for i in range(h-1):
            if li[i][j] == li[i+1][j]:
                unite(w*i+j, w*i+j+w)
    for i in range(h):
        for j in range(w):
            I1 = i+1
            I2 = i-1
            J = j+1
            if 0 <= I1 and I1<h and 0<=J and J<w:
                if li[i][j] == 1 and li[I1][J] == 1:
                    unite(w*i+j, w*I1+J)
            if 0 <= I2 and I2<h and 0<=J and J<w:
                if li[i][j] == 1 and li[I2][J] == 1:
                    unite(w*i+j, w*I2+J)

                    
    #島を数える
    #陸のリスと
    li_riku = []
    for i in range(h):
        for j in range(w):
            if li[i][j]==1:
                li_riku.append(w*i+j)
    li_island = []
    sum_size = len(li_riku)
    for i in li_riku:
        k = find(i)
        if k not in li_island:
            li_island.append(k)
    print(len(li_island))

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

first = True
while True:
    W, H = map(int, sys.stdin.readline().strip().split())
    if (W == 0) and (H == 0):
        break

    fp = [True] * (W * H)  # 探索可能示す

    M = []
    for h in range(H):
        m = list(map(int, sys.stdin.readline().strip().split()))
        M.append(m)
        for i, j in enumerate(m):
            if j == 0:
                fp[h * W + i] = False


    def dfs(u):  # u は座標
        x, y = u
        fp[y * W + x] = False  # 探索済みを示す

        for i in [-1, 0, 1]:
            x_next = x + i
            if (x_next < 0) or (x_next > W - 1):
                continue
            for j in [-1, 0, 1]:
                y_next = y + j
                if (y_next < 0) or (y_next > H - 1):
                    continue
                # print(f'y_next: {y_next}, x_next: {x_next}')
                # print(f'{fp[y_next * W + x_next]}')
                # print(f'{M[y][x]}')
                if fp[y_next * W + x_next] and (M[y][x] == 1):
                    dfs((x_next, y_next))


    count = 0
    while True in fp:
        count += 1
        # print(f'count : {count}')
        # print(f'fp: {fp}')

        y = int((fp.index(True) / W))
        x = int((fp.index(True) - y * W))
        # print(f'x: {x}, y: {y}')
        dfs((x, y))

    # if first:
    #     first = False
    # else:
    #     print('\n', end='')
    print(count, end='\n')