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(10**6)

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 -1 and 0 <= ny <= h - 1:
            if c[ny][nx] == '1':
                erase(nx, ny)

while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [[i for i in 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: def root(i):
    global roots
    if roots[i]==i:
        return i
    else:
        roots[i]=root(roots[i])
        return roots[i]

def unite(i, j):
    global roots
    ri=root(i)
    rj=root(j)
    #roots[max(ri, rj)]=min(ri, rj)
    if ri==rj: return
    roots[ri]=rj

directions=[(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]
while True:
    l=input().split()
    w=int(l[0])
    h=int(l[1])
    if w==0 and h==0: break
    map=[[False for _ in range(h+2)] for _ in range(w+2)]
    roots=[i for i in range((w+2)*(h+2))]
    for hi in range(h):
        l=input().split()
        for wi in range(w):
            map[wi+1][hi+1]=bool(int(l[wi]))

    for hi in range(1, h+1):
        for wi in range(1, w+1):
            if not map[wi][hi]: continue
            i=hi*(w+2)+wi
            for d in directions:
                if not map[wi+d[0]][hi+d[1]]: continue
                j=(hi+d[1])*(w+2)+(wi+d[0])
                unite(i, j)
    res=set()
    for hi in range(1, h+1):
        for wi in range(1, w+1):
            if not map[wi][hi]: continue
            i=hi*(w+2)+wi
            res.add(root(i))
    print(len(res))

Python: # -*- coding: utf-8 -*-
def dfs(x,y):
    c[x][y] = 0
    for dx in [-1,0,1]:
        for dy in [-1,0,1]:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
                dfs(nx, ny)
 
while True:
    w, h = map(int,input().split())
    if w == 0 and h == 0 :
        break
 
    c = [list(map(int,input().split())) for j in range(h)]
    count = 0
 
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1:
                dfs(i,j)
                count += 1
 
    print(count)

Python: # 25

import sys
import itertools

sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限
while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    c_l = [[int(x) for x in input().split()] for y in range(h)]
    s_l = [[True] * w for x in range(h)]
    v_l = list(itertools.product([-1,0,1],repeat=2))
    
    def dfs(y, x):
        if x < 0 or y < 0 or x >=w or y >= h:
            return False
        if not s_l[y][x]:
            return False
        s_l[y][x] = False
        _c = c_l[y][x]
        if _c == 0:
            return False
        for _v in v_l:
            dfs(y+_v[0],x+_v[1])
        return True

    ans = 0
    for i in range(h):
        for j in range(w):
            if s_l[i][j]:
                if dfs(i, j):
                    ans += 1
                    
    print(ans)

Python: def f(x,y):
    a[y][x]='0'
    for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:
        dx+=x;dy+=y
        if 0<=dx<w and 0<=dy<h and a[dy][dx]=='1':f(dx,dy)
while 1:
    w,h=map(int,input().split())
    if w==0:break
    a=[list(input().split()) for _ in[0]*h];b=0
    for i in range(w):
        for j in range(h):
            if a[j][i]=='1':
                b+=1;f(i,j)
    print(b)

Python: import queue

def proceess(start_x,start_y):
    #print("start_x : " + str(start_x) + ", start_y : " + str(start_y))
    #insland = []
    #insland.append([start_x,start_y])
    new_island = queue.Queue()
    map[start_x][start_y] = 0
    new_island.put([start_x,start_y])
    while True:
        ob = new_island.get()
        x = ob[0]
        y = ob[1]
        #print("x : " + str(x) + ", y : " + str(y))

        for i in [-1,0,1]:
            for j in [-1,0,1]:
                nx = x + i
                ny = y + j

                if nx >= 0 and ny >= 0 and nx < h and ny < w:
                    if map[nx][ny] == 1:
                        #insland.append([nx,ny])
                        #print("ny : " + str(ny) + ", nx : " + str(nx))
                        new_island.put([nx,ny])
                        map[nx][ny] = 0

        #print("end 8 count.")
        if new_island.empty():
            break

while True:
    map = []

    tmp = input().split(" ")
    w = int(tmp[0])
    h = int(tmp[1])

    if w == 0 and h == 0:
        break


    for j in range(h):
        nova = []
        arm = input()
        for k in arm.split(" "):
            #k.rstrip('\n')
            nova.append(int(k))
        map.append(nova)

    #print(map)

    count = 0
    for i in range(h):
        for j in range(w):
            if map[i][j] == 1:
                #print("i : " + str(i) + ", j : " + str(j))
                proceess(i,j)
                count += 1

    print(count)