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: import collections

while True:
    w, h = map(int, input().split())
    if not w and not h:
        break
    graph = [list(map(int, input().split())) for _ in range(h)]
    seen = [[False] * w for i in range(h)]

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

    def dfs(v):
        stack = collections.deque()
        stack.append(v)
        while stack:
            y, x = stack.pop()
            for i in range(8):
                ny = y + dy[i]
                nx = x + dx[i]
                if not (-1 < ny < h and -1 < nx < w):
                    continue
                if not graph[ny][nx]:
                    continue
                if seen[ny][nx]:
                    continue
                seen[ny][nx] = True
                stack.append([ny, nx])

    count = 0
    for i in range(h):
        for j in range(w):
            if graph[i][j] and not seen[i][j]:
                dfs([i, j])
                count += 1

    print(count)

Python: #!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

while 1:
    w,h = LI()
    if w == 0 and h == 0:
        break
    c = [LI() for _ in range(h)]
    check = [[True]*w for _ in range(h)]

    dx,dy = [1,1,0,-1,-1,-1,0,1],[0,1,1,1,0,-1,-1,-1]
    ans = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] and check[i][j]:
                que = deque()
                que.append((i,j))
                check[i][j] = False
                ans += 1
                while que:
                    y,x = que.popleft()
                    for k in range(8):
                        ny,nx = y+dy[k],x+dx[k]
                        if 0 <= ny < h and 0 <= nx < w:
                            if check[ny][nx]:
                                if c[ny][nx]:
                                    que.append((ny,nx))
                                    check[ny][nx] = False
    print(ans)

Python: def fill(y,x):
    global w,h,l
    if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
        l[y][x+1]=0
        fill(y,x+1)
    if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
        l[y][x-1]=0
        fill(y,x-1)
    if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
        l[y+1][x]=0
        fill(y+1,x)
    if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
        l[y-1][x]=0
        fill(y-1,x) 
    
while 1:
    w,h=map(int,input().split())
    if not w and not h:break
    l=[map(int,input().split()) for _ in range(h)]

    cnt=0
    while 1:
        cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
        if not cds:
            break
        else:
            y,x=cds[0]
        l[y][x]=0
        fill(y,x)
        cnt+=1
    print(cnt)

Python: dy = (-1,-1,0,1,1,1,0,-1)
dx = (0,1,1,1,0,-1,-1,-1)

def main():
    while True:
        w,h = map(int,input().split())
        if h == 0:
            return
        grid = [list(map(int,input().split())) for _ in range(h)]
        visited = [[0] * w for _ in range(h)]
        ans = 0
        for i in range(h):
            for j in range(w):
                if grid[i][j] == 1 and visited[i][j] == 0:
                    ans += 1
                    stack = [(i,j)]
                    visited[i][j] = 1
                    while stack:
                        p,q = stack.pop()
                        for k in range(8):
                            y = p + dy[k]
                            x = q + dx[k]
                            if y < 0 or y >= h:
                                continue
                            if x < 0 or x >= w:
                                continue
                            if grid[y][x] == 1 and visited[y][x] == 0:
                                stack.append((y,x))
                                visited[y][x] = 1
        print(ans)
if __name__ == '__main__':
    main()

Python: def main():
    W, H = (int(i) for i in input().split())
    while H != 0 or W != 0:
        c = [[int(i) for i in input().split()] for j in range(H)]
        seen = [[False for i in range(W)] for j in range(H)]
        def dfs(G,h,w,seen,H,W):
            todo = []
            hws = ((1,0),(0,1),(-1,0),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1))
            seen[h][w] = True
            todo.append((h,w))
            # todoが非空の間繰り返し
            while todo:
                h, w = todo[-1]
                for dh,dw in hws:
                    next_h = h + dh
                    next_w = w + dw
                    if not ((0 <= next_h < H) and (0 <= next_w < W)): continue
                    if G[next_h][next_w] == 0: continue
                    if seen[next_h][next_w]: continue
                    seen[next_h][next_w] = True
                    todo.append((next_h,next_w))
                    break
                else:
                    todo.pop()
        ans = 0
        for i in range(H):
            for j in range(W):
                if c[i][j] == 0 or seen[i][j]: continue
                dfs(c,i,j,seen,H,W)
                ans += 1
        print(ans)
        W, H = (int(i) for i in input().split())

if __name__ == '__main__':
    main()