Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

Python: import sys
sys.setrecursionlimit(10000)

def get_two_int():
    two_int = input().split()
    for i in range(2):
        two_int[i] = int(two_int[i])
    return two_int

def get_data_list():
    data_list = input().split()
    for i, v in enumerate(data_list):
        data_list[i] = int(v)
    # print("data_list", data_list) # debug
    return data_list


def make_map(width, height):
    map_list = [[0] * (width + 2) for x in range(height + 2)] # まわりを0で囲む
    # print("map_list", map_list) # debug pprint?
    for h in range(1, height + 1):
        line_list = get_data_list()
        for w in range(1, width + 1):
            map_list[h][w] = line_list[w-1] # 汚い
    #print("map_list", map_list) # debug
    return map_list

def count_island(width, height, map_list):
    count = 0
    checked_set = set()
    for h in range(1, height + 1):
        for w in range(1, width + 1):
            if map_list[h][w] == 1 and ((h, w) not in checked_set):
                map_list, checked_set = check_around(h, w, map_list, checked_set)
                count += 1
    # print("checked_set", checked_set) # debug
    return count

def check_around(h, w, map_list, checked_set):
    # print((h, w)) #debug
    checked_set.add((h, w))
    # print("map_list", map_list) # debug
    for i in range(h - 1, h + 2): # まわりを全てチェック、大枠のまわりを0で余分に囲っているので無問題
        for j in range(w - 1, w + 2):
            if map_list[i][j] == 1 and (i, j) not in checked_set:
                    map_list, checked_set = check_around(i, j, map_list, checked_set)
    return map_list, checked_set


if __name__ == "__main__":
    while True:
        width, height = get_two_int()
        if width == 0:
            break
        # print("width", width) # debug
        # print("height", height) # debug
        map_list = make_map(width, height)
        # print("map_list", map_list) # debug 
        count = count_island(width, height, map_list)
        print(count)

Python: import sys

sys.setrecursionlimit(20000)

def fill(m, h, w, x, y):
    m[y][x] = 0
    for i in range(-1, 2):
        for j in range(-1, 2):
            nx = x + i
            ny = y + j
            if nx < 0 or nx >= w or ny < 0 or ny >= h:
                continue
            if m[ny][nx] == 0:
                continue
            fill(m, h, w, nx, ny)

def solve(h, w):
    m = []
    for j in range(h):
        line = list(map(int, input().rstrip().split(' ')))
        m.append(line)
    

    count = 0
    for y in range(h):
        for x in range(w):
            if m[y][x] != 0:
                count += 1
                fill(m, h, w, x, y)

    print(count)

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

    solve(h, w)

Python: if __name__ == '__main__':
    while 1:
        w,h = list(map(int,input().strip().split()))
        if w == h ==0:break
        imap = [list(map(int,input().strip().split())) for _ in range(h)]
        visited = []
        count = 0

        for j in range(h):
            for i in range(w):
                stack =[]
                if not (j,i) in visited and imap[j][i] == 1:
                    count +=1
                    stack.append((j,i))
                    visited.append(((j,i)))
                while len(stack) !=0:
                    y,x = stack.pop()
                    for dx in [-1,0,1]:
                        for dy in [-1,0,1]:
                            if  0<= x + dx < w and 0<= y + dy <h and not (y+dy,x+dx)in visited:
                                if imap[y + dy][x + dx] == 1:
                                    stack.append((y+dy,x+dx))
                                    visited.append(((y+dy,x+dx)))
        print(count)

Python: import sys
sys.setrecursionlimit(10000)
def f(x,y):
    if 0<=x<w and 0<=y<h and a[y][x]=='1':
        a[y][x]='0'
        for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:f(x+dx,y+dy)
while 1:
    w,h=map(int,input().split())
    if w==0:break
    a=[list(input().split()) for _ in[0]*h];b=0
    for i in range(w):
        for j in range(h):
            if a[j][i]=='1':
                b+=1;f(i,j)
    print(b)

Python: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

import sys
sys.setrecursionlimit(10000)


def dfs(x: int, y: int):
    """dfsで1つの地続きの島を探索
    Parameters
    ----------
        x, y : 探索対称点のxy座標
    """
    seen[x][y] = True
    for i in range(8):
        nx = x + dx[i]
        ny = y + dy[i]
        if (0 <= nx < H
                and 0 <= ny < W
                and seen[nx][ny] is False
                and chart[nx][ny] == 1):
            dfs(nx, ny)


while True:
    try:
        W, H = map(int, input().split())
        if W == 0:
            break
        else:
            chart = []
            for _ in range(H):
                chart.append(list(map(int, input().split())))
            seen = [[False] * W for _ in range(H)]
            dx = [1, 0, -1, 0, 1, 1, -1, -1]
            dy = [0, 1, 0, -1, 1, -1, -1, 1]
            ans = 0
            for i in range(H):
                for j in range(W):
                    # seen=Trueのマスは既に島としてカウントされた故それ以外を探索
                    if seen[i][j] is False and chart[i][j] == 1:
                        dfs(i, j)
                        # dfsで島1つを探索終了した故カウントをインクリメント
                        ans += 1
            print(ans)
    except ValueError:
        break

Python: 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 -1 and 0 <= ny <= h - 1:
            if c[ny][nx] == '1':
                erase(nx, ny)

while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [[i for i in input().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)