Go: // AOJ 1160: How Many Islands? http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var SEA = 0
var LAND = 1

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func discoverIslands(M, V [][]int, H, W int) int {
	// 島を探す
	queue := list.New()
	islands := 0
	for i := 1; i <= H; i++ {
		for j := 1; j <= W; j++ {
			// 訪問済みの土地ならスキップ
			if V[i][j] == 1 {
				continue
			}

			// 訪問中の土地が海なら訪問済みにしてスキップ
			if M[i][j] == SEA {
				V[i][j] = 1
				continue
			}

			// 未訪問の陸地が見つかったので
			// 島の数を1つ増やし、この陸地から辿れる陸地を訪問していく
			islands++
			queue.PushBack([]int{i, j})

			for queue.Len() > 0 {
				cur := queue.Remove(queue.Front()).([]int)
				h, w := cur[0], cur[1]
				if V[h][w] == 1 {
					continue
				}
				V[h][w] = 1

				// 全方向に未訪問の陸地がないか探索
				for v := -1; v <= 1; v++ {
					for l := -1; l <= 1; l++ {
						dh := h + v
						dw := w + l
						if M[dh][dw] == LAND && V[dh][dw] == 0 {
							queue.PushBack([]int{dh, dw})
						}
					}
				}
				/*
					if M[h-1][w] == LAND {
						queue.PushBack([]int{h - 1, w})
					}
					if M[h-1][w+1] == LAND {
						queue.PushBack([]int{h - 1, w + 1})
					}
					if M[h][w+1] == LAND {
						queue.PushBack([]int{h, w + 1})
					}
					if M[h+1][w+1] == LAND {
						queue.PushBack([]int{h + 1, w + 1})
					}
					if M[h+1][w] == LAND {
						queue.PushBack([]int{h + 1, w})
					}
					if M[h+1][w-1] == LAND {
						queue.PushBack([]int{h + 1, w - 1})
					}
					if M[h][w-1] == LAND {
						queue.PushBack([]int{h, w - 1})
					}
					if M[h-1][w-1] == LAND {
						queue.PushBack([]int{h - 1, w - 1})
					}
				*/
			}
		}
	}
	return islands
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	for {
		W, H := nextInt(sc), nextInt(sc)

		if W == 0 && H == 0 {
			break
		}
		// Initialize map and visted list
		M := make([][]int, H+2)
		V := make([][]int, H+2)
		for i := 0; i < H+2; i++ {
			M[i] = make([]int, W+2)
			V[i] = make([]int, W+2)
		}
		for i := 1; i <= H; i++ {
			for j := 1; j <= W; j++ {
				M[i][j] = nextInt(sc)
			}
		}

		islands := discoverIslands(M, V, H, W)

		fmt.Println(islands)
	}
}

Python: import sys
sys.setrecursionlimit(10**7)

def dfs(row, column):
    Map[row][column] = num
    for i in range(-1, 2):
        for j in range(-1, 2):
            if Map[row + i][column + j] == 1:
                dfs(row + i, column + j)

while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    
    Map = [[0] * (w+2)]
    Map += [[0] + list(map(int, input().split())) + [0] for _ in range(h)]
    Map += [[0] * (w+2)]
    
    num = 1
    
    for i in range(h+2):
        for j in range(w+2):
            if Map[i][j] == 1:
                num += 1
                dfs(i,j)
    print(num-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 sys
sys.setrecursionlimit(10 ** 7)

direction = ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (-1, 1), (1, -1))


def dfs(y, x):
    board[y][x] = 0

    for d in direction:
        nh = y + d[1]
        nw = x + d[0]

        if nh < 0 or nh >= h or nw < 0 or nw >= w:
            continue
        if board[nh][nw] == 0:
            continue
        dfs(nh, nw)

while True:
    w, h = map(int, input().split())
    if h == 0 and w == 0:
        break
    board = [list(map(int, input().split())) for i in range(h)]

    cnt = 0
    for y in range(h):
        for x in range(w):
            if board[y][x] == 0:
                continue
            dfs(y, x)
            cnt += 1
    print(cnt)

Python: def dfs(c, x, y, w, h):
    c[y][x] = -1

    for i in range(-1, 2):
        for j in range(-1, 2):
            if i == 0 and j == 0:
                continue
            x_ = x + i
            y_ = y + j
            if 0 <= x_ < w and 0 <= y_ < h and c[y_][x_] == 1:
                dfs(c, x_, y_, w, h)

def solve(w, h):
    c = []
    ans = 0

    for _ in range(h):
        c.append(list(map(int, input().split())))

    for y in range(h):
        for x in range(w):
            if c[y][x] == 1:
                ans += 1
                dfs(c, x, y, w, h)

    print(ans)

if __name__ == "__main__":
    import sys
    sys.setrecursionlimit(10000)
    while True:
        w, h = map(int, input().split())
        if w * h == 0:
            break
        solve(w, h)

Python: import sys
sys.setrecursionlimit(10**6)

def erase(x, y):
    c[y][x] = '0'
    for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
        nx, ny = x + dx, y + dy
        if 0 <= nx <= w -1 and 0 <= ny <= h - 1:
            if c[ny][nx] == '1':
                erase(nx, ny)

while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [[i for i in input().split()] for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == '1':
                ans += 1
                erase(x, y)
    print(ans)

Python: def root(i):
    global roots
    if roots[i]==i:
        return i
    else:
        roots[i]=root(roots[i])
        return roots[i]

def unite(i, j):
    global roots
    ri=root(i)
    rj=root(j)
    #roots[max(ri, rj)]=min(ri, rj)
    if ri==rj: return
    roots[ri]=rj

directions=[(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]
while True:
    l=input().split()
    w=int(l[0])
    h=int(l[1])
    if w==0 and h==0: break
    map=[[False for _ in range(h+2)] for _ in range(w+2)]
    roots=[i for i in range((w+2)*(h+2))]
    for hi in range(h):
        l=input().split()
        for wi in range(w):
            map[wi+1][hi+1]=bool(int(l[wi]))

    for hi in range(1, h+1):
        for wi in range(1, w+1):
            if not map[wi][hi]: continue
            i=hi*(w+2)+wi
            for d in directions:
                if not map[wi+d[0]][hi+d[1]]: continue
                j=(hi+d[1])*(w+2)+(wi+d[0])
                unite(i, j)
    res=set()
    for hi in range(1, h+1):
        for wi in range(1, w+1):
            if not map[wi][hi]: continue
            i=hi*(w+2)+wi
            res.add(root(i))
    print(len(res))

Python: # -*- coding: utf-8 -*-
def dfs(x,y):
    c[x][y] = 0
    for dx in [-1,0,1]:
        for dy in [-1,0,1]:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
                dfs(nx, ny)
 
while True:
    w, h = map(int,input().split())
    if w == 0 and h == 0 :
        break
 
    c = [list(map(int,input().split())) for j in range(h)]
    count = 0
 
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1:
                dfs(i,j)
                count += 1
 
    print(count)