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: from collections import deque
def dfs(G,v,h,w):
    stack = deque([v])
    connected = set([v])
    while stack:
        u = stack.popleft()
        x,y = u
        dx = [-1,0,1]
        dy = [-1,0,1]
        for i in range(3):
            for j in range(3):
                s = x + dx[i]
                t = y + dy[j]
                if 0 <= s < h and 0 <= t < w:
                    if G[s][t] == 1:
                        if (s,t) not in connected:
                            stack.appendleft((s,t))
                            connected.add((s,t))
    return connected

def solve(C,h,w):
    node = set()
    ans = 0
    for i in range(h):
      for j in range(w):
        if C[i][j] == 1 and (i,j) not in node:
          node = node|dfs(C,(i,j),h,w)
          ans += 1
    print(ans)

def main():
    h = 0
    w = 0
    while h >= 0 and w >= 0:
        w,h = map(int,input().split(" "))
        if h == w == 0:
            return
        C = [list(map(int,input().split(" "))) for _ in range(h)]
        solve(C,h,w)
  

if __name__ == "__main__":
    main()

Python: def solve():
    w, h = [int(x) for x in input().split()]
    if w == h == 0:
        return False
    field = [[int(x) for x in input().split()] for _ in range(h)]

    ans = 0
    for x in range(w):
        for y in range(h):
            if field[y][x] > 0:
                stack = []
                mark(field, w, h, x, y, stack)
                while stack:
                    p, q = stack.pop()
                    mark(field, w, h, p, q, stack)
                    
                ans += 1
    print(ans)
    return True



def mark(field, w, h, x, y, stack):
    if x < 0 or x >= w or y < 0 or y >= h:
        return

    if field[y][x] != 1:
        return

    field[y][x] = -1

    stack.append((x+1, y+1))
    stack.append((x+1, y))
    stack.append((x+1, y-1))
    stack.append((x, y+1))
    stack.append((x, y))
    stack.append((x, y-1))
    stack.append((x-1, y+1))
    stack.append((x-1, y))
    stack.append((x-1, y-1))
    

while solve():
    pass

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

    C = [list(map(int, input().split())) for i in range(H) ]

    def work(x,y):
        ax = [-1, 0, 1, -1, 1, -1, 0, 1]
        ay = [-1, -1, -1, 0, 0, 1, 1, 1]

        if C[y][x] != 1:return False
        C[y][x] = -1 #mark

        for _ax in ax:
            for _ay in ay:
                if 0 <= _ax + x < W and 0 <= _ay + y < H:
                    work(_ax + x, _ay + y)
        return True

    cnt = 0
    for x in range(W):
        for y in range(H):
            cnt += work(x,y)
    print(cnt)

Python: import sys
sys.setrecursionlimit(100000)

def dfs(h, w):
    global fields
    if not (0 <= h < len(fields) and 0 <= w < len(fields[0])):
        return
    if fields[h][w] == "0":
        return
    fields[h][w] = "0"
    for dh in [-1, 0, 1]:
        for dw in [-1, 0, 1]:
            if dh == 0 and dw == 0:
                continue
            dfs(h + dh, w + dw)

while True:
    W, H = tuple(map(int, input().split(" ")))
    if (W, H) == (0, 0):
        break
    fields = []
    for _ in range(H):
        fields.append(input().split(" "))

    ans = 0
    for h in range(H):
        for w in range(W):
            if fields[h][w] == "1":
                dfs(h, w)
                ans += 1
    print(ans)

Python: def f():
  f()
f()

Python: from sys import stdin, setrecursionlimit
input = stdin.readline

setrecursionlimit(100000)

move = ((-1,-1),(-1,0),(-1,1),(0,1),(0,-1),(1,-1),(1,0),(1,1))
def DFS(x,y):
    if mapmatrix[j][i] == 1 and not checked[y][x]:
        checked[y][x] = True
        
        for dx, dy in move:
            X = x + dx; Y = y + dy
            if X >= 0 and X < W and Y >= 0 and Y < H:
                if mapmatrix[Y][X] == 1:
                    DFS(X,Y)
    
while True:
    W,H = map(int, input().split())
    if W == 0 and H == 0:
        break
    
    mapmatrix = [None]*H
    for i in range(H):
        mapmatrix[i] = list(map(int, input().split()))
    
    checked = [[False]*W for _ in range(H)]
    
    islandcnt = 0
    for j in range(H):
        for i in range(W):
            if mapmatrix[j][i] == 1 and not checked[j][i]:
                DFS(i,j)
                islandcnt += 1
    
    print(islandcnt)

Python: import sys
sys.setrecursionlimit(10**6)

def dfs(pos):
  i = pos[0]
  j = pos[1]
  seen[j][i] = True
  for k in range(-1+i,2+i):
    for l in range(-1+j,2+j):
      if 0 <= k < w and 0 <= l < h:
        if field[l][k] == 1 and (not seen[l][k]):
          dfs([k,l])

while True:
  w, h = map(int,input().split())
  if w == 0 and h == 0:
    break
  field = [list(map(int,input().split())) for _ in range(h)]
  seen = [[False]*w for _ in range(h)]
  count = 0
  for i in range(w):
    for j in range(h):
      if field[j][i] == 1 and (not seen[j][i]):
        dfs([i,j])
        count += 1
  print(count)