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