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: import sys

sys.setrecursionlimit(10000)

def ironuri(x, y, t, img,h,w):
    if img[y][x]!=1:
        return 0

    img[y][x]=t
    
    if y<h-1:
        ironuri(x,y+1,t,img,h,w)
    if y>0:
        ironuri(x,y-1,t,img,h,w)
    if x<w-1:
        ironuri(x+1,y,t,img,h,w)
    if x>0:
        ironuri(x-1,y,t,img,h,w)

    if x>0 and y>0:
        ironuri(x-1,y-1,t,img,h,w)
    if x>0 and y<h-1:
        ironuri(x-1,y+1,t,img,h,w)
    if x<w-1 and y>0:
        ironuri(x+1,y-1,t,img,h,w)
    if x<w-1 and y<h-1:
        ironuri(x+1,y+1,t,img,h,w)

    
    return 1
    
while True:
    cheese=[]
    il=2
    w,h=map(int, input().split())
    if w==0 and h==0:
        break
    
    for i in range(h):
        ci=list(map(int, input().split()))
        cheese.append(ci)
    
    for i in range(h):
        for j in range(w):
            if(ironuri(j,i,il,cheese,h,w)):
                il+=1

    print(il-2)

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

import sys

sys.setrecursionlimit(10**6)


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


def dfs(y,x,h,w):
    graph[y][x] = 0
    for ny,nx in (y+1,x),(y-1,x),(y,x+1),(y,x-1),(y+1,x+1),(y-1,x-1),(y-1,x+1),(y+1,x-1):
        if not (0<=ny<h and 0<=nx<w) or graph[ny][nx] ==0:
            continue
        else:
            dfs(ny,nx,h,w)
    return 

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

Python: # import numpy
import sys

sys.setrecursionlimit(10**6)

digtmp = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]

def dfs(i, j):
    global dfsflag
    if Mapdata[i][j] == 1 and Mapcheck[i][j] == 0:
        Mapcheck[i][j] = 1
        dfsflag = 1
        for tmp in digtmp:
            digh = i + tmp[0]
            digw = j + tmp[1]
            if 0 <= digh < h and 0 <= digw < w:
                dfs(digh, digw)
    else:
        Mapcheck[i][j] = 1

while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    Mapdata = []
    for _ in range(h):
        tmp = list(map(int, input().split()))
        Mapdata.append(tmp)
    Mapcheck = [[0] * w for _ in range(h)]
    count = 0
    for i in range(h):
        for j in range(w):
            dfsflag = 0
            dfs(i, j)
            if dfsflag == 1:
                count += 1
    print(count)

Python: from itertools import product
import sys

sys.setrecursionlimit(1000000)

def s(m, x, y):
    m[y][x] = 0

    for dx, dy in product(range(-1, 2), range(-1, 2)):
        if m[y + dy][x + dx]:
            s(m, x + dx, y + dy)

while True:
    c = 0
    w, h = map(int, input().split())

    if w == h == 0:
        break

    m = [[0] * (w + 2)] + [[0] + list(map(int, input().split())) + [0] for _ in range(h)] + [[0] * (w + 2)]

    for x, y in product(range(w + 2), range(h + 2)):
        if m[y][x]:
            c += 1
            s(m, x, y)
            
    print(c)

Python: import sys
sys.setrecursionlimit(10 ** 5)
 
def erase(x, y):
    c[y][x] = '0'
    for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
        nx, ny = x + dx, y + dy
        if 0 <= nx < w and 0 <= ny < h and c[ny][nx] == '1':
            erase(nx, ny)
 
while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [input().split() for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == '1':
                ans += 1
                erase(x, y)
    print(ans)

Python: import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

a = [(x, y) for x in range(-1, 2) for y in range(-1, 2) if not (x == 0 and y == 0)]
def dfs(g, i, j):
    g[i][j] = 0
    for dx, dy in a:
        nx = i + dx
        ny = j + dy
        if 0 <= nx < h and 0 <= ny < w and g[nx][ny] == 1:
            dfs(g, nx, ny)

while True:
    w, h = na()
    if w == 0 and h == 0:
        break
    g = [na() for _ in range(h)]
    #seen = [[False] * w for _ in range(h)]
    ans = 0
    for i in range(h):
        for j in range(w):
            if g[i][j] == 1:
                dfs(g, i, j)
                ans += 1
    print(ans)