Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

Python: import sys
from collections import deque

sys.setrecursionlimit(10**5)

def solve():
    while 1:
        w, h = map(int, sys.stdin.readline().split())

        if w == h == 0:
            return

        room = [[int(j) for j in sys.stdin.readline().split()] for i in range(h)]

        cnt = 0

        for i in range(h):
            for j in range(w):
                if room[i][j]:
                    cnt += 1
                    bfs(w, h, room, i, j)

        print(cnt)

dx = (1, 0, -1, 0, 1, 1, -1, -1)
dy = (0, 1, 0, -1, 1, -1, 1, -1)

def bfs(w, h, room, i, j):
    room[i][j] = 0
    q = deque([(i, j)])

    while q:
        ci, cj = q.popleft()

        for k in range(len(dx)):
            ni = ci + dy[k]
            nj = cj + dx[k]

            if 0 <= ni < h and 0 <= nj < w and room[ni][nj]:
                room[ni][nj] = 0
                q.append((ni, nj))

    return

def dfs(w, h, room, i, j):
    if i < 0 or i >= h or j < 0 or j >= w or (not room[i][j]):
        return

    room[i][j] = 0

    dfs(w, h, room, i + 1, j)
    dfs(w, h, room, i - 1, j)
    dfs(w, h, room, i, j + 1)
    dfs(w, h, room, i, j - 1)
    dfs(w, h, room, i + 1, j + 1)
    dfs(w, h, room, i + 1, j - 1)
    dfs(w, h, room, i - 1, j + 1)
    dfs(w, h, room, i - 1, j - 1)

if __name__ == '__main__':
    solve()

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(" ")))