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
sys.setrecursionlimit(10000)
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
sys.setrecursionlimit(10000)
def fill(y,x):
    global w,h,l
    if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
        l[y][x+1]=0
        fill(y,x+1)
    if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
        l[y][x-1]=0
        fill(y,x-1)
    if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
        l[y+1][x]=0
        fill(y+1,x)
    if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
        l[y-1][x]=0
        fill(y-1,x)
    
    if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
        l[y+1][x+1]=0
        fill(y+1,x+1)
    if 0<=y-1<h and 0<=x+1<w and l[y-1][x+1]==1:
        l[y-1][x+1]=0
        fill(y-1,x+1)
    if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
        l[y+1][x-1]=0
        fill(y+1,x-1)
    if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
        l[y-1][x-1]=0
        fill(y-1,x-1)
    
while 1:
    w,h=map(int,input().split())
    if not w and not h:break
    l=[list(map(int,input().split())) for _ in range(h)]

    cnt=0
    while 1:
        cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
        if not cds:
            break
        else:
            y,x=cds[0]
        l[y][x]=0
        fill(y,x)
        cnt+=1
    print(cnt)

Python: from queue import Queue
w, h = 0, 0

def bfs(x, y, data):
    que = Queue()
    que.put((x, y))

    while not que.empty():
        x, y = que.get()
        if data[x][y] == 0:
            continue

        data[x][y] = 0
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                sx, sy = x + dx, y + dy
                if sx < 0 or sx >= h or sy < 0 or sy >= w:
                    continue
                que.put((sx, sy))
    return data


while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break

    data = []
    for i in range(h):
        data.append(list(map(int, input().split())))

    ans = 0
    for x in range(h):
        for y in range(w):
            if data[x][y] > 0:
                ans += 1
                data = bfs(x, y, data)
    print (ans)

Python: import sys

sys.setrecursionlimit(10000)

def check_island(h,w):
	#print(h,w)
	for i in [-1,0,1]:
		for j in [-1,0,1]:
			if (H>(h+i)>=0) and (W>(w+j)>=0) and (c[h+i][w+j]==1):
				c[h+i][w+j] = 0
				check_island(h+i, w+j) 

while(True):
	W, H = map(int,input().split())
	if W==0 and H == 0 : break
	c = [list(int(i) for i in input().split()) for i in range(H)]
	count = 0
	for i in range(H):
		for j in range(W):
			if c[i][j]==1:
				count+=1
				check_island(i,j)
	print(count)

Python: import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

out = []
while True:
    W, H = map(int,input().split())
    if W == 0 and H == 0:
        break
    G = [list(map(int,input().split())) for _ in range(H)]
    visited = [[False]*W for _ in range(H)]
    ans = 0
    def dfs(h, w):
        visited[h][w] = True
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                ww, hh = w+dx, h+dy
                if not 0 <= ww < W: continue
                if not 0 <= hh < H: continue
                if G[hh][ww] == 0 or visited[hh][ww]: continue
                dfs(hh, ww)
    for h in range(H):
        for w in range(W):
            if G[h][w] == 0 or visited[h][w]: continue
            ans += 1
            dfs(h, w)
    out.append(ans)
print(*out, sep="\n")

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

import array
import itertools
import sys


class RecursiveDepthFirstSearch(object):

    def __init__(self, width, height, stage):
        self.width = width
        self.height = height
        self.stage = stage

    def search(self, x0, y0):
        if x0 < 0 or self.width <= x0 or y0 < 0 or self.height <= y0:
            pass
        elif self.stage[x0][y0] == 0:
            pass
        else:
            self.stage[x0][y0] = 0
            for dx, dy in itertools.product(range(-1, 2), range(-1, 2)):
                if dx != 0 or dy != 0:
                    self.search(x0 + dx, y0 + dy)


if __name__ == "__main__":
    sys.setrecursionlimit(10000)
    while True:
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break
        else:
            stage = [array.array("B", x) for x in zip(
                *[list(map(int, input().split())) for y in range(h)])]
            dfs = RecursiveDepthFirstSearch(w, h, stage)
            answer = 0
            for x, y in itertools.product(range(w), range(h)):
                if dfs.stage[x][y] == 1:
                    dfs.search(x, y)
                    answer += 1
            print(answer)