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 fill(y,x):
    global w,h,l
    if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
        l[y][x+1]=0
        fill(y,x+1)
    if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
        l[y][x-1]=0
        fill(y,x-1)
    if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
        l[y+1][x]=0
        fill(y+1,x)
    if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
        l[y-1][x]=0
        fill(y-1,x)
    
    if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
        l[y+1][x+1]=0
        fill(y+1,x+1)
    if 0<=y-1<h and 0<=x+1<w and l[y-1][x+1]==1:
        l[y-1][x+1]=0
        fill(y-1,x+1)
    if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
        l[y+1][x-1]=0
        fill(y+1,x-1)
    if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
        l[y-1][x-1]=0
        fill(y-1,x-1)
    
while 1:
    w,h=map(int,input().split())
    if not w and not h:break
    l=[map(int,input().split()) for _ in range(h)]

    cnt=0
    while 1:
        cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
        if not cds:
            break
        else:
            y,x=cds[0]
        l[y][x]=0
        fill(y,x)
        cnt+=1
    print(cnt)

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

def dfs(y, x):
    if not (0 <= y < h) or not (0 <= x < w) or not L[y][x]:
        return
    else:
        L[y][x] = 0
        dy = [1, 1, 1, 0, 0, -1, -1, -1]
        dx = [-1, 0, 1, -1, 1, -1, 0, 1]
    for i in range(8):
        ny = y + dy[i]
        nx = x + dx[i]
        dfs(ny, nx)
while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    L = [list(map(int, input().split())) for _ in range(h)]
    cnt = 0
    for i in range(h):
        for j in range(w):
            if L[i][j]:
                cnt += 1
                dfs(i, j)
    print(cnt)

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

around = [(0,1),(1,0),(0,-1),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]

def dfs(i,j):
    visited[i][j] = True
    for di, dj in around:
        ni = i + di
        nj = j + dj
        if not (0 <= ni < H and 0 <= nj < W):
            continue
        if C[ni][nj] and not visited[ni][nj]:
            dfs(ni,nj)

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

    visited = [[False]*W for _ in range(H)]
        
    ans = 0

    for i in range(H):
        for j in range(W):
            if visited[i][j]:
                continue
            if C[i][j]:
                dfs(i,j)
                ans += 1

    print(ans)

Python: import sys
from pprint import pprint


class UnionFind:
    def __init__(self):
        self._parent = {}
        self._rank = {}
        self._equiv = {}
    
    def _find(self, x):
        if self._parent[x] == x:
            return self._parent[x]
        else:
            p = self._find(self._parent[x])
            self._parent[x] = p
            return p
    
    def _set_parent(self, child, parent):
        self._parent[child] = parent
        self._equiv[parent].update(self._equiv.pop(child))
    
    def _union(self, x, y):
        x = self._find(x)
        y = self._find(y)
        if x == y: return
        if self._rank[x] > self._rank[y]:
            self._set_parent(y, x)
        elif  self._rank[x] < self._rank[y]:
            self._set_parent(x, y)
        else:
            self._set_parent(x, y)
            self._rank[x] += 1
    
    def _make_set(self, x):
        self._parent[x] = x
        self._rank[x] = 0
        self._equiv[x] = {x}
    
    def unite(self, *elements):
        if not elements: return
        for x in elements:
            if x in self._parent: continue
            self._make_set(x)
        x0 = elements[0]
        for x in elements[1:]:
            self._union(x0, x)
    
    def groups(self):
        return [set(e) for e in self._equiv.values()]


def solve(ncol, nrow, rows):
    pairs = []
    # ?????????
    for x in range(ncol - 1):
        for y in range(nrow):
            pairs.append([(x, y), (x + 1, y)])

    # ?????????
    for x in range(ncol):
        for y in range(nrow - 1):
            pairs.append([(x, y), (x, y + 1)])

    # ???????????????
    for x in range(ncol - 1):
        for y in range(nrow - 1):
            pairs.append([(x, y), (x + 1, y + 1)])

    # ???????????????
    for x in range(ncol - 1):
        for y in range(1, nrow):
            pairs.append([(x, y), (x + 1, y - 1)])

    f = UnionFind()
    for x in range(ncol):
        for y in range(nrow):
            if rows[y][x]:
                f.unite((x, y))
    for (x1, y1), (x2, y2) in pairs:
        if rows[y1][x1] and rows[y2][x2]:
            f.unite((x1, y1), (x2, y2))
    return len(f.groups())


def main():
    while True:
        ncol, nrow = map(int, input().split())
        if ncol == 0 and nrow == 0:
            break
        rows = []
        for _ in range(nrow):
            row = list(map(int, input().split()))
            rows.append(row)
        # print(ncol, nrow)
        # pprint(rows)
        print(solve(ncol, nrow, rows))

main()

Python: import sys

sys.setrecursionlimit(10**6)


def is_in_map(x, y, w, h):
	return 0 <= x < w and 0 <= y < h


def dfs(x, y, lct):
	lct[y][x] = 0
	w = len(lct[0])
	h = len(lct)

	for i in range(-1, 2):
		for j in range(-1, 2):
			nx = x + i
			ny = y + j
			if not is_in_map(nx, ny, w, h):
				continue
			if lct[ny][nx] == 0:
				continue
			dfs(nx, ny, lct)

def resolve():
	while True:
		w, h = map(int, input().split())

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

		ans = 0
		lct = []

		for _ in range(h):
			lct.append(list(map(int, input().split())))
		for i in range(h):
			for j in range(w):
				if lct[i][j] == 0:
					continue
				dfs(j, i, lct)
				ans += 1
		print(ans)

resolve()