Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

Python: # -*- coding: utf-8 -*-
"""
How Many Islands?
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline


def bfs(data, sx, sy, w, h):
    dy = [-1, 1, 0, 0, -1, -1, 1, 1]
    dx = [0, 0, -1, 1, -1, 1, -1, 1]
    Q = deque()
    Q.append((sx, sy))
    data[sy][sx] = '-'
    while Q:
        cx, cy = Q.popleft()        #  現在地の座標
        for i in range(8):
            nx = cx + dx[i]
            ny = cy + dy[i]
            if (not 0 <= nx < w) or (not 0 <= ny < h):
                continue
            elif data[ny][nx] == '1':
                Q.append((nx, ny))
                data[ny][nx] = '-'


def solve(field, w, h):
    ans = 0
    for y in range(h):
        for x in range(w):
            if field[y][x] == '1':
                bfs(field, x, y, w , h)
                ans += 1
    return ans


def main(args):
    while True:
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break
        field = []
        for _ in range(h):
            field.append([x for x in input().split()])
        ans = solve(field, w, h)
        print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

def dfs(s,Map,H,W,c):
    dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
    for i in range(len(dxdy)):
        Map[s[0]][s[1]] = c
        nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
        if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
            if Map[nextPos[0]][nextPos[1]] == 1:
                dfs(nextPos,Map,H,W,c)

while True:
    W,H = map(int,input().split(" "))
    if W == 0 and H == 0:
        break
    Map = [list(map(int,input().split())) for i in range(0,H)]
    c = 1
    for i in range(H):
        for j in range(W):
            if Map[i][j] == 1:
                c += 1
                dfs((i,j),Map,H,W,c)
    print(c-1)

Python: import queue
def main():
	count_list = []

	w,h = map(int,input().split())
	while not(w == 0 and h == 0):
		data = []
		count = 0
		for _ in range(h):
			data.append(list(map(int,input().split())))

		for row,i in zip(data,range(len(data))):
			for cell,j in zip(row,range(len(row))):
				if cell == 1:
					fill_sea(data,j,i,w,h)
					count += 1

		w,h = map(int,input().split())
		count_list.append(count)




	for i in count_list:
		print(i)


def fill_sea(data,x,y,w,h):
	q = queue.LifoQueue()
	q.put((x,y))
	while not q.empty():
		current_cell = q.get()
		data[current_cell[1]][current_cell[0]] = 0


		for i in [-1,0,1]:
			if current_cell[1]+i <= -1 or  current_cell[1]+i >= h:
				continue
			for j in [-1,0,1]:
				if i == 0 and j == 0:
					continue
				if current_cell[0]+j <= -1 or current_cell[0]+j >= w :
					continue

				if data[current_cell[1]+i][current_cell[0]+j] == 1:
					q.put((current_cell[0]+j,current_cell[1]+i))


if __name__ == '__main__':
	main()

Python: #!python
# -*- coding: utf-8 -*-

import sys

import numpy as np

sys.setswitchinterval(10**8)


next_s = ((-1, 1), (0, 1), (1, 1),
          (-1, 0), (1, 0),
          (-1, -1), (0, -1), (1, -1))


def DFS(x, y):
    if not (0 <= x < W and 0 <= y < H):
        return
    if not map_[y * W + x]:
        return

    map_[y * W + x] = 0

    for dx, dy in next_s:
        DFS(x + dx, y + dy)


while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break

    map_ = []
    for h in range(H):
        map_ += list(map(int, input().split()))
    # print(map_)
    island_num = 0
    while any(map_):
        next_idx = map_.index(1)
        x = next_idx % W
        y = next_idx // W
        # print(map_)
        # print("x: {} | y: {}".format(x, y))
        DFS(x, y)
        island_num += 1

    print(island_num)

Python: def dfs(startx, starty):
    dxdy = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]
    stack = [(startx, starty)]
    reached = set()
    while stack:
        d = stack.pop()
        reached.add(d)
        for a in dxdy:
            x = d[0] + a[0]
            y = d[1] + a[1]
            if (x, y) not in reached and 0 <= x < W and 0 <= y < H and maze[y][x] != '0':
                stack.append((x, y))
    return reached


while True:
    W, H = map(int, input().split())
    if H == W == 0:
        break
    maze = [input().split()for _ in range(H)]
    ans = 0
    l = set()
    for i in range(H):
        for j in range(W):
            if (j, i) not in l and maze[i][j] == '1':
                l = l | dfs(j, i)
                ans += 1
    print(ans)

Python: import sys
sys.setrecursionlimit(10000000)


while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    G = [list(map(int, input().split())) for i in range(H)]
    dh = (0, -1, -1, -1, 0, 1, 1, 1)
    dw = (-1, -1, 0, 1, 1, 1, 0, -1)

    def dfs(h, w):
        for i in range(8):
            nh = h + dh[i]
            nw = w + dw[i]
            if nh < 0 or nh >= H or nw < 0 or nw >= W:
                continue
            if not G[nh][nw]:
                continue
            G[nh][nw] = 0
            dfs(nh, nw)
    
    cnt = 0
    for i in range(H):
        for j in range(W):
            if G[i][j]:
                dfs(i, j)
                cnt += 1
    print(cnt)