Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

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

import sys

import numpy as np

sys.setswitchinterval(10**8)


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 not map_[y * W + x]:
        return

    map_[y * W + 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_ = []
    for h in range(H):
        map_ += list(map(int, input().split()))
    # print(map_)
    island_num = 0
    while any(map_):
        next_idx = map_.index(1)
        x = next_idx % W
        y = next_idx // W
        # print(map_)
        # print("x: {} | y: {}".format(x, y))
        DFS(x, y)
        island_num += 1

    print(island_num)

Python: def dfs(startx, starty):
    dxdy = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]
    stack = [(startx, starty)]
    reached = set()
    while stack:
        d = stack.pop()
        reached.add(d)
        for a in dxdy:
            x = d[0] + a[0]
            y = d[1] + a[1]
            if (x, y) not in reached and 0 <= x < W and 0 <= y < H and maze[y][x] != '0':
                stack.append((x, y))
    return reached


while True:
    W, H = map(int, input().split())
    if H == W == 0:
        break
    maze = [input().split()for _ in range(H)]
    ans = 0
    l = set()
    for i in range(H):
        for j in range(W):
            if (j, i) not in l and maze[i][j] == '1':
                l = l | dfs(j, i)
                ans += 1
    print(ans)

Python: import sys
sys.setrecursionlimit(10000000)


while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    G = [list(map(int, input().split())) for i in range(H)]
    dh = (0, -1, -1, -1, 0, 1, 1, 1)
    dw = (-1, -1, 0, 1, 1, 1, 0, -1)

    def dfs(h, w):
        for i in range(8):
            nh = h + dh[i]
            nw = w + dw[i]
            if nh < 0 or nh >= H or nw < 0 or nw >= W:
                continue
            if not G[nh][nw]:
                continue
            G[nh][nw] = 0
            dfs(nh, nw)
    
    cnt = 0
    for i in range(H):
        for j in range(W):
            if G[i][j]:
                dfs(i, j)
                cnt += 1
    print(cnt)

Python: import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)


def search(i, j):
    global ans
    if c[i][j] == 1:
        c[i][j] = -1
        search(i, j+1)
        search(i+1, j-1)
        search(i+1, j)
        search(i+1, j+1)
        search(i, j-1)
        search(i-1, j)
        search(i-1, j+1)
        search(i-1, j-1)


while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [[] for _ in range(h + 2)]
    c[0] = [0] * (w + 2)
    c[-1] = [0] * (w + 2)
    for i in range(h):
        c[i + 1] = [0] + list(map(int, input().split())) + [0]
    ans = 0
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if c[i][j] == 1:
                ans += 1
                search(i, j)
    print(ans)

Python: import sys
sys.setrecursionlimit(10000)
d = [-1,0,1]
def search(x,y,m):
    if not(0<=x<len(m[0]) and 0<=y<len(m)) or m[y][x]=='0': return
    m[y][x] = '0'
    for dx,dy in [[-1,-1],[0,-1],[1,-1],[-1,0],[1,0],[-1,1],[0,1],[1,1]]:
        search(x+dx,y+dy,m)
while True:
    w,h = map(int,input().split())
    if w==h==0: break
    m = [input().split() for _ in range(h)]
    c = 0
    for y in range(h):
        for x in range(w):
            if m[y][x]=='1':
                c += 1
                search(x,y,m)
    print(c)

Python: import sys
sys.setrecursionlimit(1000000)


def dfs(h, w, field, seen, dhw):
    seen[h][w] = True
    for dh, dw in dhw:
        nh = h + dh
        nw = w + dw

        if field[nh][nw] == 0:
            continue
        if seen[nh][nw]:
            continue

        dfs(nh, nw, field, seen, dhw)


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

        field = []
        field.append([0] * (w + 2))
        for i in range(h):
            line = list(map(int, input().split()))
            field.append([0] + line + [0])
        field.append([0] * (w + 2))

        seen = [[False] * (w + 2) for x in range(h + 2)]
        dhw = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (-1, 1), (1, -1)]

        count = 0
        for ih in range(1, h+2):
            for iw in range(1, w+2):
                if seen[ih][iw]:
                    continue
                if field[ih][iw] == 1:
                    dfs(ih, iw, field, seen, dhw)
                    count += 1
        res.append(count)

    for i in res:
        print(i)


main()