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: class UnionSet:
    def __init__(self, nmax):
        self.size = [1]*nmax
        self.id = [i for i in range(nmax+1)]
    def root(self, i):
        while i != self.id[i]:
            self.id[i] = self.id[self.id[i]]
            i = self.id[i]
        return i
    def connected(self, p, q): return self.root(p) == self.root(q)
    def unite(self, p, q):
        i, j = self.root(p), self.root(q)
        if i == j: return
        if self.size[i] < self.size[j]:
            self.id[i] = j
            self.size[j] += self.size[i]
        else:
            self.id[j] = i
            self.size[i] += self.size[j]
# UNION-FIND library
 
def id(r, c):
    global w
    return r*w+c
 
mv = [[-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: break
    arr = [[] for r in range(h)]
    u = UnionSet(w*h)
    for r in range(h): arr[r] = list(map(int, input().split()))
    for r in range(h):
        for c in range(w):
            if not arr[r][c]: continue
            for i in range(8):
                r2, c2 = r + mv[i][0], c + mv[i][1]
                if r2 < 0 or r2 >= h or c2 < 0 or c2 >= w or not arr[r2][c2]: continue
                u.unite(id(r, c), id(r2, c2))
    ans = 0
    for r in range(h):
        for c in range(w):
            if arr[r][c] and u.root(id(r, c)) == id(r, c): ans += 1
    print(ans)

Python: import sys
sys.setrecursionlimit(10 ** 5)
anslist = []
def sarch(i, j, visited):

    if 0 < i:
        if visited[i - 1][j] == 0:
            visited[i - 1][j] = 1
            sarch(i - 1, j, visited)
        if 0 < j:
            if visited[i - 1][j - 1] == 0:
                visited[i - 1][j - 1] = 1
                sarch(i - 1, j - 1, visited)
        if j < len(visited[0]) - 1:
            if visited[i - 1][j + 1] == 0:
                visited[i - 1][j + 1] = 1
                sarch(i - 1, j + 1, visited)

    if i < len(visited) - 1:
        if visited[i + 1][j] == 0:
            visited[i + 1][j] = 1
            sarch(i + 1, j, visited)
        if 0 < j:
            if visited[i + 1][j - 1] == 0:
                visited[i + 1][j - 1] = 1
                sarch(i + 1, j - 1, visited)
        if j < len(visited[0]) - 1:
            if visited[i + 1][j + 1] == 0:
                visited[i + 1][j + 1] = 1
                sarch(i + 1, j + 1, visited)

    if 0 < j:
        if visited[i][j - 1] == 0:
            visited[i][j - 1] = 1
            sarch(i, j - 1, visited)
    if j < len(visited[0]) - 1:
        if visited[i][j + 1] == 0:
            visited[i][j + 1] = 1
            sarch(i, j + 1, visited)
    return visited


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    grid = [list(map(int, input().split())) for j in range(h)]
    visited = [[0 for i in range(w)] for j in range(h)]
    starti = 0
    startj = 0
    for i in range(h):
        for j in range(w):
            if grid[i][j] == 0:
                visited[i][j] = -1
            else:
                visited[i][j] = 0

    i = 0
    j = 0
    cnt = 0
    ans = 0
    #print(visited)
    while True:
        if i >= h:
            i = 0
            j = 0
        while True:
            cnt += 1
            if j >= w:
                j = 0
                break
            #print(visited[i][j], 100100)
            if visited[i][j] == 0:
                ans += 1
                visited[i][j] = 1
                visited = sarch(i, j, visited)
                cnt = 0
            j += 1
        i += 1
        #print(cnt, visited)
        if cnt > w * h:
            #print(10)
            break
    anslist.append(ans)

for i in anslist:
    print(i)

Python: def island(w,h,M,i,j):
    q=[(i,j)]
    while len(q)>0:
        cur=q.pop()
        M[cur[0]][cur[1]]=0
        for k1 in range(-1,2):
            for k2 in range(-1,2):
                if 0<=cur[0]+k1<h and 0<=cur[1]+k2<w:
                    if M[cur[0]+k1][cur[1]+k2]==1:
                        q.append(tuple((cur[0]+k1,cur[1]+k2)))


def solve(w,h):
    count=0
    M=[list(map(int,input().split())) for _2 in range(h)]
    for i in range(h):
        for j in range(w):
            if M[i][j]==1:
                count += 1
                island(w,h,M,i,j)
    return count
                

w,h=map(int,input().split())
while(w>0):
    print(solve(w,h))
    w,h=map(int,input().split())

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

ans = []
while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    c = [list(map(int, input().split())) for _ in range(h)]
    s = [[0] * w for _ in range(h)]

    def dfs(x, y):
        s[y][x] = 1

        for dx in range(-1, 2, 1):
            for dy in range(-1, 2, 1):
                nx = x + dx
                ny = y + dy
                if 0 <= nx < w and 0 <= ny < h and c[ny][nx] == 1 and s[ny][nx] == 0:
                    dfs(nx, ny)

    res = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1 and s[i][j] == 0:
                dfs(j, i)
                res += 1
    ans.append(res)

for a in ans:
    print(a)