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 collections,itertools,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
while 1:
    w,h = LI()
    if w==h==0:
        break
    ans = 0
    c = [LI() for _ in range(h)]
    seen = [[0]*w for _ in range(h)]
    stack = []
    dhdw = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
    def dfs(H,W):
        seen[H][W] = 1
        stack.append([H,W])
        while stack:
            sh,sw = stack.pop()
            for dh,dw in dhdw:
                nh,nw = sh+dh,sw+dw
                if not(0<=nh<=h-1) or not(0<=nw<=w-1) or seen[nh][nw] or c[nh][nw]==0:
                    continue
                seen[nh][nw] = 1
                stack.append([nh,nw])
    for H,W in itertools.product(range(h),range(w)):
        if seen[H][W] or c[H][W]==0:
            continue
        ans += 1
        dfs(H,W)
    print(ans)

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

import sys
sys.setrecursionlimit(10**6)

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)
def dfs(i, j):
    if(c[i][j] != "1"):
        return 1
    else:
        c[i][j] = "2"

        if(0 < i):                      dfs(i - 1, j)
        if(0 < j):                      dfs(i, j - 1)
        if(i < h - 1):                  dfs(i + 1, j)
        if(j < w - 1):                  dfs(i, j + 1)
        if(0 < i and 0 < j):            dfs(i - 1, j - 1)
        if(0 < i and j < w - 1):        dfs(i - 1, j + 1)
        if(i < h - 1 and 0 < j):        dfs(i + 1, j - 1)
        if(i < h - 1 and j < w - 1):    dfs(i + 1, j + 1)
        return 1

ans=[]
while(True):
    islands=0
    w, h = map(int, input().split())
    if(w == 0 and h == 0):
        break
    c = []
    for i in range(h):
        c.append(input().split())
    for i in range(h):
        for j in range(w):
            if(c[i][j] == "1"):
                islands += dfs(i, j)
    ans.append(islands)

for i in range(len(ans)):
    print(ans[i])

Python: import sys

def landsearch(i,j):
    result=[]
    if i>0:
        if j>0:
            for x in range(-1,2):
                for y in range(-1,2):
                    try:
                        if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                            result.append([i+x,j+y])
                    except:
                        pass
        else:
            for x in range(-1,2):
                for y in range(2):
                    try:
                        if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                            result.append([i+x,j+y])
                    except:
                        pass
    elif j>0:
        for x in range(2):
            for y in range(-1,2):
                try:
                    if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                        result.append([i+x,j+y])
                except:
                    pass
    else:
        for x in range(2):
            for y in range(2):
                try:
                    if (not (x==0 and y==0)) and atlas[i+x][j+y][0]==1:
                        result.append([i+x,j+y])
                except:
                    pass
    return result           


while True:
    w,h=[int(i) for i in input().split(" ")]
    if w==0 and h==0:
        sys.exit()


    atlas=[]
    for i in range(h):
        atlas.append([[int(j),0] for j in input().split(" ")])


    how_many_island=0
    for i in range(h):
        for j in range(w):
            if atlas[i][j][0]==1 and atlas[i][j][1]==0:
                how_many_island+=1
                dis_list=[[i,j]]
                while len(dis_list)!=0:
                    dis_list_cash=[]
                    for k in dis_list:
                        land=landsearch(k[0],k[1])
                        for l in land:
                            if atlas[l[0]][l[1]][1]==0:
                                atlas[l[0]][l[1]][1]=1
                                dis_list_cash.append([l[0],l[1]])
                    dis_list=dis_list_cash

    print(how_many_island)

Python: def solver():
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        return None
    islands = set()
    counter = 2
    c = [[0] + [int(x) for x in input().split()] + [0]  for _ in range(h)]
    c = [[0 for _ in range(w+2)]] + c + [[0 for _ in range(w+2)]]
    for i in range(1, h+1):
        for j in range(1, w+1):
            if c[i][j] > 0:
                neighbour = {c[i-1][j-1], c[i-1][j], c[i-1][j+1], c[i][j-1]}
                if max(neighbour) > 1:
                    c[i][j] = max(neighbour)
                    if len(neighbour - {0}) > 1:
                        islands -= {max(neighbour)}
                else:
                    c[i][j] = counter
                    islands |= {counter}
                    counter += 1
    for row in c:
        print(row)
    print(islands)
    return len(islands)

outputs = []
while(True):
    ans = solver()
    if ans == None:
        break
    outputs.append(ans)
print('\n'.join(map(str, outputs)))