Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

Python: for i in range(10**6):
    W,H=map(int,input().split())
    if H==0 and W==0:
        exit()
    L=[]
    l=[0 for j in range(W+2)]
    L.append(l)
    for h in range(H):
        a=list(map(int,input().split()))
        a=[0]+a+[0]
        L.append(a)
    L.append(l)
    
    P=[[-1 for w in range(W+2)]for h in range(H+2)]
    for h in range(1,H+1):
        for w in range(1,W+1):
            P[h][w]=(h-1)*W+(w-1)
    Size=[[0 for w in range(W+2)]for h in range(H+2)]
    #print(P)
    def find(a):
        x=a%W+1
        y=a//W+1
        if P[y][x]==a:
            return a
        else:
            return find(P[y][x])
    def union(b,c):
        B=find(b)
        C=find(c)
        if B!=C:
            P[C//W+1][C%W+1]=P[B//W+1][B%W+1]
    for h in range(1,H+1):
        for w in range(1,W+1):
            if L[h][w]==1:
                if L[h-1][w-1]==1:
                    union(P[h][w],P[h-1][w-1])
                if L[h-1][w]==1:
                    union(P[h][w],P[h-1][w])
                if L[h][w-1]==1:
                    union(P[h][w],P[h][w-1])
                if L[h-1][w+1]==1:
                    union(P[h][w],P[h-1][w+1])
                if L[h+1][w-1]==1:
                    union(P[h][w],P[h+1][w-1])
                
    cnt=0
    for h in range(1,H+1):
        for w in range(1,W+1):
            P[h][w]=find(P[h][w])
            if L[h][w]==1 and P[h][w]==(h-1)*W+(w-1):
                cnt+=1
    #print(L)
    #print(P)
    print(cnt)

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)