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: from collections import deque
import sys
input = sys.stdin.readline

w = []
h = []
dataset = []
while True:
    wt, ht = map(int, input().split())
    if wt==0 and ht==0:
        break
    w.append(wt)
    h.append(ht)
    subset = [[0]*(wt+2) for _ in range(ht+2)]
    for i in range(1, ht + 1):
        inls = [0] + list(map(int, input().split())) + [0]
        for j in range(1, wt + 1):
            subset[i][j] = inls[j]
    dataset.append(subset)

for i in range(len(dataset)):
    subset = dataset[i]
    w_ = w[i]
    h_ = h[i]
    idxs = []
    for row in range(1, h_+1):
        for col in range(1, w_+1):
            if subset[row][col] == 1:
                idxs.append((row, col))
    
    graph = {idx: deque() for idx in idxs}
    for idx in idxs:
        x = idx[0]
        y = idx[1]
        surroundings = [(x+1, y), (x+1, y+1), (x, y+1), (x-1, y+1)
                         ,(x-1, y), (x-1, y-1), (x, y-1), (x+1, y-1)]
        for surrounding in surroundings:
            if subset[surrounding[0]][surrounding[1]] == 1:
                graph[idx].append(surrounding)
    
    seen = [[0]*(w_+2) for _ in range(h_+2)]
    stack = []
    def dfs(idx):
        seen[idx[0]][idx[1]] = 1
        stack.append(idx)
        while stack:
            s = stack[-1]
            if not graph[s]:
                stack.pop()
                continue
            
            g = graph[s].popleft()
            if seen[g[0]][g[1]]:
                continue
            seen[g[0]][g[1]] = 1
            stack.append(g)

    island_count = 0
    for idx in idxs:
        if not seen[idx[0]][idx[1]]:
            dfs(idx)
            island_count += 1
    print(island_count)

Python: import sys

sys.setrecursionlimit(3000)

while True:

    w, h = map(int, input().split())

    if w == 0 and h == 0:
        exit() 

    C = []
    for i in range(h):
        c = list(map(int, input().split()))
        C.append(c)

    def is_island(i, j):
        if 0 <= i < h and 0 <= j < w and C[i][j] == 1:
            C[i][j] = 0
            return True
        else:
            return False

    def walk_to(i, j):
        if is_island(i - 1, j):
            walk_to(i - 1, j)
        if is_island(i - 1, j - 1):
            walk_to(i - 1, j - 1)
        if is_island(i, j - 1):
            walk_to(i, j - 1)
        if is_island(i + 1, j - 1):
            walk_to(i + 1, j - 1)
        if is_island(i + 1, j):
            walk_to(i + 1, j)
        if is_island(i + 1, j + 1):
            walk_to(i + 1, j + 1)
        if is_island(i, j + 1):
            walk_to(i, j + 1)
        if is_island(i - 1, j + 1):
            walk_to(i - 1, j + 1)

    count = 0        
    for i in range(h):
        for j in range(w):
            if is_island(i, j):
                count = count + 1
                walk_to(i, j)

    print(count)

Python: import sys

sys.setrecursionlimit(10000)


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


def dfs(field, w, h, height, width):
    field[height][width] = 0

    for i in range(8):
        nw = width + dx[i]
        nh = height + dy[i]

        if nw < 0 or nw >= w or nh < 0 or nh >= h:
            continue
        if field[nh][nw] == 0:
            continue

        dfs(field, w, h, nh, nw)
    return


def solve():
    ans = []
    while True:
        w, h = map(int, input().split())
        if w == h == 0:
            break

        field = [list(map(int, input().split())) for _ in range(h)]
        count = 0
        for c in range(h):
            for r in range(w):
                if field[c][r] == 1:
                    count += 1
                    dfs(field, w, h, c, r)

        ans.append(count)
    print(*ans, sep="\n")


if __name__ == "__main__":
    solve()

Python: import sys
sys.setrecursionlimit(10**7)
def dfs_rec():
    W,H = list(map(int,input().split()))
    if W == 0 and H == 0:
        exit()
    C = [list(map(int,input().split())) for _ in range(H)]
    D = [[0,1],[0,-1],[1,0],[1,1],[1,-1],[-1,0],[-1,1],[-1,-1]]
    
    def dfs(C, x, y):
        C[y][x] = 2
        for d in D:
            if x+d[0] < 0 or x+d[0] > W-1 or y+d[1] < 0 or y+d[1] > H-1:
                continue
            if C[y+d[1]][x+d[0]] == 0 or C[y+d[1]][x+d[0]] == 2:
                continue
            dfs(C, x+d[0], y+d[1])
    
    count = 0
    for w in range(W):
        for h in range(H):
            if C[h][w] == 0 or C[h][w] == 2:
                continue
            dfs(C, w, h)
            count += 1
    
    print(count)

while True:
    dfs_rec()