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(10**6)

def dfs(pos):
  i = pos[0]
  j = pos[1]
  seen[j][i] = True
  for k in range(-1+i,2+i):
    for l in range(-1+j,2+j):
      if 0 <= k < w and 0 <= l < h:
        if field[l][k] == 1 and (not seen[l][k]):
          dfs([k,l])

while True:
  w, h = map(int,input().split())
  if w == 0 and h == 0:
    break
  field = [list(map(int,input().split())) for _ in range(h)]
  seen = [[False]*w for _ in range(h)]
  count = 0
  for i in range(w):
    for j in range(h):
      if field[j][i] == 1 and (not seen[j][i]):
        dfs([i,j])
        count += 1
  print(count)

Python: import sys
sys.setrecursionlimit(100000)

def dfs(i, j):
    g[i][j] = 0

    for x in range(-1, 2):
        for y in range(-1, 2):
            dx = x + i
            dy = y + j

            if 0<=dx<h and 0<=dy<w and g[dx][dy] == 1:
                dfs(dx, dy)
    return

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

    g = [list(map(int, input().split())) for _ in range(h)]

    ans = 0
    for i in range(h):
        for j in range(w):
            if g[i][j] == 1:
                dfs(i, j)
                ans += 1

    print(ans)

Python: m = []

def walk(i,j):
    stack = []
    stack.append((i,j))
    while stack:
        p = stack.pop()
        pi = p[0]
        pj = p[1]
        m[pi][pj] = 0
        for k in range(-1,2):
            for l in range(-1,2):
                a = pi+k
                b = pj+l
                if a < 0 or b < 0 \
                        or a >= len(m) or b >= len(m[a]):
                            pass
                elif m[a][b] == 0:
                    pass
                else:
                    stack.append((a,b))

while True:
    w, h = map(int, input().split())
    if w == 0: break
    for i in range(h):
        m.append(list(map(int, input().split())))
    ans = 0
    for i in range(len(m)):
        for j in range(len(m[i])):
            if m[i][j] == 1:
                walk(i,j)
                ans += 1
    print(ans)
    m = []

Python: import sys
sys.setrecursionlimit(10 ** 6)
islands = []
visited_list = []
w_list = []
h_list = []
while True:
    w ,h = map(int,input().split())
    if w == 0 and h == 0:
        break
    w_list.append(w)
    h_list.append(h)
    island = [list(input().split()) for _ in range(h)]
    islands.append(island)
    visited_list.append([[False] * w for _ in range(h)])

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

def dfs(island_m ,now_row , now_column , idx):
    if now_row < 0 or h_list[idx] <= now_row or now_column < 0 or w_list[idx] <= now_column or island_m[now_row][now_column] == "0":
        return
    if visited_list[idx][now_row][now_column]:
        return
    visited_list[idx][now_row][now_column] = True
    for i in range(8):
        dfs(island_m,now_row + dx[i] ,now_column + dy[i],idx)


for m in range(len(islands)):
    cnt = 0
    for row in range(h_list[m]):
        for column in range(w_list[m]):
            if not visited_list[m][row][column] and islands[m][row][column] == "1":
                dfs(islands[m] , row , column , m)
                cnt += 1
    print(cnt)

Python: from collections import deque
from itertools import product
import itertools

def dfs(w, h, graph):
    visited = [[0] * w for _ in range(h)]
    dy_dx = [(0, 0), (0, 1), (0, -1), (1, 1), (1, -1), (1, 0), (-1, 1), (-1, -1), (-1, 0)]
    dots = itertools.product(range(h), range(w))
    count = 0
    for dot in dots:
        dot_y, dot_x = dot
        if graph[dot_y][dot_x] == 0:
            continue
        if visited[dot_y][dot_x] == 1:
            continue

        q = deque()
        q.append((dot_y, dot_x))
        while q:
            move_y, move_x = q.pop()

            for delta in dy_dx:
                dy, dx = delta

                moved_x = move_x + dx
                moved_y = move_y + dy

                if moved_y < 0 or h -1 < moved_y or moved_x < 0 or w -1 < moved_x:
                    continue
                if graph[moved_y][moved_x] == 0:
                    continue
                if visited[moved_y][moved_x] == 0:
                    q.append((moved_y, moved_x))
                    visited[moved_y][moved_x] = 1
        
        count += 1

    return count

if __name__ == "__main__":

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

        graph = [list(map(int, input().split())) for _ in range(h)]
        answer_list.append(dfs(w, h, graph))
    
    for answer in answer_list:
        print(answer)

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

def dfs(h,w,visited,S):
    #print(h,w)
    for i in range(8):
        h0 = h+dh[i]
        w0 = w+dw[i]
        if visited[h0][w0]==False and S[h0][w0]=='1':
            visited[h0][w0]=True
            dfs(h0,w0,visited,S)

ans = []
while True:
    W,H = map(int, input().split())
    if (W,H) == (0,0):
        break
    S = [0]*(H+2)
    S[0] = ['*']*(W+2)
    S[H+1] = ['*']*(W+2)
    for h in range(1,H+1):
        S[h] = ['*'] + input().split() + ['*']

    visited = [[False]*(W+2) for _ in range(H+2)]
    dh = [0,1,1,1,0,-1,-1,-1]
    dw = [1,1,0,-1,-1,-1,0,1]

    cnt = 0
    for h in range(1,H+2):
        for w in range(1,W+2):
            if visited[h][w]==False and S[h][w]=='1':
                visited[h][w]=True
                dfs(h,w,visited,S)
                cnt += 1
    ans.append(cnt)
print(*ans, sep='\n')