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: def dfs(w,h,W,H,ID,islands,islandsID):
    if islands[h][w]==1 and islandsID[h][w]!=0:
        for i in range(-1,2):
            for j in range(-1,2):
                dw=w+i
                dh=h+j
                if dw>=0 and dw<W and dh>=0 and dh<H:
                    if islands[dh][dw]==1:
                        islandsID[dh][dw]=islandsID[h][w]
    if islands[h][w]==1 and islandsID[h][w]==0:
        for i in range(-1,2):
            for j in range(-1,2):
                dw=w+i
                dh=h+j
                if dw>=0 and dw<W and dh>=0 and dh<H:
                    if islands[dh][dw]==1:
                        islandsID[dh][dw]=ID
        ID+=1
    return ID

while True:
    islands=[]
    islandsID=[]
    ID=1
    W,H=map(int,input().split())
    if W==0:
        break
    for i in range(H):
        islandsID.append([0 for j in range(W)])
    for _ in range(H):
        islands.append(list(map(int,input().split())))
    for h in range(H):
        for w in range(W):
            ID=dfs(w,h,W,H,ID,islands,islandsID)
    print(ID-1)

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

def search_island(h, w, max_h, max_w):
    for i in range(8):
        next_h = h + [1, 1, 0, -1, -1, -1, 0, 1][i]
        next_w = w + [0, 1, 1, 1, 0, -1, -1, -1][i]
        if not 0 <= next_h < max_h or not 0 <= next_w < max_w:
            continue
        elif step[next_h][next_w] or not field[next_h][next_w]:
            continue
        step[next_h][next_w] = True
        search_island(next_h, next_w, max_h, max_w)

while True:
    max_w, max_h = map(int, input().split())
    if max_w == max_h == 0:
        break
    field = [[int(i) for i in input().split()] for _ in range(max_h)]
    step = [[False for _ in range(max_w)] for _ in range(max_h)]
    island = 0
    for i in range(max_h):
        for j in range(max_w):
            if step[i][j] or not field[i][j]:
                continue
            search_island(i, j, max_h, max_w)
            island += 1
    print(island)

Python: import sys

sys.setrecursionlimit(1000000)


def rec(y, x, f):
    f[y][x] = '0'
    for dy, dx in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0),
            (1, 1)]:
        if 0 <= y+dy < len(f) and 0 <= x+dx < len(f[0]) and f[y+dy][x+dx] == '1':
            rec(y+dy, x+dx, f)


def main():
    while True:
        w, h = [int(i) for i in input().split()]
        if w == 0 and h == 0:
            break
        field = []
        for i in range(h):
            field.append(input().split())
        ans = 0
        for i in range(h):
            for j in range(w):
                if field[i][j] == '1':
                    rec(i, j, field)
                    ans += 1
        print(ans)


if __name__ == '__main__':
    main()

Python: import sys
sys.setrecursionlimit(4100000)


def dfs(cy, cx):
    # 一回走らせたら一つの島が消滅すると考える
    if not (cy < 0 or cy >= H or cx < 0 or cx >= W or gridgraph[cy][cx] == "0"):
        gridgraph[cy][cx] = "0"
        for dy, dx in adjacent:
            ny, nx = cy+dy, cx+dx
            dfs(ny, nx)


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

Python: while 1:
	W, H = map(int, input().split())
	field = [input().split() for i in range(H)]
	if W == 0 and H == 0:
		break
	
	searched = [[0]*W for i in range(H)]
	stack = []
	count = 0
	for y in range(H):
		for x in range(W):
			if searched[y][x] == 0:
				searched[y][x] = 1
				if field[y][x] == '1':
					stack += [[x, y]]
					count += 1
					while (stack):
						sx, sy = stack.pop()
						for m in range(8):
							ny = sy + [0,1,1,1,0,-1,-1,-1][m]
							nx = sx + [1,1,0,-1,-1,-1,0,1][m]
							if (ny >= 0 and ny < H) and (nx >= 0 and nx < W):
								if (searched[ny][nx] == 0) and (field[ny][nx] == '1'):
									searched[ny][nx] = 1
									stack += [[nx, ny]]
	else:
		print(count)

Python: def dfs(w,h,W,H,ID,islands,islandsID):
    if islands[h][w]==1 and islandsID[h][w]!=0:
        for i in range(-1,2):
            for j in range(-1,2):
                dw=w+i
                dh=h+j
                if dw>=0 and dw<W and dh>=0 and dh<H:
                    if islands[dh][dw]==1:
                        islandsID[dh][dw]=islandsID[h][w]
    if islands[h][w]==1 and islandsID[h][w]==0:
        for i in range(-1,2):
            for j in range(-1,2):
                dw=w+i
                dh=h+j
                if dw>=0 and dw<W and dh>=0 and dh<H:
                    if islands[dh][dw]==1:
                        islandsID[dh][dw]=ID
        ID+=1
    return ID

while True:
    islands=[]
    islandsID=[]
    ID=1
    W,H=map(int,input().split())
    if W==0:
        break
    for i in range(H):
        islandsID.append([0 for j in range(W)])
    for _ in range(H):
        islands.append(list(map(int,input().split())))
    for h in range(H):
        for w in range(W):
            ID=dfs(w,h,W,H,ID,islands,islandsID)
    print(ID-1)