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(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)

Python: w, h = 0, 0

def dfs(x, y, data):
    data[x][y] = 0
    for dx in range(-1, 2):
        for dy in range(-1, 2):
            sx, sy = x + dx, y + dy
            if sx < 0 or sx >= h or sy < 0 or sy >= w:
                continue
            if data[sx][sy] > 0:
                data = dfs(sx, sy, data)
    return data

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

    data = []
    for i in range(h):
        data.append(list(map(int, input().split())))

    ans = 0
    for x in range(h):
        for y in range(w):
            if data[x][y] > 0:
                ans += 1
                data = dfs(x, y, data)
    print (ans)