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(x,y):
    color[x][y]="gray"
    for i,j in [[-1,1],[-1,0],[-1,-1],[0,1],[0,-1],[1,-1],[1,0],[1,1]]:
        X,Y=x+i,y+j
        if 0<=X<h and 0<=Y<w:
            if C[X][Y]==1 and color[X][Y]=="white":
                DFS(x+i,y+j)
    color[x][y]="black"


while 1:
    w,h=map(int,input().split())
    if w==0 and h==0:break
    C=[list(map(int,input().split())) for _ in range(h)]
    color=[["white" for _ in range(w)] for _ in range(h)]
    cnt=0
    for i in range(h):
        for j in range(w):
            if color[i][j]=="white" and C[i][j]==1:
                DFS(i,j)
                cnt +=1
    print(cnt)

Python: import sys
from collections import deque

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')

def land_check(y,x,W):
    tansaku = deque()
    tansaku.append([y,x])
    che_land = 0
    while tansaku:
        l_y,l_x = tansaku.pop()
        if l_x in visited_land[l_y]:
            continue
        for i in range(l_x-1,l_x+2):
                for j in range(l_y-1,l_y+2):
                    if 0 <= i <= w-1 and 0 <= j <= h-1  and W[j][i] == 1:
                        tansaku.append([j,i])
        visited_land[l_y].add(l_x)
        che_land += 1
    return che_land


def answer(h,w):

    res = 0
    ans = 0
    for i in range(h):
        for j in range(w):
            if tizu[i][j] == 0 or j in visited_land[i]:
                continue
            res += land_check(i,j,tizu)
            ans += 1
            if res == land_count:
                break
        else:
            continue
        break
    return ans

if __name__ == '__main__':
    kotae = []
    while True:
        w,h = nm()
        if [h,w] == [0,0]:
            break
        visited_land = {i:set() for i in range(h)}
        tizu = []
        land_count = 0
        for i in range(h):
            temp = nl()
            land_count += temp.count(1)
            tizu.append(temp)
        kotae.append(answer(h,w))
    [print(i) for i in kotae]

Python: def solve(H, W, C):
	cnt = 0
	d = [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]

	while True:
		start = [-1,-1]
		for i in range(H):
			for j in range(W):
				if C[i][j] == 1:
					start = [i,j]
					break

		if start == [-1,-1]:
			return cnt
		else:
			cnt += 1
			q = [start]


		while q:
			cx,cy = q.pop()
			C[cx][cy] = 0
			for dx,dy in d:
				nx,ny = cx+dx, cy+dy
				if 0 <= nx < H and 0 <= ny < W:
					if C[nx][ny] == 1:
						q.append([nx,ny])


ans = []
while True:
	W,H = map(int, input().split())
	if H == 0 and W == 0:
		break
	C = [list(map(int, input().split())) for _ in range(H)]
	#print(H,W,C)
	ans.append(solve(H,W,C))




for a in ans:
	print(a)

Python: def dfs(field, i, j, w, h):
    field[i][j] = "0"
    
    OFFSETS = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
    for di, dj in OFFSETS:
        if i+di < 0 or i+di >= h:
            continue
        if j+dj < 0 or j+dj >= w:
            continue
        if field[i+di][j+dj] == "1":
            dfs(field, i+di, j+dj, w, h)


def main():
    while True:
        w, h = [int(s) for s in input().strip().split()]

        if w == 0 and h == 0:
            break

        num_island = 0
    
        field = []
        for _ in range(h):
            field.append(input().strip().split())
       
        #print(field) 
        for i in range(h):
            for j in range(w):
                if field[i][j] == "1":
                    num_island += 1
                    dfs(field, i, j, w, h)
        
        print(num_island)


if __name__ == "__main__":
    main()

Python: from collections import deque
while True:
    W,H = map(int,input().split())
    if W == 0: break
    src = [list(map(int,input().split())) for i in range(H)]
    visited = [[0 for x in range(W)] for y in range(H)]
    dxy = [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]
    ans = 0
    for y in range(H):
        for x in range(W):
            if visited[y][x] or not(src[y][x]): continue
            ans += 1
            visited[y][x] = 1
            q = deque([(x,y)])
            while q:
                x0,y0 = q.popleft()
                for dx,dy in dxy:
                    nx,ny = x0+dx, y0+dy
                    if not(0 <= nx < W) or not(0 <= ny < H): continue
                    if visited[ny][nx] or not(src[ny][nx]): continue
                    visited[ny][nx] = 1
                    q.append((nx,ny))
    print(ans)

Python: import sys
sys.setrecursionlimit(10000)
def f(x,y):
    global w,h,c
    if x<0 or x>=h or y<0 or y>=w or c[x][y]==0:
        return
    c[x][y]=0
    for a in [-1,0,1]:
        for b in [-1,0,1]:
            if a==0 and b==0:
                continue
            f(x+a,y+b)
while True:
    w,h = map(int,input().split())
    if w+h==0:
        break
    c = [list(map(int,input().split())) for _ in range(h)]
    ans = 0
    for x in range(h):
        for y in range(w):
            if c[x][y]==1:
                ans+=1
                f(x,y)
    print(ans)