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


class UnionFind:
    def __init__(self, n=0):
        self.d = [-1]*n
        self.u = n

    def root(self, x):
        if self.d[x] < 0:
            return x
        self.d[x] = self.root(self.d[x])
        return self.d[x]

    def unite(self, x, y):
        x, y = self.root(x), self.root(y)
        if x == y:
            return False
        if x > y:
            x, y = y, x
        self.d[x] += self.d[y]
        self.d[y] = x
        self.u -= 1
        return True

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.d[self.root(x)]

    def num_union(self):
        return self.u


input = sys.stdin.readline
while True:
    W, H = map(int, input()[:-1].split())
    if W == 0:
        break
    S = [input()[:-1].split() for _ in [0]*H]

    uf = UnionFind(H*W)
    cnt = 0
    for h, ss in enumerate(S):
        for w, s in enumerate(ss):
            if s == '0':
                cnt += 1
                continue

            if h and w and S[h-1][w-1] == '1':
                uf.unite(h*W+w, (h-1)*W+w-1)
            if h and S[h-1][w] == '1':
                uf.unite(h*W+w, (h-1)*W+w)
            if h and w+1<W and S[h-1][w+1] == '1':
                uf.unite(h*W+w, (h-1)*W+w+1)
            if w and S[h][w-1] == '1':
                uf.unite(h*W+w, h*W+w-1)


    print(uf.u-cnt)

Python: import queue
def allvisited():
    for i in range(w):
        for j in range(h):
            if (visited[i][j]==False and s[i][j]=='1'):
                return (False,i,j)
    else:
         return (True,-1,-1)
        
def legal(pos):
    if(0<=pos[0]<w) and (0<=pos[1]<h) and s[pos[0]][pos[1]]=='1' and visited[pos[0]][pos[1]]==False:
        return True
    else:
        return False
    
d=[[0,1],[0,-1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]]
while(1):
    h,w=map(eval,input().split())
    if (w==0 and h==0): break
    s=[]
    for i in range(w):
        s.append(input().split())
    #print(s)
    Q=queue.Queue()
    visited=[]
    for i in range(w):
        visited.append([False]*h)
    #print(visited)
    count=0
    while(1):
        a=allvisited()
        if (a[0]==False):
            Q.put((a[1],a[2]))
            count+=1
        else:
            break
        while(not Q.empty()):
            now=Q.get()
            visited[now[0]][now[1]]=True
            for i in range(8):
                new=(now[0]+d[i][0],now[1]+d[i][1])
                if(legal(new)):
                    Q.put(new)
    print(count)

Python: import sys
readline = sys.stdin.buffer.readline

from collections import deque
import copy

def myinput():
    return map(int,readline().split())

def mycol(data,col):
    return [ row[col] for row in data ]

ls_ans = []

w,h = myinput()
c = [ list(myinput()) for _ in range(h) ]

while w!=0 or h!=0:
    def dfs(sx,sy):
        stack = deque()
        stack.append([sx,sy])
        while stack:
            x,y = stack.pop()
            if c[x][y]==1:
                c[x][y] = 0
                for i in range(-1,2):
                    for j in range(-1,2):
                        if i==0 and j==0:
                            continue
                        else:
                            nx = x + i
                            ny = y + j
                            if nx==-1 or ny==-1 or nx==h or ny==w:
                                pass
                            else:
                                if c[nx][ny]==1:
                                    stack.append([nx,ny])
                                else:
                                    pass
            else:
                pass

    ans = 0
    for i in range(h):
        for j in range(w):
            if c[i][j]==1:
                dfs(i,j)
                ans += 1
            else:
                pass
    # print(ans)
    ls_ans.append(ans)

    w,h = myinput()
    c = [ list(myinput()) for _ in range(h) ]

for i in range(len(ls_ans)):
    print(ls_ans[i])

Python: import sys
sys.setrecursionlimit(1000000)


def dfs(map, reached, pos):
    global dx, dy
    x, y = pos

    if reached[x][y] == 1 or map[x][y] == '0':
        return 0

    reached[x][y] = 1
    for dxi, dyi in zip(dx, dy):
        if not reached[x + dxi][y + dyi]:
            dfs(map, reached, (x + dxi, y + dyi))

    return int(map[x][y])


def island(map):
    w = len(map)
    h = len(map[0])
    reached = [[0] * h for _ in range(w)]

    ans = 0
    for x in range(w):
        for y in range(h):
            if reached[x][y] == 1 or map[x][y] == '0':
                continue
            ans += dfs(map, reached, (x, y))

    return ans


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

while True:
    w, h = map(int, input().split())

    if (0, 0) == (w, h):
        break

    mapp = ['0' * (w + 2)]
    for _ in range(h):
        mapp.append('0%s0' % input().replace(' ', ''))
    mapp.append('0' * (w + 2))
    print(island(mapp))