Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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)