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: d = [-1,0,1]
def search(x,y,m):
    if not(0<=x<len(m[0]) and 0<=y<len(m)) or m[y][x]=='0': return
    m[y][x] = '0'
    for dy in d:
        for dx in d:
            search(x+dx,y+dy,m)
while True:
    w,h = map(int,input().split())
    if w==h==0: break
    m = [input().split() for _ in range(h)]
    c = 0
    for y in range(h):
        for x in range(w):
            if m[y][x]=='1':
                c += 1
                search(x,y,m)
    print(c)

Python: import sys
from itertools import product as pro
sys.setrecursionlimit(10**7)

while True:
    w,h=map(int,input().split())
    if w==0 and h==0:
        sys.exit()
    
    G=[]
    for i in range(h):
        G.append(list(map(int,input().split())))

    #隣接する島
    def sur(X):
        x=X[0]
        y=X[1]
        axis_x=[x+1,x,x-1]
        axis_y=[y+1,y,y-1]
        preE=list(pro(axis_x,axis_y))
        preE.remove((x,y))
        E=[]
        for e in preE:
            if 0<=e[0]<=h-1 and 0<=e[1]<=w-1:
                if G[e[0]][e[1]]==1:
                    E.append(e)
        return E

    check=[[False]*w for i in range(h)]

    #checkの陸埋めをしても良い
    def dfs(v):
        check[v[0]][v[1]]=True
        for next in sur(v):
            if check[next[0]][next[1]]:
                continue
            dfs(next)

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

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

w=0;h=0;
def dfs(x,y,Map):
    #ending condition
    if not (0 <= x and x < h and 0 <= y and y < w):
        return
    if Map[x][y] == 0:
        return
    
    #operation
    Map[x][y] = 0

    #moving deep
    for i in [-1,0,1]:
        for j in [-1,0,1]:
            dfs(x+i,y+j,Map)

def start_cnt():
    global w,h
    w,h = map(int,input().split())
    if w == 0 and h == 0:exit();
    cnt = 0
    Map = []
    for i in range(h):
            Map.append(list(map(int,input().split())))
    for i in range(h):
        for j in range(w):
            if Map[i][j] == 1:
                dfs(i,j,Map)
                cnt += 1
    print(cnt)
while True:
    start_cnt()

Python: import sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**6)
def dfs(a,v):
    global st,per
    if v in st: st.remove(v)
    else: return
    for tom in per:
        if a[v[0]+tom[0]][v[1]+tom[1]]==1: dfs(a,(v[0]+tom[0],v[1]+tom[1]))
while True:
    c,r=map(int,input().split())
    if c==0 and r==0 : exit(0)
    a=[[0]*(c+2)]
    for j in range(r):
        a.append([0]+[int(i) for i in input().split()]+[0])
    a.append([0]*(c+2))
    st=set()
    for i in range(len(a)):
        for j in range(len(a[i])):
            if a[i][j]==1:
                st.add((i,j))
    per=[[1,1],[1,0],[0,1],[-1,1],[1,-1],[-1,-1],[-1,0],[0,-1]]
    ans=0
    while st:
        ans+=1
        for i in st:
            dfs(a,i)
            break
    print(ans)

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

def dfs(row, column):
    Map[row][column] = num
    for i in range(-1, 2):
        for j in range(-1, 2):
            if Map[row + i][column + j] == 1:
                dfs(row + i, column + j)

while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    
    Map = [[0] * (w+2)]
    Map += [[0] + list(map(int, input().split())) + [0] for _ in range(h)]
    Map += [[0] * (w+2)]
    
    num = 1
    
    for i in range(h+2):
        for j in range(w+2):
            if Map[i][j] == 1:
                num += 1
                dfs(i,j)
    print(num-1)

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 ** 7)

direction = ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (-1, 1), (1, -1))


def dfs(y, x):
    board[y][x] = 0

    for d in direction:
        nh = y + d[1]
        nw = x + d[0]

        if nh < 0 or nh >= h or nw < 0 or nw >= w:
            continue
        if board[nh][nw] == 0:
            continue
        dfs(nh, nw)

while True:
    w, h = map(int, input().split())
    if h == 0 and w == 0:
        break
    board = [list(map(int, input().split())) for i in range(h)]

    cnt = 0
    for y in range(h):
        for x in range(w):
            if board[y][x] == 0:
                continue
            dfs(y, x)
            cnt += 1
    print(cnt)

Python: def dfs(c, x, y, w, h):
    c[y][x] = -1

    for i in range(-1, 2):
        for j in range(-1, 2):
            if i == 0 and j == 0:
                continue
            x_ = x + i
            y_ = y + j
            if 0 <= x_ < w and 0 <= y_ < h and c[y_][x_] == 1:
                dfs(c, x_, y_, w, h)

def solve(w, h):
    c = []
    ans = 0

    for _ in range(h):
        c.append(list(map(int, input().split())))

    for y in range(h):
        for x in range(w):
            if c[y][x] == 1:
                ans += 1
                dfs(c, x, y, w, h)

    print(ans)

if __name__ == "__main__":
    import sys
    sys.setrecursionlimit(10000)
    while True:
        w, h = map(int, input().split())
        if w * h == 0:
            break
        solve(w, h)