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(10000)
def bom(i,j):
    for s in range(-1,2):
        for d in range(-1,2):
            if i+s==-1 or i+s==h or j+d==-1 or j+d==w:
                continue
            if l[i+s][j+d]==1:
                l[i+s][j+d]=0
                bom_2(i+s,j+d)
                return 1
    return 0
def bom_2(i,j):
    for s in range(-1,2):
        for d in range(-1,2):
            if i+s==-1 or i+s==h or j+d==-1 or j+d==w:
                continue
            if l[i+s][j+d]==1:
                l[i+s][j+d]=0
                bom_2(i+s,j+d)                
while True:
    w,h=map(int,input().split())
    if w==0:break
    l=[list(map(int,input().split())) for _ in range(h)]
    ans=0
    for i in range(h):
        for j in range(w):
            if l[i][j]==1:
                ans+=bom(i,j)
    print(ans)

Python: import sys
sys.setrecursionlimit(2500)
 
def erase(x, y):
    c[y][x] = '0'
    for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
        nx, ny = x + dx, y + dy
        if 0 <= nx < w and 0 <= ny < h and c[ny][nx] == '1':
            erase(nx, ny)
 
while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [input().split() for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == '1':
                ans += 1
                erase(x, y)
    print(ans)

Python: import sys
sys.setrecursionlimit(10**8) #再帰関数の上限を変更
ans = []
def dfs(x, y):
    c[x][y] = 0 #今いる場所を0にする
    for dx in range(-1, 2):
        for dy in range(-1, 2):
            nx = x + dx
            ny = y + dy
            ##動く先が範囲内で、1であれば移動する
            if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
                dfs(nx, ny)
while True:
    w, h = map(int, input().split())
    if w == 0 and h ==0:
        break
    count = 0
    c = [[] for _ in range(h)]
    for i in range(h):
        if w == 1:
            c[i] = [int(input())]
        else:
            c[i] = list(map(int, input().split()))
            #c = [[1, 0, 1, 0, 0], [1, 0, 0, 0, 0], [1, 0, 1, 0, 1], [1, 0, 0, 1, 0]]
    for x in range(h):
        for y in range(w):
            if c[x][y] == 1:
                dfs(x, y)
                count += 1
    ans.append(count)
for i in ans:
    print(i)

Python: while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    cnt = 0
    maps = [list(map(int, input().split())) for _ in range(h)]
    for x in range(w):
        for y in range(h):
            if maps[y][x] == 0:
                continue
            else:
                cnt += 1
                maps[y][x] = 0
                stack = [(y, x)]
                while stack:
                    f_y, f_x = stack.pop()
                    for dy in range(-1, 2):
                        for dx in range(-1, 2):
                            t_y = f_y + dy
                            t_x = f_x + dx
                            if 0 <= t_y <= h - 1 and 0 <= t_x <= w - 1 and maps[t_y][t_x] == 1:
                                maps[t_y][t_x] = 0
                                stack.append((t_y, t_x))
    print(cnt)

Python: class UnionSet:
        def __init__(self, nmax):
            self.size = [1]*nmax
            self.id = [i for i in range(nmax+1)]
        def root(self, i):
            while i != self.id[i]:
                self.id[i] = self.id[self.id[i]]
                i = self.id[i]
            return i
        def connected(self, p, q): return self.root(p) == self.root(q)
        def unite(self, p, q):
            i, j = self.root(p), self.root(q)
            if i == j: return
            if self.size[i] < self.size[j]:
                self.id[i] = j
                self.size[j] += self.size[i]
            else:
                self.id[j] = i
                self.size[i] += self.size[j]
    # UNION-FIND library
     
def id(r, c):
        global w
        return r*w+c

mv = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]

while True:
        w, h = map(int, input().split())
        if w == 0: break
        arr = [[] for r in range(h)]
        u = UnionSet(w*h)
        for r in range(h): arr[r] = list(map(int, input().split()))
        for r in range(h):
                for c in range(w):
                        if not arr[r][c]: continue
                        for i in range(8):
                                r2, c2 = r + mv[i][0], c + mv[i][1]
                                if r2 < 0 or r2 >= h or c2 < 0 or c2 >= w or not arr[r2][c2]: continue
                                u.unite(id(r, c), id(r2, c2))
        ans = 0
        for r in range(h):
                for c in range(w):
                        if arr[r][c] and u.root(id(r, c)) == id(r, c): ans += 1
        print(ans)