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 sys
sys.setrecursionlimit(10 ** 6)
islands = []
visited_list = []
w_list = []
h_list = []
while True:
    w ,h = map(int,input().split())
    if w == 0 and h == 0:
        break
    w_list.append(w)
    h_list.append(h)
    island = [list(input().split()) for _ in range(h)]
    islands.append(island)
    visited_list.append([[False] * w for _ in range(h)])

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

def dfs(island_m ,now_row , now_column , idx):
    if now_row < 0 or h_list[idx] <= now_row or now_column < 0 or w_list[idx] <= now_column or island_m[now_row][now_column] == "0":
        return
    if visited_list[idx][now_row][now_column]:
        return
    visited_list[idx][now_row][now_column] = True
    for i in range(8):
        dfs(island_m,now_row + dx[i] ,now_column + dy[i],idx)


for m in range(len(islands)):
    cnt = 0
    for row in range(h_list[m]):
        for column in range(w_list[m]):
            if not visited_list[m][row][column] and islands[m][row][column] == "1":
                dfs(islands[m] , row , column , m)
                cnt += 1
    print(cnt)

Python: from collections import deque
from itertools import product
import itertools

def dfs(w, h, graph):
    visited = [[0] * w for _ in range(h)]
    dy_dx = [(0, 0), (0, 1), (0, -1), (1, 1), (1, -1), (1, 0), (-1, 1), (-1, -1), (-1, 0)]
    dots = itertools.product(range(h), range(w))
    count = 0
    for dot in dots:
        dot_y, dot_x = dot
        if graph[dot_y][dot_x] == 0:
            continue
        if visited[dot_y][dot_x] == 1:
            continue

        q = deque()
        q.append((dot_y, dot_x))
        while q:
            move_y, move_x = q.pop()

            for delta in dy_dx:
                dy, dx = delta

                moved_x = move_x + dx
                moved_y = move_y + dy

                if moved_y < 0 or h -1 < moved_y or moved_x < 0 or w -1 < moved_x:
                    continue
                if graph[moved_y][moved_x] == 0:
                    continue
                if visited[moved_y][moved_x] == 0:
                    q.append((moved_y, moved_x))
                    visited[moved_y][moved_x] = 1
        
        count += 1

    return count

if __name__ == "__main__":

    answer_list = []
    while True:
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break

        graph = [list(map(int, input().split())) for _ in range(h)]
        answer_list.append(dfs(w, h, graph))
    
    for answer in answer_list:
        print(answer)

Python: import sys
sys.setrecursionlimit(10**8)

def dfs(h,w,visited,S):
    #print(h,w)
    for i in range(8):
        h0 = h+dh[i]
        w0 = w+dw[i]
        if visited[h0][w0]==False and S[h0][w0]=='1':
            visited[h0][w0]=True
            dfs(h0,w0,visited,S)

ans = []
while True:
    W,H = map(int, input().split())
    if (W,H) == (0,0):
        break
    S = [0]*(H+2)
    S[0] = ['*']*(W+2)
    S[H+1] = ['*']*(W+2)
    for h in range(1,H+1):
        S[h] = ['*'] + input().split() + ['*']

    visited = [[False]*(W+2) for _ in range(H+2)]
    dh = [0,1,1,1,0,-1,-1,-1]
    dw = [1,1,0,-1,-1,-1,0,1]

    cnt = 0
    for h in range(1,H+2):
        for w in range(1,W+2):
            if visited[h][w]==False and S[h][w]=='1':
                visited[h][w]=True
                dfs(h,w,visited,S)
                cnt += 1
    ans.append(cnt)
print(*ans, sep='\n')

Python: import sys
sys.setrecursionlimit(10 ** 6)

def dfs(here_w, here_h):
    if 0 <= here_w < width and 0 <= here_h < height:
        if target[here_h][here_w]:
            target[here_h][here_w] = 0
            for x, y in all_move:
                dfs(here_w + x, here_h + y)
                
all_move = [[1, 1], [1, 0], [1, -1], [0, 1], [0, -1], [-1, 1], [-1, 0], [-1, -1]]
while True:
    width, height = map(int, input().split(" "))
    if not width:
        break
    target = []
    for h in range(height):
        temp = [int(n) for n in input().split(" ")]
        target.append(temp)
    # print(width, height, temp)
    answer = 0
    for h in range(height):
        for w in range(width):
            if target[h][w]:
                answer += 1
                dfs(w, h)
    print(answer)