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
from pprint import pprint


class UnionFind:
    def __init__(self):
        self._parent = {}
        self._rank = {}
        self._equiv = {}
    
    def _find(self, x):
        if self._parent[x] == x:
            return self._parent[x]
        else:
            p = self._find(self._parent[x])
            self._parent[x] = p
            return p
    
    def _set_parent(self, child, parent):
        self._parent[child] = parent
        self._equiv[parent].update(self._equiv.pop(child))
    
    def _union(self, x, y):
        x = self._find(x)
        y = self._find(y)
        if x == y: return
        if self._rank[x] > self._rank[y]:
            self._set_parent(y, x)
        elif  self._rank[x] < self._rank[y]:
            self._set_parent(x, y)
        else:
            self._set_parent(x, y)
            self._rank[x] += 1
    
    def _make_set(self, x):
        self._parent[x] = x
        self._rank[x] = 0
        self._equiv[x] = {x}
    
    def unite(self, *elements):
        if not elements: return
        for x in elements:
            if x in self._parent: continue
            self._make_set(x)
        x0 = elements[0]
        for x in elements[1:]:
            self._union(x0, x)
    
    def groups(self):
        return [set(e) for e in self._equiv.values()]


def solve(ncol, nrow, rows):
    pairs = []
    # ?????????
    for x in range(ncol - 1):
        for y in range(nrow):
            pairs.append([(x, y), (x + 1, y)])

    # ?????????
    for x in range(ncol):
        for y in range(nrow - 1):
            pairs.append([(x, y), (x, y + 1)])

    # ???????????????
    for x in range(ncol - 1):
        for y in range(nrow - 1):
            pairs.append([(x, y), (x + 1, y + 1)])

    # ???????????????
    for x in range(ncol - 1):
        for y in range(1, nrow):
            pairs.append([(x, y), (x + 1, y - 1)])

    f = UnionFind()
    for x in range(ncol):
        for y in range(nrow):
            if rows[y][x]:
                f.unite((x, y))
    for (x1, y1), (x2, y2) in pairs:
        if rows[y1][x1] and rows[y2][x2]:
            f.unite((x1, y1), (x2, y2))
    return len(f.groups())


def main():
    while True:
        ncol, nrow = map(int, input().split())
        if ncol == 0 and nrow == 0:
            break
        rows = []
        for _ in range(nrow):
            row = list(map(int, input().split()))
            rows.append(row)
        # print(ncol, nrow)
        # pprint(rows)
        print(solve(ncol, nrow, rows))

main()

Python: import sys

sys.setrecursionlimit(10**6)


def is_in_map(x, y, w, h):
	return 0 <= x < w and 0 <= y < h


def dfs(x, y, lct):
	lct[y][x] = 0
	w = len(lct[0])
	h = len(lct)

	for i in range(-1, 2):
		for j in range(-1, 2):
			nx = x + i
			ny = y + j
			if not is_in_map(nx, ny, w, h):
				continue
			if lct[ny][nx] == 0:
				continue
			dfs(nx, ny, lct)

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

		if w == 0 and h == 0:
			break

		ans = 0
		lct = []

		for _ in range(h):
			lct.append(list(map(int, input().split())))
		for i in range(h):
			for j in range(w):
				if lct[i][j] == 0:
					continue
				dfs(j, i, lct)
				ans += 1
		print(ans)

resolve()

Python: import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

def dfs(y,x):
    if x<0 or x>=w or y<0 or y>=h:
        return
    if li[y][x] == 0:
        return
    if li[y][x] == 1:
        li[y][x] = 0
    dfs(y,x+1)
    dfs(y,x-1)
    dfs(y+1,x)
    dfs(y-1,x)
    dfs(y+1,x+1)
    dfs(y+1,x-1)
    dfs(y-1,x+1)
    dfs(y-1,x-1)
while True:
    w,h = map(int,input().split())
    if w == 0 and h == 0:
        break
    li = []
    for i in range(h):
        c = list(map(int,input().split()))
        li.append(c)
    cnt = 0
    for i in range(h):
        for j in range(w):
            if li[i][j] == 1:
                dfs(i,j)
                cnt += 1
    print(cnt)