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(500*500) #再起上限回数突破

class Node:
    def __init__(self, data):
        self.data = data
        self.f_search = False
        
    def __repr__(self):
        return str(self.data) + "," + str(self.f_search)
        

data_set = list()

while(1):
    w, h = map(int, input().split() )
    if w == 0 and h == 0:
        break

    aa = list()
    for _ in range(h):
        a = list()
        for d in map(int, input().split() ):
            a.append(Node(d))
        aa.append(a)

    data_set.append(aa)


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

iland_cnt = 0
def search_iland(iland_data):
    global iland_cnt
    for y in range(len(iland_data)):
        for x in range(len(iland_data[0])):
            if iland_data[y][x].f_search == True \
            or iland_data[y][x].data == 0:
                continue
            iland_cnt += 1
            iland_data[y][x].f_search = True
            for (dx,dy) in search_vec:
                if x+dx < 0 or y+dy < 0 \
                or x+dx >= len(iland_data[0]) \
                or y+dy >= len(iland_data): continue
                search_iland_area(iland_data, x+dx, y+dy)
            
            

def search_iland_area(iland_data, sx, sy):  
    if iland_data[sy][sx].f_search == True \
    or iland_data[sy][sx].data == 0:
        return 
    iland_data[sy][sx].f_search = True
    for (dx,dy) in search_vec:
        if sx+dx < 0 or sy+dy < 0 \
        or sx+dx >= len(iland_data[0]) \
        or sy+dy >= len(iland_data): continue
        search_iland_area(iland_data, sx+dx, sy+dy)
            
for iland in data_set:
    iland_cnt = 0
    search_iland(iland)
    print(iland_cnt)

Python: #「探索候補スタック」「探索済みリスト」「現在地点」の３点セットでとらえる
while 1:
    w,h=map(int,input().split())
    if w==h==0:
        break
    c=[list(map(int,input().split())) for _ in range(h)]
    dxdy=[[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]
    ans=0
    for y in range(h):
        for x in range(w):
            if c[y][x]:
                ans+=1
                stack=[]
                stack.append([x,y])
                while stack:
                    xx,yy=stack.pop()
                    for dx,dy in dxdy:
                        nx=xx+dx
                        ny=yy+dy
                        if 0<=nx<w and 0<=ny<h:
                            if c[ny][nx]:
                                c[ny][nx]=0
                                stack.append([nx,ny])
    print(ans)

Python: # coding: utf-8

import sys 

sys.setrecursionlimit(1000000)
class DFSSolver:

    def __init__(self, table, h, w):
        self.table = table
        self.count = 0
        self.h = h
        self.w = w

    def delta(self, i, j):
        candididate = [(i-1, j-1), (i-1, j), (i-1, j+1),
                       (i, j-1), (i, j+1),
                       (i+1, j-1), (i+1, j), (i+1, j+1)]
        return candididate

    def dfs(self, i, j):
        self.table[i][j] = 0
        for i, j in self.delta(i, j):
            if i < 0 or j < 0 or i >= self.h or j >= self.w:
                pass
            elif self.table[i][j] == 1:
                self.dfs(i, j)

    def solve(self):
        for i in range(self.h):
            for j in range(self.w):
                if self.table[i][j] == 1:
                    self.dfs(i, j)
                    self.count += 1
        print(self.count)


W, H = list(map(int, input().split(" ")))
while W != 0 and H != 0:
    t = []
    for _ in range(H):
        t.append(list(map(int, input().split(" "))))
    d = DFSSolver(t, H, W)
    d.solve()
    W, H = list(map(int, input().split(" ")))

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

def dfs(s,Map,H,W,c):
    dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
    for i in range(len(dxdy)):
        Map[s[0]][s[1]] = c
        # nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
        # if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
        #     if Map[nextPos[0]][nextPos[1]] == 1:
        #         dfs(nextPos,Map,H,W,c)

while True:
    W,H = map(int,input().split(" "))
    if W == 0 and H == 0:
        break
    Map = [list(map(int,input().split())) for i in range(0,H)]
    c = 1
    for i in range(H):
        for j in range(W):
            if Map[i][j] == 1:
                c += 1
                dfs((i,j),Map,H,W,c)
    print(c-1)

Python: import sys
sys.setrecursionlimit(10**6)

def dfs(i, j):  # i行j列の島を探索する
    seen[i][j] = 1

    for k in range(i-1, i+2):
        for l in range(j-1, j+2):
            if k < 0 or k >= h or l < 0 or l >= w: continue
            if seen[k][l]: continue
            if c[k][l] == 0: continue
            dfs(k, l)


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    c = [list(map(int, input().split())) for _ in range(h)]
    
    seen = [[0] * w for _ in range(h)]
    count = 0
    for i in range(h):
        for j in range(w):
            if not seen[i][j] and c[i][j] == 1: 
                dfs(i, j)
                count += 1

    print(count)