Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

w = []
h = []
dataset = []
while True:
    wt, ht = map(int, input().split())
    if wt==0 and ht==0:
        break
    w.append(wt)
    h.append(ht)
    subset = [[0]*(wt+2) for _ in range(ht+2)]
    for i in range(1, ht + 1):
        inls = [0] + list(map(int, input().split())) + [0]
        for j in range(1, wt + 1):
            subset[i][j] = inls[j]
    dataset.append(subset)

for i in range(len(dataset)):
    subset = dataset[i]
    w_ = w[i]
    h_ = h[i]
    idxs = []
    for row in range(1, h_+1):
        for col in range(1, w_+1):
            if subset[row][col] == 1:
                idxs.append((row, col))
    
    graph = {idx: deque() for idx in idxs}
    for idx in idxs:
        x = idx[0]
        y = idx[1]
        surroundings = [(x+1, y), (x+1, y+1), (x, y+1), (x-1, y+1)
                         ,(x-1, y), (x-1, y-1), (x, y-1), (x+1, y-1)]
        for surrounding in surroundings:
            if subset[surrounding[0]][surrounding[1]] == 1:
                graph[idx].append(surrounding)
    
    seen = [[0]*(w_+2) for _ in range(h_+2)]
    stack = []
    def dfs(idx):
        seen[idx[0]][idx[1]] = 1
        stack.append(idx)
        while stack:
            s = stack[-1]
            if not graph[s]:
                stack.pop()
                continue
            
            g = graph[s].popleft()
            if seen[g[0]][g[1]]:
                continue
            seen[g[0]][g[1]] = 1
            stack.append(g)

    island_count = 0
    for idx in idxs:
        if not seen[idx[0]][idx[1]]:
            dfs(idx)
            island_count += 1
    print(island_count)

Python: import sys

sys.setrecursionlimit(3000)

while True:

    w, h = map(int, input().split())

    if w == 0 and h == 0:
        exit() 

    C = []
    for i in range(h):
        c = list(map(int, input().split()))
        C.append(c)

    def is_island(i, j):
        if 0 <= i < h and 0 <= j < w and C[i][j] == 1:
            C[i][j] = 0
            return True
        else:
            return False

    def walk_to(i, j):
        if is_island(i - 1, j):
            walk_to(i - 1, j)
        if is_island(i - 1, j - 1):
            walk_to(i - 1, j - 1)
        if is_island(i, j - 1):
            walk_to(i, j - 1)
        if is_island(i + 1, j - 1):
            walk_to(i + 1, j - 1)
        if is_island(i + 1, j):
            walk_to(i + 1, j)
        if is_island(i + 1, j + 1):
            walk_to(i + 1, j + 1)
        if is_island(i, j + 1):
            walk_to(i, j + 1)
        if is_island(i - 1, j + 1):
            walk_to(i - 1, j + 1)

    count = 0        
    for i in range(h):
        for j in range(w):
            if is_island(i, j):
                count = count + 1
                walk_to(i, j)

    print(count)

Python: import sys

sys.setrecursionlimit(10000)


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


def dfs(field, w, h, height, width):
    field[height][width] = 0

    for i in range(8):
        nw = width + dx[i]
        nh = height + dy[i]

        if nw < 0 or nw >= w or nh < 0 or nh >= h:
            continue
        if field[nh][nw] == 0:
            continue

        dfs(field, w, h, nh, nw)
    return


def solve():
    ans = []
    while True:
        w, h = map(int, input().split())
        if w == h == 0:
            break

        field = [list(map(int, input().split())) for _ in range(h)]
        count = 0
        for c in range(h):
            for r in range(w):
                if field[c][r] == 1:
                    count += 1
                    dfs(field, w, h, c, r)

        ans.append(count)
    print(*ans, sep="\n")


if __name__ == "__main__":
    solve()

Python: import sys
sys.setrecursionlimit(10**7)
def dfs_rec():
    W,H = list(map(int,input().split()))
    if W == 0 and H == 0:
        exit()
    C = [list(map(int,input().split())) for _ in range(H)]
    D = [[0,1],[0,-1],[1,0],[1,1],[1,-1],[-1,0],[-1,1],[-1,-1]]
    
    def dfs(C, x, y):
        C[y][x] = 2
        for d in D:
            if x+d[0] < 0 or x+d[0] > W-1 or y+d[1] < 0 or y+d[1] > H-1:
                continue
            if C[y+d[1]][x+d[0]] == 0 or C[y+d[1]][x+d[0]] == 2:
                continue
            dfs(C, x+d[0], y+d[1])
    
    count = 0
    for w in range(W):
        for h in range(H):
            if C[h][w] == 0 or C[h][w] == 2:
                continue
            dfs(C, w, h)
            count += 1
    
    print(count)

while True:
    dfs_rec()