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

Python: import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    W = W + 2
    H = H + 2
    A = [0] * H
    for i in range(H):
        if i == 0 or i == H - 1:
            A[i] = ['#'] * W
        else:
            A[i] = ['#'] + list(map(int, input().split())) + ['#']
    checked = [[False] * W for _ in range(H)]

    def dfs(s):
        dirs = [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]
        x, y = s
        if checked[x][y]:
            return False
        if A[x][y] == 0:
            return False

        checked[x][y] = True
        for d in dirs:
            dx, dy = d
            if A[x + dx][y + dy] == '#':
                continue
            if checked[x + dx][y + dy]:
                continue
            if A[x + dx][y + dy] == 1:
                dfs((x + dx, y + dy))
        return True

    ans = 0
    for i in range(1, H - 1):
        for j in range(1, W - 1):
            if dfs((i, j)):
                ans += 1
    print(ans)