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: #演習2-24
import sys
sys.setrecursionlimit(10**6)

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 1:
    w,h = map(int,input().split())
    c = [input().split() for _ in range(h)]
    
    if w == 0 and h == 0:
        break
    cnt = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == "1":
                dfs(i,j)
                cnt += 1

    print(cnt)

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: from collections import deque
import sys
input = sys.stdin.readline

ldyx = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]


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

    MAP = [list(map(int, input().split())) for _ in range(h)]
    D = [[-1 for _ in range(w)] for _ in range(h)]    

    count = 1
    for j in range(h):
        for i in range(w):
            if D[j][i] == -1 and MAP[j][i] == 1:
                D[j][i] = count
                queue = deque([(j, i)])

                while queue:
                    (ny, nx) = queue.pop()
                    d = D[ny][nx]

                    # all neighbors
                    for (dy, dx) in ldyx:
                        new_y, new_x = ny + dy, nx + dx
                        if 0 <= new_x < w and 0 <= new_y < h:
                            if MAP[new_y][new_x] == 1:
                                if D[new_y][new_x] > -1:
                                    continue
                                D[new_y][new_x] = d
                                queue.append((new_y, new_x))

                # increase island number
                count += 1

    mv = 0
    for row in D:
        mv = max(mv, max(row))
    print(mv)

Python: def solve():
    from sys import stdin, setrecursionlimit
    setrecursionlimit(4000)
    f_i = stdin
    
    def dfs(pos):
        area_map[pos] = '0'
        for mv in move:
            next_pos = pos + mv
            if area_map[next_pos] == '1':
                dfs(next_pos)
    
    while True:
        w, h = map(int, f_i.readline().split())
        if w == 0:
            break
        
        area_map = ['0'] * (w + 2)
        for i in range(h):
            area_map += ['0'] + f_i.readline().split() + ['0']
        area_map += ['0'] * (w + 2)
        
        move = (1, -1, w + 1, w + 2, w + 3, -w - 3, -w - 2, -w - 1)
        
        ans = 0
        for i in range((w + 2) * (h + 2)):
            if area_map[i] == '1':
                dfs(i)
                ans += 1
        
        print(ans)

solve()

Python: import sys

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


def main():
    global w, h

    while True:
        count = 0
        w, h = map(int, input().split())
        if w is 0 and h is 0:
            break
        field = []
        [field.append(list(map(int, input().split()))) for _ in range(h)]

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

        print(count)


def dfs(field, x, y):
    field[y][x] = 0

    for dx_, dy_ in zip(dx, dy):
        next_x = x + dx_
        next_y = y + dy_
        if next_x < 0 or next_y < 0 or next_x >= w or next_y >= h or field[next_y][next_x] == 0:
            continue
        dfs(field, next_x, next_y)


if __name__ == '__main__':
    sys.setrecursionlimit(10000)
    main()

Python: import sys

sys.setrecursionlimit(1000000)

D = [(1,0), (0,1), (-1,0), (0,-1), (-1,-1),(1,-1),(-1,1),(1,1) ] # 下、右、上、左、斜め

def dfs(x,y):
  # Validation
  if not(0<=x<H and 0<=y<W) : return

  # 探索済みか、探索可能かを確認する
  if f[x][y]!=0 or G[x][y] == 0: return

  # Validation完了、探索範囲内、探索可能、ゴール以外＝＞visitフラグを立てる
  f[x][y]=1
 
  # 次の場所に向かう
  for dx,dy in D:
     nx=x+dx
     ny=y+dy
     dfs(nx,ny)
  return

def main():

    cnt=0
    for i in range(H):
        for j in range(W):
            if f[i][j]==0 and G[i][j]: 
                dfs(i, j)
                cnt+=1
    print(cnt)

while True:
    W,H=map(int,input().split())
    if W==0 and H==0: break
    G=[list(map(int,input().split())) for _ in range(H)]
    f=[[0]*W for _ in range(H)]

    main()