Go: // AOJ 1160: How Many Islands? http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var SEA = 0
var LAND = 1

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func discoverIslands(M, V [][]int, H, W int) int {
	// 島を探す
	queue := list.New()
	islands := 0
	for i := 1; i <= H; i++ {
		for j := 1; j <= W; j++ {
			// 訪問済みの土地ならスキップ
			if V[i][j] == 1 {
				continue
			}

			// 訪問中の土地が海なら訪問済みにしてスキップ
			if M[i][j] == SEA {
				V[i][j] = 1
				continue
			}

			// 未訪問の陸地が見つかったので
			// 島の数を1つ増やし、この陸地から辿れる陸地を訪問していく
			islands++
			queue.PushBack([]int{i, j})

			for queue.Len() > 0 {
				cur := queue.Remove(queue.Front()).([]int)
				h, w := cur[0], cur[1]
				if V[h][w] == 1 {
					continue
				}
				V[h][w] = 1

				// 全方向に未訪問の陸地がないか探索
				for v := -1; v <= 1; v++ {
					for l := -1; l <= 1; l++ {
						dh := h + v
						dw := w + l
						if M[dh][dw] == LAND && V[dh][dw] == 0 {
							queue.PushBack([]int{dh, dw})
						}
					}
				}
				/*
					if M[h-1][w] == LAND {
						queue.PushBack([]int{h - 1, w})
					}
					if M[h-1][w+1] == LAND {
						queue.PushBack([]int{h - 1, w + 1})
					}
					if M[h][w+1] == LAND {
						queue.PushBack([]int{h, w + 1})
					}
					if M[h+1][w+1] == LAND {
						queue.PushBack([]int{h + 1, w + 1})
					}
					if M[h+1][w] == LAND {
						queue.PushBack([]int{h + 1, w})
					}
					if M[h+1][w-1] == LAND {
						queue.PushBack([]int{h + 1, w - 1})
					}
					if M[h][w-1] == LAND {
						queue.PushBack([]int{h, w - 1})
					}
					if M[h-1][w-1] == LAND {
						queue.PushBack([]int{h - 1, w - 1})
					}
				*/
			}
		}
	}
	return islands
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	for {
		W, H := nextInt(sc), nextInt(sc)

		if W == 0 && H == 0 {
			break
		}
		// Initialize map and visted list
		M := make([][]int, H+2)
		V := make([][]int, H+2)
		for i := 0; i < H+2; i++ {
			M[i] = make([]int, W+2)
			V[i] = make([]int, W+2)
		}
		for i := 1; i <= H; i++ {
			for j := 1; j <= W; j++ {
				M[i][j] = nextInt(sc)
			}
		}

		islands := discoverIslands(M, V, H, W)

		fmt.Println(islands)
	}
}

Python: #!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

dire = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

def solve(W, H, c):
    ret = 0
    checked = [[0]*W for i in range(H)]
    q = deque()
    for i in range(H):
        for j in range(W):
            if (c[i][j]) and (not checked[i][j]):
                q.append((i, j)) #((now),(before))
                while q:
                    v = q.pop()
                    nowy, nowx = v
                    checked[nowy][nowx] = 1
                    for dy, dx in dire:
                        if (nowy+dy<H) and (nowy+dy>=0) and (nowx+dx<W) and (nowx+dx>=0):
                            if (c[nowy+dy][nowx+dx]) and (not checked[nowy+dy][nowx+dx]):
                                q.append((nowy+dy, nowx+dx))
                ret += 1
    return ret


def main():
    ans = []
    while True:
        W, H = LI()
        if (W==0) and (H==0):
            break
        c = LIR(H)
        ans.append(solve(W, H, c))
    for i in ans:
        print(i)


main()

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)