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 queue

def proceess(start_x,start_y):
    #insland = []
    #insland.append([start_x,start_y])
    new_island = queue.Queue()
    map[start_x][start_y] = 0
    new_island.put([start_x,start_y])
    while True:
        ob = new_island.get()
        x = ob[0]
        y = ob[1]
        #print("x : " + str(x) + ", y : " + str(y))

        for i in [-1,0,1]:
            for j in [-1,0,1]:
                nx = x + i
                ny = y + j

                if nx >= 0 and ny >= 0 and nx < w and ny < h:
                    #print(map[nx][ny])
                    if map[nx][ny] == 1:
                        #insland.append([nx,ny])
                        #print("nx : " + str(nx) + ", ny : " + str(ny))
                        new_island.put([nx,ny])
                        map[nx][ny] = 0

        if new_island.empty():
            break

while True:
    map = []

    tmp = input().split(" ")
    w = int(tmp[0])
    h = int(tmp[1])

    if w == 1 and h == 1:
        break


    for j in range(h):
        nova = []
        arm = input()
        for k in arm.split(" "):
            #k.rstrip('\n')
            nova.append(int(k))
        map.append(nova)

    #print(map)

    count = 0
    for i in range(w):
        for j in range(h):
            if map[i][j] == 1:
                #print("i : " + str(i) + ", j : " + str(j))
                proceess(i,j)
                count += 1

    print(count)

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()