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 erase(w, h, x, y):
    c[y][x] = '0'
    for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
        nx, ny = x + dx, y + dy
        if 0 <= nx <= w -1 and 0 <= ny <= h - 1:
            if c[ny][nx] == '1':
                erase(w, h, nx, ny)

while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [[i for i in input().split()] for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == '1':
                ans += 1
                erase(w, h, x, y)
    print(ans)

Python: # -*- coding: utf-8 -*-

w, h = map(int,input().split())
c = [list(map(int,input().split())) for j in range(h)]
count = 0

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

print(count)

Python: k = [-1,0,1]
def search(x,y,m):
    if x<0 or x>=len(m[0]) or y<0 or y>=len(m): return
    if m[y][x]==0: return
    m[y][x] = 0
    for i in k:
        for j in k:
            search(x+i,y+j,m)

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

Python: import sys
from collections import deque
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]


def main():
    move = [[i, j] for i in range(-1,2) for j in range(-1,2) if not(i==j and i == 0)]
    try:
        while True:
            w, h = MI()
            if w == h and h == 0:
                sys.exit()
            d = Init(h, w, True)
            mylist = [LI() for i in range(h)]
            result = 0
            for i in range(h):
                for j in range(w):
                    if d[i][j] and mylist[i][j] == 1:
                        result+=1
                        dq = deque()
                        dq.append([i, j])
                        while dq:
                            x, y = dq.pop()
                            for x2, y2 in move:
                                x2 += x
                                y2 += y
                                if x2 >= 0 and x2 < h and y2 >= 0 and y2 < w:
                                    if mylist[x2][y2] == 1 and d[x2][y2]:
                                        dq.append([x2,y2])
                                        d[x2][y2] = False
                                    elif d[x2][y2]:
                                        d[x2][y2] = False
            print(result)
    except:
        sys.exit()
    
    
    
if __name__ == "__main__":
    main()

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

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

while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [[i for i in input().split()] for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == '1':
                ans += 1
                erase(w, h, x, y)
    print(ans)