Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

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)

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)