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: # coding: utf-8
import sys

stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip() # ignore trailing spaces
    

def main():
    w,h = na()
    if w==0 and h==0:
        return 0
    field = [na() for _ in range(h)]
    checked = [[0 for _ in range(w)] for __ in range(h)]
    ans = 0
    for i in range(h):
        for j in range(w):
            if checked[i][j] == 1 or field[i][j] == 0:
                continue
            ans += 1
            que = [(j,i)]
            while que != []:
                now = que.pop(0)
                x,y = now[0],now[1]
                checked[y][x] = 1
                for dx in range(-1,2,1):
                    for dy in range(-1,2,1):
                        tmpx = x+dx
                        tmpy = y+dy
                        if 0<=tmpx and tmpx<w and 0<=tmpy and tmpy<h:
                            if field[tmpy][tmpx] == 1 and checked[tmpy][tmpx] == 0:
                                checked[tmpy][tmpx] = 1
                                que.append((tmpx,tmpy))
    print(ans)
    return 1


if __name__ == '__main__':
    while main():
        continue

Python: #演習2-24
import sys
sys.setrecursionlimit(10**6)

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

while 1:
    w,h = map(int,input().split())
    c = [input().split() for _ in range(h)]
    
    if w == 0 and h == 0:
        break
    cnt = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == "1":
                dfs(i,j)
                cnt += 1

    print(cnt)

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: from collections import deque
import sys
input = sys.stdin.readline

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


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break

    MAP = [list(map(int, input().split())) for _ in range(h)]
    D = [[-1 for _ in range(w)] for _ in range(h)]    

    count = 1
    for j in range(h):
        for i in range(w):
            if D[j][i] == -1 and MAP[j][i] == 1:
                D[j][i] = count
                queue = deque([(j, i)])

                while queue:
                    (ny, nx) = queue.pop()
                    d = D[ny][nx]

                    # all neighbors
                    for (dy, dx) in ldyx:
                        new_y, new_x = ny + dy, nx + dx
                        if 0 <= new_x < w and 0 <= new_y < h:
                            if MAP[new_y][new_x] == 1:
                                if D[new_y][new_x] > -1:
                                    continue
                                D[new_y][new_x] = d
                                queue.append((new_y, new_x))

                # increase island number
                count += 1

    mv = 0
    for row in D:
        mv = max(mv, max(row))
    print(mv)

Python: def solve():
    from sys import stdin, setrecursionlimit
    setrecursionlimit(4000)
    f_i = stdin
    
    def dfs(pos):
        area_map[pos] = '0'
        for mv in move:
            next_pos = pos + mv
            if area_map[next_pos] == '1':
                dfs(next_pos)
    
    while True:
        w, h = map(int, f_i.readline().split())
        if w == 0:
            break
        
        area_map = ['0'] * (w + 2)
        for i in range(h):
            area_map += ['0'] + f_i.readline().split() + ['0']
        area_map += ['0'] * (w + 2)
        
        move = (1, -1, w + 1, w + 2, w + 3, -w - 3, -w - 2, -w - 1)
        
        ans = 0
        for i in range((w + 2) * (h + 2)):
            if area_map[i] == '1':
                dfs(i)
                ans += 1
        
        print(ans)

solve()