Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

Python: import sys
sys.setrecursionlimit(100000)

def dfs(i, j):
    g[i][j] = 0

    for x in range(-1, 2):
        for y in range(-1, 2):
            dx = x + i
            dy = y + j

            if 0<=dx<h and 0<=dy<w and g[dx][dy] == 1:
                dfs(dx, dy)
    return

while True:
    w, h = map(int, input().split())
    if not (w or h):
        break

    g = [list(map(int, input().split())) for _ in range(h)]

    ans = 0
    for i in range(h):
        for j in range(w):
            if g[i][j] == 1:
                dfs(i, j)
                ans += 1

    print(ans)

Python: m = []

def walk(i,j):
    stack = []
    stack.append((i,j))
    while stack:
        p = stack.pop()
        pi = p[0]
        pj = p[1]
        m[pi][pj] = 0
        for k in range(-1,2):
            for l in range(-1,2):
                a = pi+k
                b = pj+l
                if a < 0 or b < 0 \
                        or a >= len(m) or b >= len(m[a]):
                            pass
                elif m[a][b] == 0:
                    pass
                else:
                    stack.append((a,b))

while True:
    w, h = map(int, input().split())
    if w == 0: break
    for i in range(h):
        m.append(list(map(int, input().split())))
    ans = 0
    for i in range(len(m)):
        for j in range(len(m[i])):
            if m[i][j] == 1:
                walk(i,j)
                ans += 1
    print(ans)
    m = []

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