Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

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)

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)