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

sys.setrecursionlimit(10000)

def dfs(pos):
    x = pos[0]
    y = pos[1]
    C[y][x] = -1
    
    for x_d, y_d in [[1,0],[1,1],[0,1],[1,-1],[-1,1],[-1,0],[-1,-1],[0,-1]]:        
        x_new = x+x_d
        y_new = y+y_d

        if not 0<=x_new<=w-1:
            continue

        if not 0<=y_new<=h-1:
            continue

        if C[y_new][x_new]!=1:
            continue

        dfs([x_new, y_new])

while True:
    w, h = map(int, input().split())
    if w==h==0:
        break
    
    C = [list(map(int, input().split())) for _ in range(h)]
    
    counter = 0
    for w_i in range(w):
        for h_i in range(h):
            
            if C[h_i][w_i]==1:
                counter += 1
                dfs([w_i,h_i])
    
    print(counter)

Python: import sys
def f(n):
  if n%100==0:
    print>>sys.stderr, n
  f(n+1)
f(1)

Python: import queue

def proceess(start_x,start_y):
    #insland = []
    #insland.append([start_x,start_y])
    new_island = queue.Queue()
    map[start_x][start_y] = 0
    new_island.put([start_x,start_y])
    while True:
        ob = new_island.get()
        x = ob[0]
        y = ob[1]
        #print("x : " + str(x) + ", y : " + str(y))

        for i in [-1,0,1]:
            for j in [-1,0,1]:
                nx = x + i
                ny = y + j

                if nx >= 0 and ny >= 0 and nx < w and ny < h:
                    #print(map[nx][ny])
                    if map[nx][ny] == 1:
                        #insland.append([nx,ny])
                        #print("nx : " + str(nx) + ", ny : " + str(ny))
                        new_island.put([nx,ny])
                        map[nx][ny] = 0

        if new_island.empty():
            break

while True:
    map = []

    tmp = input().split(" ")
    w = int(tmp[0])
    h = int(tmp[1])

    if w == 1 and h == 1:
        break


    for j in range(h):
        nova = []
        arm = input()
        for k in arm.split(" "):
            #k.rstrip('\n')
            nova.append(int(k))
        map.append(nova)

    #print(map)

    count = 0
    for i in range(w):
        for j in range(h):
            if map[i][j] == 1:
                #print("i : " + str(i) + ", j : " + str(j))
                proceess(i,j)
                count += 1

    print(count)

Python: import sys
sys.setrecursionlimit(10000)

def main():
    ans=[]
    while True:
        w,h=map(int,input().split())
        if w==0 and h==0:
            break
        dis=[-1,0,1]
        c=[[0]*(w+2)]
        for i in range(h):
            c.append([0]+list(map(int,input().split()))+[0])
        c.append([0]*(w+2))
        chk=[[0]*(w+2) for _ in range(h+2)]

        def dfs(x,y):
            chk[x][y]=1
            if x<=0 or y<=0 or x>=h+1 or y>=w+1 or c[x][y]==0:
                return
            for dx in dis:
                for dy in dis:
                    if chk[x+dx][y+dy]==0 and c[x+dx][y+dy]==1:
                        dfs(x+dx,y+dy)
            return

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

if __name__ == '__main__':
    main()

Python: from sys import stdin
from sys import setrecursionlimit
setrecursionlimit(10**9)

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

while True:
    readline=stdin.readline
    w,h=map(int,readline().split())
    if w==0 and h==0:
        break
    grid=[list(map(int,readline().split())) for _ in range(h)]

    cnt=0
    for y in range(h):
        for x in range(w):
            if grid[y][x]==1:
                dfs(y,x)
                cnt+=1

    print(cnt)

Python: import sys
sys.setrecursionlimit(10000)
def dfs(x,y):
    for i in range(-1,2):
        if 0<=x+i<a[1]:
            for j in range(-1,2):
                if 0<=y+j<a[0]:
                    if G[x+i][y+j]==1:
                        G[x+i][y+j] = 0
                        dfs(x+i,y+j)
while  True:
    a = list(map(int, input().split()))
    if a[0] == 0:
        break
    G = []
    cnt = 0
    for i in range(a[1]):
        G.append(list(map(int, input().split())))
    for i in range(a[1]):
        for j in range(a[0]):
            if G[i][j] == 1:
                cnt += 1
                dfs(i,j)
    print(cnt)