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 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()

Python: import sys
sys.setrecursionlimit(1000000)
moves = ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1))

def dfs(y, x):
  c[y][x] = 0

  for dy, dx in moves:
    ny = y + dy
    nx = x + dx 
    if 0 <= ny < h and 0 <= nx < w and c[ny][nx] == 1:
      dfs(ny, nx)

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)]
  ans = 0
  for i in range(h):
    for j in range(w):
      if c[i][j] == 1:
        dfs(i, j)
        ans += 1
  print(ans)

Python: import sys
sys.setrecursionlimit(100000)


def dfs(h, w):
    checked[h][w] = 1
    for hi in range(-1, 2):
        for wj in range(-1, 2):
            if 0 <= h+hi <= H-1 and 0 <= w+wj <= W-1:
                if hw[h+hi][w+wj] == 1 and checked[h+hi][w+wj] == -1:
                    dfs(h+hi, w+wj)


while True:
    W, H = list(map(int, input().split()))
    if W == 0 and H == 0:
        break

    hw = []
    for _ in range(H):
        tmp = list(map(int, input().split()))
        hw.append(tmp)

    checked = [[-1] * W for i in range(H)]
    ans = 0
    for w in range(W):
        for h in range(H):
            if hw[h][w] == 1 and checked[h][w] == -1:
                ans += 1
                dfs(h, w)
    print(ans)