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: h,w=1,1

count=0
anser=0

def search(x,y):
    #マップの外側か壁の場合は何もしない
    #print(x,y)

    global count
    count+=1
    if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
 
        return
        
    
    #以前に到達していたら何もしない
    if reached[y][x] ==0:
         return

    reached[y][x]=0
    
    #print("aa")



    search(x+1,y)#右
    search(x-1,y)#左
    search(x,y+1)#上
    search(x,y-1)#下
    search(x-1,y-1)
    search(x-1,y+1)
    search(x+1,y-1)
    search(x+1,y+1)



while h!=0 and w!=0: 
    anser=0
    count=1
    #グラフの入力方法------

    w,h=map(int ,raw_input().split())
    if h==0 and w==0:
        break

    map_data = [[0]*w for _ in range(h)]
    reached = [[1]*w for _ in range(h)]

    for i in range(h):
        ct = raw_input().split()
       
        for j in range(w):
            map_data[i][j] = int(ct[j])
            reached [i][j]=int(ct[j])
            if ct[j]==1:
                sx=j
                sy=i

    t=0
    a=0
    while a<=h:
       if 1 in reached[t%h]:         
            search(reached[t%h].index(1),t%h)
            anser+=1
            #print("count",count)
            count=1
            a=0
       elif count==h:
                break
       t+=1
       a+=1

    print(anser)

Python: import sys
import copy
sys.setrecursionlimit(10**7)
dx = [1,0,-1,0,1,1,-1,-1]
dy = [0,1,0,-1,1,-1,1,-1]
def dfs(MAP,w,h,x,y):
    MAP[y][x] = 0
    for dir in range(8):
        nx = x + dx[dir]
        ny = y + dy[dir]
        if nx < 0 or nx >= w or ny < 0 or ny >= h:
            continue
        if MAP[ny][nx] == 0:
            continue
        dfs(MAP,w,h,nx,ny)
ans = []
bool = True
while(bool):
    w,h = map(int,input().split())
    if w == 0 and h == 0:
        bool = False
        break
    c = [list(map(int,input().split())) for _ in range(h)]
    cnt = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1:
                cnt += 1
                dfs(c,w,h,j,i)
    ans.append(str(cnt))
for i in range(len(ans)):
    print(ans[i])

Python: #!python
# -*- coding: utf-8 -*-

import sys

sys.setswitchinterval(10**9)


next_s = ((-1, 1), (0, 1), (1, 1),
          (-1, 0), (1, 0),
          (-1, -1), (0, -1), (1, -1))


def DFS(x, y):
    if not (0 <= x < W and 0 <= y < H):
        return
    if map_[y][x] == 0:
        return

    map_[y][x] = 0

    for dx, dy in next_s:
        DFS(x + dx, y + dy)


while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    map_ = [list(map(int, input().split())) for h in range(H)]
    island_num = 0
    for x in range(W):
        for y in range(H):
            if map_[y][x] == 1:
                DFS(x, y)
                island_num += 1
    print(island_num)

Python: def dfs():
    while len(stack) > 0:
        height, width = stack.pop()
        for k in range(-1, 2):
            for l in range(-1, 2):
                new_h, new_w = height + k, width + l
                if new_h < 0 or new_w < 0 or new_h >= h or new_w >= w:
                    continue
                #訪れていない場所で，そこが島だったら
                if visited[new_h][new_w] == 0 and c[new_h][new_w] == 1:
                    visited[new_h][new_w]  = 1
                    stack.append([new_h, new_w])

    return 1

#実行部分
while True:
    w, h = map(int, input().split())
    #入力が終わるとき
    if w == 0 and h == 0:
        break
    else:
        c = [list(map(int, input().split())) for _ in range(h)]
        visited = [[0] * w for _ in range(h)]
        ans = 0

        #実行部分
        for i in range(h):
            for j in range(w):
                if visited[i][j] == 0 and c[i][j] == 1:
                    visited[i][j] = 1
                    stack = [[i, j]]
                    ans += dfs()
        print(ans)

Python: import sys
sys.setrecursionlimit(10 ** 9)
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 = [[False for _ in range(w)] for _ in range(h)]

    def dfs(x, y, seen):
        if not (0 <= x < h and 0 <= y < w):
            return
        if seen[x][y]:
            return
        if c[x][y] == 0:
            return
        seen[x][y] = True
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                dfs(x + dx, y + dy, seen)

        return seen

    cnt = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1 and seen[i][j] == False:
                cnt += 1
                dfs(i, j, seen)

    print(cnt)

Python: import sys
sys.setrecursionlimit(10 ** 6)
readline = sys.stdin.readline
directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]

class Ans():

    def __init__(self, H, W, mat):
        self.H = H
        self.W = W
        self.mat = mat
        self.visited = [[False] * W for _ in range(H)]

    def dfs(self, h, w):
        if self.mat[h][w] == 0 or self.visited[h][w] == True:
            return
        
        self.visited[h][w] = True
        for dh, dw in directions:
            if not (0 <= h + dh < self.H and 0 <= w + dw < self.W):
                continue
            if self.mat[h+dh][w+dw] == 0 or self.visited[h+dh][w+dw] == True:
                continue
            self.dfs(h+dh, w+dw)
        return

def main():
    while 1:
        W, H = map(int, readline().rstrip().split())
        if W == H == 0:
            break

        mat = [list(map(int, readline().rstrip().split())) for _ in range(H)]
        ans = Ans(H, W, mat)

        res = 0
        for h in range(H):
            for w in range(W):
                if ans.mat[h][w] == 1 and ans.visited[h][w] == False:
                    res += 1
                ans.dfs(h, w)

        print(res)

if __name__ == '__main__':
    main()