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: 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
    return len(islands)

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

Python: # Union Find
#0オリジン以下注意

#xの根を求める
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
    
def unite(x, y):
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きい方がx
        if par[x] > par[y]:
            x, y = y, x
        par[x] += par[y] #ここで集合の個数を更新している
        par[y] = x
        return True
    
#xとyが同じ集合に含まれているかどうかを判定(同じ集合にあればTrue)
def same(x, y):
    return find(x) == find(y)

#xが属する集合の個数
def size(x):
    return -par[find(x)]

#初期化
#根なら-size, 子なら親の頂点
################以下にコードを書く

while True:
    w, h = map(int, input().split())
    par = [-1]*(w*h)
    if (w, h) == (0, 0):
        break
    li = []
    for i in range(h):
        li.append(list(map(int, input().split())))
    for i in range(h):
        for j in range(w-1):
            if li[i][j] == li[i][j+1]:
                unite(w*i+j, w*i+j+1)
    for j in range(w):
        for i in range(h-1):
            if li[i][j] == li[i+1][j]:
                unite(w*i+j, w*i+j+w)
    for i in range(h):
        for j in range(w):
            I1 = i+1
            I2 = i-1
            J = j+1
            if 0 <= I1 and I1<h and 0<=J and J<w:
                if li[i][j] == 1 and li[I1][J] == 1:
                    unite(w*i+j, w*I1+J)
            if 0 <= I2 and I2<h and 0<=J and J<w:
                if li[i][j] == 1 and li[I2][J] == 1:
                    unite(w*i+j, w*I2+J)

                    
    #島を数える
    #陸のリスと
    li_riku = []
    for i in range(h):
        for j in range(w):
            if li[i][j]==1:
                li_riku.append(w*i+j)
    li_island = []
    sum_size = len(li_riku)
    for i in li_riku:
        k = find(i)
        if k not in li_island:
            li_island.append(k)
    print(len(li_island))

Python: import sys
sys.setrecursionlimit(10000)
def f(x,y):
    a[y][x]='0'
    for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:
        dx+=x;dy+=y
        if 0<=dx<w and 0<=dy<h and a[dy][dx]=='1':f(dx,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: import sys
sys.setrecursionlimit(10 ** 9)

first = True
while True:
    W, H = map(int, sys.stdin.readline().strip().split())
    if (W == 0) and (H == 0):
        break

    fp = [True] * (W * H)  # 探索可能示す

    M = []
    for h in range(H):
        m = list(map(int, sys.stdin.readline().strip().split()))
        M.append(m)
        for i, j in enumerate(m):
            if j == 0:
                fp[h * W + i] = False


    def dfs(u):  # u は座標
        x, y = u
        fp[y * W + x] = False  # 探索済みを示す

        for i in [-1, 0, 1]:
            x_next = x + i
            if (x_next < 0) or (x_next > W - 1):
                continue
            for j in [-1, 0, 1]:
                y_next = y + j
                if (y_next < 0) or (y_next > H - 1):
                    continue
                # print(f'y_next: {y_next}, x_next: {x_next}')
                # print(f'{fp[y_next * W + x_next]}')
                # print(f'{M[y][x]}')
                if fp[y_next * W + x_next] and (M[y][x] == 1):
                    dfs((x_next, y_next))


    count = 0
    while True in fp:
        count += 1
        # print(f'count : {count}')
        # print(f'fp: {fp}')

        y = int((fp.index(True) / W))
        x = int((fp.index(True) - y * W))
        # print(f'x: {x}, y: {y}')
        dfs((x, y))

    # if first:
    #     first = False
    # else:
    #     print('\n', end='')
    print(count, end='\n')