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, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


while 1:
	w, h = MAP()
	if w == 0 and h == 0:
		exit()

	c = [LIST() for _ in range(h)]

	cnt = 0
	check = [[0]*w for _ in range(h)]

	dy = (1, 1, 0, -1, -1, -1, 0, 1)
	dx = (0, -1, -1, -1, 0, 1, 1, 1)

	for y in range(h):
		for x in range(w):
			if check[y][x] == 0 and c[y][x] == 1:
				cnt += 1
				check[y][x] = cnt
				q = deque([(y, x)])
				while q:
					ny, nx = q.popleft()
					for k in range(8):
						ky = ny + dy[k]
						kx = nx + dx[k]
						if 0 <= ky < h and 0 <= kx < w and check[ky][kx] == 0 and c[ky][kx] == 1:
							check[ky][kx] = cnt
							q.append((ky, kx))

	print(cnt)

Python: import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))


def dfs(i, j, c):
#    print('dfs:')
#    pri(c)
    if i<0 or j<0 or i>=len(c) or j>=len(c[0]) or c[i][j]==0:
        return
    c[i][j] = 0

    for di in [-1, 0, 1]:
        for dj in [-1, 0, 1]:
            dfs(i+di, j+dj, c)
    
    return

def solve(w, h, c):
    cnt = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1:
                cnt += 1
                dfs(i, j, c)
    print(cnt)
    return


while True:
    w, h = map(int, input().split())
    c = [list(map(int, input().split())) for _ in range(h)]
    if w == h == 0:
        break
    solve(w, h, c)

Python: import sys

def dfs(i, j):
    todo.append([i, j])
    while(todo != []):
        v = todo.pop(len(todo)-1)
        seen[v[0]][v[1]] = 1
        for i in range(8):
            if m[v[0]+dx[i]][v[1]+dy[i]] == 1 and seen[v[0]+dx[i]][v[1]+dy[i]] == 0:
                todo.append([v[0]+dx[i], v[1]+dy[i]])

while(1):
    w, h = map(int, input().split())
    if not w+h:
        break
    m = [[0] * (w+2) for i in range(h+2)]
    for j in range(h):
        m[j+1][1:w+1] = [int(i) for i in input().split()]

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

    seen = [[0] * (w+2) for i in range(h+2)]
    todo = []
    ans = 0

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

    print(ans)

Python: #!python
# -*- coding: utf-8 -*-

import sys

sys.setswitchinterval(10**9)


next_s = ((-1, 1), (0, 1), (1, 1),
          (-1, 0), (1, 0),
          (-1, -1), (0, -1), (1, -1))


def DFS(x, y):
    if not (0 <= x < W and 0 <= y < H):
        return
    if map_[y][x] == 0:
        return

    map_[y][x] = 0

    for dx, dy in next_s:
        DFS(x + dx, y + dy)


while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    map_ = [list(map(int, input().split())) for h in range(H)]
    island_num = 0
    for x in range(W):
        for y in range(H):
            if map_[y][x] == 1:
                DFS(x, y)
                island_num += 1
    print(island_num)

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: import sys
sys.setrecursionlimit(10 ** 9)

def search_island(h, w, max_h, max_w):
    for i in range(8):
        next_h = h + [1, 1, 0, -1, -1, -1, 0, 1][i]
        next_w = w + [0, 1, 1, 1, 0, -1, -1, -1][i]
        if not 0 <= next_h < max_h or not 0 <= next_w < max_w:
            continue
        elif step[next_h][next_w] or not field[next_h][next_w]:
            continue
        step[next_h][next_w] = True
        search_island(next_h, next_w, max_h, max_w)

while True:
    max_w, max_h = map(int, input().split())
    if max_w == max_h == 0:
        break
    field = [[int(i) for i in input().split()] for _ in range(max_h)]
    step = [[False for _ in range(max_w)] for _ in range(max_h)]
    island = 0
    for i in range(max_h):
        for j in range(max_w):
            if step[i][j] or not field[i][j]:
                continue
            search_island(i, j, max_h, max_w)
            island += 1
    print(island)