Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

Python: def dfs(w,h,W,H,ID,islands,islandsID):
    if islands[h][w]==1 and islandsID[h][w]!=0:
        for i in range(-1,2):
            for j in range(-1,2):
                dw=w+i
                dh=h+j
                if dw>=0 and dw<W and dh>=0 and dh<H:
                    if islands[dh][dw]==1:
                        islandsID[dh][dw]=islandsID[h][w]
    if islands[h][w]==1 and islandsID[h][w]==0:
        for i in range(-1,2):
            for j in range(-1,2):
                dw=w+i
                dh=h+j
                if dw>=0 and dw<W and dh>=0 and dh<H:
                    if islands[dh][dw]==1:
                        islandsID[dh][dw]=ID
        ID+=1
    return ID

while True:
    islands=[]
    islandsID=[]
    ID=1
    W,H=map(int,input().split())
    if W==0:
        break
    for i in range(H):
        islandsID.append([0 for j in range(W)])
    for _ in range(H):
        islands.append(list(map(int,input().split())))
    for h in range(H):
        for w in range(W):
            ID=dfs(w,h,W,H,ID,islands,islandsID)
    print(ID-1)

Python: # -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

# 8方向
directions = ((1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1))
cnt = 0

def rec(cur, prev):

    global cnt
    h, w = cur // (W+2), cur % (W+2)
    if visited[h][w]:
        return
    visited[h][w] = 1
    # 初回のみ、島カウントを足す
    if prev == -1:
        cnt += 1

    for d in directions:
        h2, w2 = h + d[0], w + d[1]
        if grid[h2][w2]:
            rec(h2*(W+2)+w2, cur)
    return 

while True:

    W, H = MAP()
    if H == W == 0:
        break

    grid = list2d(H+2, W+2, 0)
    for i in range(1, H+1):
        row = LIST()
        for j in range(1, W+1):
            grid[i][j] = row[j-1]

    visited = list2d(H+2, W+2, 0)
    cnt = 0
    for i in range(1, H+1):
        for j in range(1, W+1):
            if grid[i][j]:
                rec(i*(W+2)+j, -1)
    print(cnt)

Python: from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

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

def dfs(x,y):
    global MAP
    if MAP[y][x] == 0:
        return
    else:
        MAP[y][x] = 0
        for i in range(9):
            dfs(x+dx[i],y+dy[i])


while True:
    W,H = inpl()
    if W == 0:
        break
    else:
        MAP = [[0] + inpl() + [0] for i in range(H)]
        MAP = [[0]*(W+2)] + MAP + [[0]*(W+2)]
        ans = 0
        for y in range(1,H+1):
            for x in range(1,W+1):
                if MAP[y][x] == 1:
                    ans += 1
                    dfs(x,y)
        print(ans)

Python: import sys
sys.setrecursionlimit(2500)
 
def erase(x, y):
    c[y][x] = '0'
    for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
        nx, ny = x + dx, y + dy
        if 0 <= nx < w and 0 <= ny < h and c[ny][nx] == '1':
            erase(nx, ny)
 
while True:
    w, h = map(int, sys.stdin.readline().split())
    if w == h == 0:
        break
    c = [sys.stdin.readline().split() for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == '1':
                ans += 1
                erase(x, y)
    print(ans)

Python: def solver():
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        return None
    islands = set()
    counter = 2
    c = [[0] + [int(x) for x in input().split()] + [0]  for _ in range(h)]
    c = [[0 for _ in range(w+2)]] + c + [[0 for _ in range(w+2)]]
    for i in range(1, h+1):
        for j in range(1, w+1):
            if c[i][j] > 0:
                neighbour = {c[i-1][j-1], c[i-1][j], c[i-1][j+1], c[i][j-1]}
                if max(neighbour) > 1:
                    c[i][j] = max(neighbour)
                    if len(neighbour) > 2:
                        islands -= {max(neighbour)}
                else:
                    c[i][j] = counter
                    islands |= {counter}
                    counter += 1
    return len(islands)

outputs = []
while(True):
    ans = solver()
    if ans == None:
        break
    outputs.append(ans)
print('\n'.join(map(str, outputs)))

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)