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

def main():
    ans=[]
    while True:
        w,h=map(int,input().split())
        if w==0 and h==0:
            break
        dis=[-1,0,1]
        c=[[0]*(w+2)]
        for i in range(h):
            c.append([0]+list(map(int,input().split()))+[0])
        c.append([0]*(w+2))
        chk=[[0]*(w+2) for _ in range(h+2)]

        def dfs(x,y):
            chk[x][y]=1
            if x<=0 or y<=0 or x>=h+1 or y>=w+1 or c[x][y]==0:
                return
            for dx in dis:
                for dy in dis:
                    if chk[x+dx][y+dy]==0 and c[x+dx][y+dy]==1:
                        dfs(x+dx,y+dy)
            return

        cnt=0
        for i in range(1,h+1):
            for j in range(1,w+1):
                if chk[i][j]==0:
                    chk[i][j]=1
                    if c[i][j]==1:
                        dfs(i,j)
                        cnt+=1
        ans.append(cnt)
    for i in ans:
        print(i)

if __name__ == '__main__':
    main()

Python: from sys import stdin
from sys import setrecursionlimit
setrecursionlimit(10**9)

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

while True:
    readline=stdin.readline
    w,h=map(int,readline().split())
    if w==0 and h==0:
        break
    grid=[list(map(int,readline().split())) for _ in range(h)]

    cnt=0
    for y in range(h):
        for x in range(w):
            if grid[y][x]==1:
                dfs(y,x)
                cnt+=1

    print(cnt)

Python: import sys
sys.setrecursionlimit(10000)
def dfs(x,y):
    for i in range(-1,2):
        if 0<=x+i<a[1]:
            for j in range(-1,2):
                if 0<=y+j<a[0]:
                    if G[x+i][y+j]==1:
                        G[x+i][y+j] = 0
                        dfs(x+i,y+j)
while  True:
    a = list(map(int, input().split()))
    if a[0] == 0:
        break
    G = []
    cnt = 0
    for i in range(a[1]):
        G.append(list(map(int, input().split())))
    for i in range(a[1]):
        for j in range(a[0]):
            if G[i][j] == 1:
                cnt += 1
                dfs(i,j)
    print(cnt)

Python: import sys

sys.setrecursionlimit(10 ** 4)

w = h = 0
islands = []


def dfs(ii: int, jj: int) -> None:
    global islands
    islands[jj][ii] = 0

    for i in range(ii - 1, ii + 2):
        for j in range(jj - 1, jj + 2):
            if 0 <= i < w and 0 <= j < h and islands[j][i] > 0:
                dfs(i, j)


def main() -> None:
    ans = []
    global islands, w, h
    while True:
        cnt = 0
        w, h = map(int, input().split())
        if w + h == 0:
            break
        islands = [list(map(int, input().split())) for _ in range(h)]
        for j in range(h):
            for i in range(w):
                if islands[j][i] > 0:
                    cnt += 1
                    dfs(i, j)
        ans += [cnt]
    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

Python: import sys
sys.setrecursionlimit(10000)


def integrate(x, i, j):
    if x[i-1][j-1] != 1 and x[i-1][j] != 1 and x[i-1][j+1] != 1 \
        and x[i][j-1] != 1 and x[i][j+1] != 1 and x[i+1][j-1] != 1 \
            and x[i+1][j] != 1 and x[i+1][j+1] != 1:
            return 0
    if x[i-1][j] == 1:
        x[i-1][j] = 0
        integrate(x, i-1, j)
    if x[i-1][j-1] == 1:
        x[i-1][j-1] = 0
        integrate(x, i-1, j-1)
    if x[i+1][j] == 1:
        x[i+1][j] = 0
        integrate(x, i+1, j)
    if x[i+1][j+1] == 1:
        x[i+1][j+1] = 0
        integrate(x, i+1, j+1)
    if x[i][j-1] == 1:
        x[i][j-1] = 0
        integrate(x, i, j-1)
    if x[i+1][j-1] == 1:
        x[i+1][j-1] = 0
        integrate(x, i+1, j-1)
    if x[i][j+1] == 1:
        x[i][j+1] = 0
        integrate(x, i, j+1)
    if x[i-1][j+1] == 1:
        x[i-1][j+1] = 0
        integrate(x, i-1, j+1)
    return 0


answer = []

while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    c = [[] for i in range(h+2)]
    c[0] = [2 for i in range(w+2)]

    for i in range(1, h+1):
        c[i].append(2)
        c_i = list(map(int, input().split()))
        c[i].extend(c_i)
        c[i].append(2)

    c[h+1] = [2 for i in range(w+2)]

    count = 0

    for i in range(1, h+1):
        for j in range(1, w+1):
            if c[i][j] == 1:
                c[i][j] = 0
                integrate(c, i, j)
                count += 1

    answer.append(count)

for i in range(len(answer)):
    print(answer[i])

Python: import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    ans = []
    while True:
        W, H = map(int, input().split())
        if W == H == 0:
            break
        grid = [list(map(int, input().split())) for _ in range(H)]

        res = 0
        visited = [[False] * W for _ in range(H)]
        for y in range(H):
            for x in range(W):
                if grid[y][x] == 1 and not visited[y][x]:
                    res += 1
                    que = deque([[y, x]])
                    while que:
                        h, w = que.pop()
                        for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)):
                            next_h, next_w = h + dh, w + dw
                            if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W or grid[next_h][next_w] == 0:
                                continue
                            else:
                                if not visited[next_h][next_w]:
                                    visited[next_h][next_w] = True
                                    que.append([next_h, next_w])
        ans.append(res)
    print(*ans, sep="\n")


if __name__ == '__main__':
    resolve()