Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

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

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

Python: # python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve(w, h):
    mat = [[0]*(w+2) for _ in range(h+2)]
    for j in range(h):
        l = list(map(int, input().split()))
        for i in range(w):
            mat[j+1][i+1] = l[i]
    visited = [[False]*(w+2) for _ in range(h+2)]

    ans = 0
    for i in range(h+2):
        for j in range(w+2):
            if visited[i][j]:
                continue
            else:
                if mat[i][j] == 1:
                    visited[i][j] = True
                    ans += 1
                    Q = [[i, j]]
                    ran = [1, 0, -1]
                    while Q:
                        x, y = Q.pop(-1)
                        for dx in ran:
                            for dy in ran:
                                if mat[x+dx][y+dy] == 1 and not visited[x+dx][y+dy]:
                                    visited[x+dx][y+dy] = True
                                    Q.append([x+dx, y+dy])
    return ans


a = []
while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    else:
        a.append(solve(w, h))
print("\n".join(map(str, a)))

Python: if __name__ == '__main__':
    while 1:
        w,h = list(map(int,input().strip().split()))
        if w == h ==0:break
        imap = [list(map(int,input().strip().split())) for _ in range(h)]
        visited = []
        count = 0
        for j in range(h):
            for i in range(w):
                stack =[]
                if not (j,i) in visited and imap[j][i] == 1:
                    count +=1
                    stack.append((j,i))
                    visited.append(((j,i)))
                while len(stack) !=0:
                    y,x = stack.pop()
                    if y>0 and imap[y-1][x] == 1 and not (y-1,x) in visited:
                        visited.append((y-1,x))
                        stack.append((y-1,x))
                    if x>0 and imap[y][x-1] == 1 and not (y,x-1) in visited:
                        visited.append(((y,x-1)))
                        stack.append((y,x-1))
                    if y>0 and x>0 and imap[y-1][x-1] == 1 and not (y-1,x-1) in visited:
                        visited.append((y-1,x-1))
                        stack.append((y-1,x-1))
                    if y>0 and x<w-1 and imap[y-1][x+1] ==1 and not (y-1, x+1) in visited:
                        visited.append((y-1, x+1))
                        stack.append((y-1, x+1))
                    if y <h-1 and x>0 and imap[y+1][x-1] ==1 and not (y+1,x-1) in visited:
                        visited.append((y+1,x-1))
                        stack.append((y+1,x-1))
                    if y < h-1 and x < w-1 and imap[y+1][x+1] ==1and not (y+1,x+1) in visited:
                        visited.append((y+1,x+1))
                        stack.append((y+1,x+1))
                    if y < h-1 and imap[y+1][x]== 1 and not (y+1,x) in visited:
                        visited.append((y+1,x))
                        stack.append((y+1,x))
                    if x < w-1 and imap[y][x+1] == 1 and not (y,x+1) in visited:
                        visited.append((y,x+1))
                        stack.append((y,x+1))
        print(count)

Python: # -*- coding:utf-8 -*-
def solve():
    import sys
    sys.setrecursionlimit(100*100)

    anss = []
    while True:
        W, H = list(map(int, input().split()))

        if W==0 and H==0:
            break

        grid = [[] for _ in range(H)]
        for h in range(H):
            Cs = list(map(int, input().split()))
            grid[h] = Cs

        visited = [[0 for _ in range(W)] for __ in range(H)]  # 探索済みか？

        def dfs(h, w, depth):
            if w<0 or h<0 or w>=W or h>=H:
                return 0

            if visited[h][w] == 1:
                return 0
            visited[h][w] = 1

            if grid[h][w] == 0:
                return 0

            ret = 0
            for hh in range(h-1, h+2):
                for ww in range(w-1, w+2):
                    ret += dfs(hh, ww, depth+1)

            return ret+depth

        ans = 0
        for h in range(H):
            for w in range(W):
                if dfs(h, w, grid[h][w]) != 0:
                    ans += 1

        anss.append(ans)

    for ans in anss:
        print(ans)


if __name__ == "__main__":
    solve()

Python: def dfs(island, r, c):
    stack = [(r, c)]
    ans = 0
    while stack:
        r, c = stack.pop()
        if r < 0 or r >= len(island) or c < 0 or c >= len(island[r]):
            continue
        if island[r][c] == 0:
            continue
        ans += 1
        island[r][c] = 0
        for rr in range(-1, 2):
            for cc in range(-1, 2):
                if rr == 0 and cc == 0:
                    continue
                stack.append((r+rr, c+cc))
    return ans


def solve(w, h):
    island = []
    for _ in range(h):
        island.append(list(map(int, input().split())))
    cnt = 0
    for r in range(h):
        for c in range(w):
           if dfs(island, r, c) > 0:
               cnt += 1
    print(cnt)

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