Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

Python: import sys
sys.setrecursionlimit(4100000)


def dfs(cy, cx):
    # 一回走らせたら一つの島が消滅すると考える
    if not (cy < 0 or cy >= H or cx < 0 or cx >= W or gridgraph[cy][cx] == "0"):
        gridgraph[cy][cx] = "0"
        for dy, dx in adjacent:
            ny, nx = cy+dy, cx+dx
            dfs(ny, nx)


adjacent = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    gridgraph = [list(map(str, input().split())) for _ in range(H)]
    ans = 0
    for i in range(H):
        for j in range(W):
            if gridgraph[i][j] == "1":
                dfs(i, j)
                ans += 1
    print(ans)

Python: while 1:
	W, H = map(int, input().split())
	field = [input().split() for i in range(H)]
	if W == 0 and H == 0:
		break
	
	searched = [[0]*W for i in range(H)]
	stack = []
	count = 0
	for y in range(H):
		for x in range(W):
			if searched[y][x] == 0:
				searched[y][x] = 1
				if field[y][x] == '1':
					stack += [[x, y]]
					count += 1
					while (stack):
						sx, sy = stack.pop()
						for m in range(8):
							ny = sy + [0,1,1,1,0,-1,-1,-1][m]
							nx = sx + [1,1,0,-1,-1,-1,0,1][m]
							if (ny >= 0 and ny < H) and (nx >= 0 and nx < W):
								if (searched[ny][nx] == 0) and (field[ny][nx] == '1'):
									searched[ny][nx] = 1
									stack += [[nx, ny]]
	else:
		print(count)

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)