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(200000)
readline = sys.stdin.buffer.readline


def dfs(C, h, w):
    # 一度訪ねた陸地は'0'(海)に置き換える
    C[h][w] = '0'
    for i in range(-1, 2):
        for j in range(-1, 2):
            if 0 <= h+i < len(C) and 0 <= w+j < len(C[0]) and C[h+i][w+j] == '1':
                dfs(C, h+i, w+j)


W, H = 1, 1
while W != 0 or H != 0:
    W, H = map(int, input().split())
    C = []
    for h in range(H):
        c = list(input().split())
        #c = readline().split()
        C.append(c)

    count = 0
    for h in range(H):
        for w in range(W):
            if C[h][w] == '1':
                dfs(C, h, w)
                count += 1
    if W != 0 or H != 0:
        print(count)

Python: import sys
sys.setrecursionlimit(10**6)

# 移動ベクトル
dx = [1, 1, 0, -1, -1, -1, 0, 1]
dy = [0, 1, 1, 1, 0, -1, -1, -1]

def dfs(h, w):
    seen[h][w] = True
    
    # 次の探索位置を取得
    for dir in range(8):
        nh, nw = h + dx[dir], w + dy[dir]
        if nh < 0 or nh >= H or nw < 0 or nw >= W: continue
        if field[nh][nw] == 0: continue
        if seen[nh][nw] == True: continue
        dfs(nh, nw)
        
while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    
    field = [[int(i) for i in input().split()] for j in range(H)]

    seen = [[False for i in range(W)] for j in range(H)]

    count = 0
    for h in range(H):
        for w in range(W):
            if seen[h][w] == False and field[h][w] == 1:
                dfs(h, w)
                count += 1

    print(count)

Python: import sys

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7) 

count_list = []

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

    grid = [0] * (w + 2)
    for _ in range(h):
        grid += [0]
        grid += list(map(int, input().split()))
        grid += [0]
    grid += [0] * (w + 2)

    move = [-1, +1, -(w + 2) - 1, -(w + 2), -(w + 2) + 1, w + 2 - 1, w + 2, w + 2 + 1]
    visit = set()
    count = 0

    def search(x):
        if x not in visit:
            visit.add(x)
            for dx in move:
                y = x + dx
                if grid[y] == 0:
                    continue
                else:
                    search(y)

    for i in range((w+2) * (h+2)):
        if grid[i] == 0 or i in visit:
            continue
        count += 1
        search(i)
    count_list.append(count)

for i in range(len(count_list)):
    print(count_list[i])

Python: def dfs(tiles, W, H, x, y):
    tiles[y][x] = '0'
    for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
        if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
            dfs(tiles, W, H, x+dx, y+dy)
    return

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [[c for c in input().split()] for h in range(H)]
        cnt = 0
        for h in range(H):
            for w in range(W):
                if tiles[h][w] == '1':
                    cnt += 1
                    dfs(tiles, W, H, w, h)
        print(cnt)

if __name__ == '__main__':
    main()

Python: 
dirs = [(-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,h) == (0,0):
		break
	tile = [input().split() for i in range(h)]
	count = 0
	for i in range(h):
		for j in range(w):
			if tile[i][j] == '1':
				count += 1
				q = [(i,j)]
				while q:
					(x,y) = q.pop()
					for d in dirs:
						xx = x+d[0]
						yy = y+d[1]
						if (0 <= xx and xx < h) and (0 <= yy and yy < w):
							if tile[xx][yy] == '1':
								tile[xx][yy] = '0'
								q.append((xx,yy))
	print(count)

Python: import sys
sys.setrecursionlimit(2500)
w,h = map(int,input().split())
while not(w==h==0):
    m = [[0]*(w+2)]
    m += [[0]+list(map(int,input().split()))+[0] for i in range(h)]
    m += [[0]*(w+2)]
    g = [[] for i in range(w*h)]
    for i in range(1,h+1):
        for j in range(1,w+1):
            if m[i][j]==1:
                for k in [-1,0,1]:
                    for l in [-1,0,1]:
                        if m[i+k][j+l]==1:
                            g[(i-1)*w+j-1].append((i+k-1)*w+j+l-1)
    reach = [False]*(w*h)
    def dfs(v):
        reach[v] = True
        for i in g[v]:
            if not reach[i]:
                dfs(i)
    ans = 0   
    for i in range(h*w):
        if g[i] and not reach[i]:
            ans += 1
            dfs(i)

    print(ans)

    w,h = map(int,input().split())

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