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)
def f(x,y):
    if 0<=x<w and 0<=y<h and a[y][x]=='1':
        a[y][x]='0'
        for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:f(x+dx,y+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: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

import sys
sys.setrecursionlimit(10000)


def dfs(x: int, y: int):
    """dfsで1つの地続きの島を探索
    Parameters
    ----------
        x, y : 探索対称点のxy座標
    """
    seen[x][y] = True
    for i in range(8):
        nx = x + dx[i]
        ny = y + dy[i]
        if (0 <= nx < H
                and 0 <= ny < W
                and seen[nx][ny] is False
                and chart[nx][ny] == 1):
            dfs(nx, ny)


while True:
    try:
        W, H = map(int, input().split())
        if W == 0:
            break
        else:
            chart = []
            for _ in range(H):
                chart.append(list(map(int, input().split())))
            seen = [[False] * W for _ in range(H)]
            dx = [1, 0, -1, 0, 1, 1, -1, -1]
            dy = [0, 1, 0, -1, 1, -1, -1, 1]
            ans = 0
            for i in range(H):
                for j in range(W):
                    # seen=Trueのマスは既に島としてカウントされた故それ以外を探索
                    if seen[i][j] is False and chart[i][j] == 1:
                        dfs(i, j)
                        # dfsで島1つを探索終了した故カウントをインクリメント
                        ans += 1
            print(ans)
    except ValueError:
        break

Python: 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: # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10000)


def search(n, m, tile, N, M):
    for dy, dx in zip([-1, -1, -1, 0, 0, 1, 1, 1], [-1, 0, 1, -1, 1, -1, 0, 1]):
        if 0 <= dy + n < N and 0 <= dx + m < M:
            if tile[dy + n][dx + m] == '1':
                tile[dy + n][dx + m] = '2'
                search(dy + n, dx + m, tile, N, M)


while True:
    M, N = map(int, input().split())
    if M == 0 and N == 0:
        break
    tile = [input().split() for n in range(N)]
    ans = 0
    for n in range(N):
        for m in range(M):
            if tile[n][m] == '1':
                tile[n][m] = '2'
                search(n, m, tile, N, M)
                ans += 1
    print(ans)

Python: import copy

move = ((1, 0), (0, 1), (0, -1), (-1, 0), (1, 1), (-1, -1), (-1, 1), (1, -1))


def dfs(start):
    field[start[1]][start[0]] = '0'
    CP = [start]
    while CP:
        NP = []
        for cp in CP:
            for m in move:
                n_p = (cp[0]+m[0], cp[1]+m[1])
                if not(n_p[0] < 0 or n_p[1] < 0 or n_p[1] >= h or n_p[0] >= w) and field[n_p[1]][n_p[0]] == '1':
                    field[n_p[1]][n_p[0]] = '0'
                    NP.append(n_p)
        CP = NP


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    field = [list(input().split()) for _ in range(h)]
    count = 0
    for y in range(h):
        for x in range(w):
            if field[y][x] == '1':
                dfs((x, y))
                count += 1
    print(count)

Python: #00:45
import sys

sys.setrecursionlimit(10000000)


dx = [1, 0, -1, 0,-1,-1,1,1]
dy = [0, 1, 0, -1,-1,1,-1,1]



#左上からせめて深さ優先で探索する問題
while True:
    # 地図作る
    count = 0
    width,hight = map(int,input().split())
    if width ==0 :
        break
    d =[]
    for _ in range(hight):
        d.append(list(map(int,input().split())))
    
    def dfs(x, y):
    # いまたどり着いたはずなので1に塗りつぶす
        d[y][x]=0

    #4方向ループ
    #あくまで方向の表し方dxは
        for i in range(8):
            ny = y + dx[i]
            nx = x + dy[i]

        #行ったことがなくって、迷路の範囲内で、障害物がなければ、進む
            if 0 <= ny and ny < hight \
              and 0 <= nx and nx < width \
              and d[ny][nx] == 1:
                dfs(nx,ny) 

    

    for y in range(hight):
        for x in range(width):            
            if d[y][x] == 1:
                count = count + 1
                dfs(x,y)
    print(count)