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(1000000)

while(1):
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    else:
        maps = [[-1]*(w+2)] + [([-1] + list(map(int, input().split())) + [-1]) for _ in range(h)] + [[-1]*(w+2)]
        seen = [[True]*(w+2)] + [([True] + [False]*w + [True]) for _ in range(h)] + [[True]*(w+2)]
        dxy = ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))
        
        
        def dfs(y, x):
            seen[y][x] = True
            for i in dxy:
                if seen[y+i[0]][x+i[1]] is False:
                    if maps[y+i[0]][x+i[1]] == 0:
                        seen[y+i[0]][x+i[1]] = True
                    else:
                        dfs(y+i[0], x+i[1])
        
        now = 0
        ans = 0
        while now < w * h:
            y = now // w + 1
            x = now % w + 1
            if seen[y][x] is False:
                if maps[y][x] == 0:
                    seen[y][x] = True
                else:
                    ans += 1
                    dfs(y, x)
            
            now += 1
        
        print(ans)

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

ans = []

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 True:
    w, h = map(int, input().split())
    if w == 0 and h ==0:
        break

    count = 0

    c = [[] for _ in range(h)]
    for i in range(h):
        if w == 1:
            c[i] = [int(input())]
        else:
            c[i] = list(map(int, input().split()))

    for x in range(h):
        for y in range(w):
            if c[x][y] == 1:
                dfs(x, y)
                count += 1

    ans.append(count)

for i in ans:
    print(i)

Python: import sys
sys.setrecursionlimit(5000)

dx = [1, 0, -1]
dy = [1, 0, -1]

def dfs(area, x, y):
    area[x][y] = 0
    for i in dx:
        for j in dy:
            next_x = x + i
            next_y = y + j
            if area[next_x][next_y] == 1:
                dfs(area, next_x, next_y)

def solver(w, h):
    ans = 0
    area = [[0]*(w+2)]
    for i in range(h):
        area.append([0]+list(map(int, input().split()))+[0])
    area.append([0]*(w+2))

    for i in range(1, h+1):
        for j in range(1, w+1):
            if area[i][j] == 1:
                ans += 1
                dfs(area, i, j)
    return ans

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

Python: import sys
sys.setrecursionlimit(2500)

def dfs(v):
    y, x = v
    checked[y][x] = 1
    ret = 1
    for dy, dx in directions:
        if y+dy>=h or y+dy<0 or x+dx>=w or x+dx<0:
            continue
        elif Map[y+dy][x+dx]==0:
            continue
        if not checked[y+dy][x+dx]:
            ret +=  dfs((y+dy, x+dx))
    return ret


directions = ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))
while 1:
    w, h = map(int, input().split())
    if w==0 and h==0:
        break
    Map = [list(map(int, input().split())) for _ in range(h)]
    SUM = sum([sum(l) for l in Map])
    checked = [[0]*w for _ in range(h)]
    ans = 0
    cnt = 0
    for i in range(h):
        for j in range(w):
            if not checked[i][j] and Map[i][j]!=0:
                ans += 1
                cnt += dfs((i, j))
            if cnt==SUM:
                break
    print(ans)

Python: # -*- coding: utf-8 -*-
#150 島は幾つある？
h,w=1,1

count=0
anser=0

def search(x,y):
    #マップの外側か壁の場合は何もしない
    #print(x,y)

    global count
    count+=1
    if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
 
        return
        
    
    #以前に到達していたら何もしない
    if reached[y][x] ==0:
         return

    reached[y][x]=0
    
    #print("aa")



    search(x+1,y)#右
    search(x-1,y)#左
    search(x,y+1)#上
    search(x,y-1)#下
    search(x-1,y-1)
    search(x-1,y+1)
    search(x+1,y-1)
    search(x+1,y+1)



while h!=0 and w!=0: 
    anser=0
    count=1
    #グラフの入力方法------

    w,h=map(int ,raw_input().split())
    if h==0 and w==0:
        break

    map_data = [[0]*w for _ in range(h)]
    reached = [[1]*w for _ in range(h)]

    for i in range(h):
        ct = raw_input().split()
       
        for j in range(w):
            map_data[i][j] = int(ct[j])
            reached [i][j]=int(ct[j])
            if ct[j]==1:
                sx=j
                sy=i

    t=0
    a=0
    while a<=h:
       if 1 in reached[t%h]:         
            search(reached[t%h].index(1),t%h)
            anser+=1
            #print("count",count)
            count=1
            a=0
       elif count==h:
                break
       t+=1
       a+=1

    print(anser)