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: #AOJ1160
def search(w,h,data):
    for i in range(h):
        if 1 in data[i]:
            return (i,data[i].index(1))
    return (-1,-1)

def main(w,h):
    data=[list(map(int,input().split())) for j in range(h)]
    start=search(w,h,data)
    island=0
    while start!=(-1,-1):
        island+=1
        checking=[start]
        data[start[0]][start[1]]=0
        while len(checking)>0:
            now=checking.pop()
            for j in range(now[0]-1,now[0]+2):
                for k in range(now[1]-1,now[1]+2):
                    if (j,k)!=now and j>=0 and k>=0 and j<h and k<w:
                        if data[j][k]==1:
                            data[j][k]=0
                            checking.append((j,k))
        start=search(w,h,data)
    return island          

w,h=map(int,input().split())
while w!=0 and h!=0:
    print(main(w,h))
    w,h=map(int,input().split())

Python: # -*- coding: utf-8 -*-
def dfs(x,y):
    c[x][y] = 0
    for dx in [-1,0,1]:
        for dy in [-1,0,1]:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
                dfs(nx, ny)

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

    print(count)

Python: # AOJ 1160: How Many Islands?
# Python3 2018.7.15 bal4u

# UNION-FIND library
class UnionSet:
	def __init__(self, nmax):
		self.size = [1]*nmax
		self.id = [i for i in range(nmax+1)]
	def root(self, i):
		while i != self.id[i]:
			self.id[i] = self.id[self.id[i]]
			i = self.id[i]
		return i
	def connected(self, p, q): return self.root(p) == self.root(q)
	def unite(self, p, q):
		i, j = self.root(p), self.root(q)
		if i == j: return
		if self.size[i] < self.size[j]:
			self.id[i] = j
			self.size[j] += self.size[i]
		else:
			self.id[j] = i
			self.size[i] += self.size[j]
# UNION-FIND library

def id(r, c):
	global w
	return r*w+c

mv = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]

while True:
	w, h = map(int, input().split())
	if w == 0: break
	arr = [[] for r in range(h)]
	u = UnionSet(w*h)
	for r in range(h): arr[r] = list(map(int, input().split()))
	for r in range(h):
		for c in range(w):
			if not arr[r][c]: continue
			for i in range(8):
				r2, c2 = r + mv[i][0], c + mv[i][1]
				if r2 < 0 or r2 >= h or c2 < 0 or c2 >= w or not arr[r2][c2]: continue
				u.unite(id(r, c), id(r2, c2))
	ans = 0
	for r in range(h):
		for c in range(w):
			if arr[r][c] and u.root(id(r, c)) == id(r, c): ans += 1
	print(ans)

Python: from itertools import product
while True:
    w, h = map(int, input().split())
    if w == 0:
        break
    a = [[0]*(w+2)] + [[0]+list(map(int, input().split()))+[0] for _ in [0]*h] + [[0]*(w+2)]
    visited = [[0]*(w+2) for _ in [0]*(h+2)]
    ans = 0
    for y in range(1, h+1):
        for x in range(1, w+1):
            if visited[y][x] or not a[y][x]:
                continue
            ans += 1
            visited[y][x] = 1
            q = [(x, y)]
            pop, append = q.pop, q.append
            while q:
                _x, _y = pop()
                for dx, dy in product(range(-1, 2), range(-1, 2)):
                    nx, ny = _x+dx, _y+dy
                    if not visited[ny][nx] and a[ny][nx]:
                        visited[ny][nx] = 1
                        append((nx, ny))
    print(ans)

Python: 
# 深さ優先探索
def dfs(sy, sx, visited, C):
    cango = [[1, 1], [1, 0], [1, -1], [0, 1], [0, -1], [-1, 1], [-1, 0], [-1, -1]]
    stack = [[sy, sx]]
    if visited[sy][sx] == 1:#きたことあるならスルー
      return 0
    visited[sy][sx] = 1
    if C[sy][sx] == 0:#もし海ならスルー
      return 0
    while stack:
      y, x = stack[-1]
      stack.pop()
      for i, j in cango:
        if (0 <= y + i < H) and (0 <= x + j < W):
          if (visited[y + i][x + j] == 0) and (C[y + i][x + j] == 1):
            visited[y + i][x + j] = 1
            stack.append([y + i, x + j])
    return 1

def dfs2(W, H, C1):
  ans = 0  
  visited = [[0] * W for i in range(H)]
  C = C1
  for i in range(H):
    for j in range(W):
      ans += dfs(i, j, visited, C)
    
  print(ans)

p = 0
while (p == 0):
#from collecitons import deque
  W, H = map(int, input().split())
  if (W, H) == (0, 0):
    p == 1
    quit()
  C1 = [0] * H
  for i in range(H):
    C1[i] = list(map(int, input().split()))
  dfs2(W, H, C1)