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 collections import deque

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')

def land_check(y,x,W):
    tansaku = deque()
    tansaku.append([y,x])
    che_land = 0
    while tansaku:
        l_y,l_x = tansaku.pop()
        if l_x in visited_land[l_y]:
            continue
        for i in range(l_x-1,l_x+2):
                for j in range(l_y-1,l_y+2):
                    if 0 <= i <= w-1 and 0 <= j <= h-1  and W[j][i] == 1:
                        tansaku.append([j,i])
        visited_land[l_y].add(l_x)
        che_land += 1
    return che_land


def answer(h,w):

    res = 0
    ans = 0
    for i in range(h):
        for j in range(w):
            if tizu[i][j] == 0 or j in visited_land[i]:
                continue
            res += land_check(i,j,tizu)
            ans += 1
            if res == land_count:
                break
        else:
            continue
        break
    return ans

if __name__ == '__main__':
    kotae = []
    while True:
        w,h = nm()
        if [h,w] == [0,0]:
            break
        visited_land = {i:set() for i in range(h)}
        tizu = []
        land_count = 0
        for i in range(h):
            temp = nl()
            land_count += temp.count(1)
            tizu.append(temp)
        kotae.append(answer(h,w))
    [print(i) for i in kotae]

Python: def solve(H, W, C):
	cnt = 0
	d = [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]

	while True:
		start = [-1,-1]
		for i in range(H):
			for j in range(W):
				if C[i][j] == 1:
					start = [i,j]
					break

		if start == [-1,-1]:
			return cnt
		else:
			cnt += 1
			q = [start]


		while q:
			cx,cy = q.pop()
			C[cx][cy] = 0
			for dx,dy in d:
				nx,ny = cx+dx, cy+dy
				if 0 <= nx < H and 0 <= ny < W:
					if C[nx][ny] == 1:
						q.append([nx,ny])


ans = []
while True:
	W,H = map(int, input().split())
	if H == 0 and W == 0:
		break
	C = [list(map(int, input().split())) for _ in range(H)]
	#print(H,W,C)
	ans.append(solve(H,W,C))




for a in ans:
	print(a)

Python: def dfs(field, i, j, w, h):
    field[i][j] = "0"
    
    OFFSETS = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
    for di, dj in OFFSETS:
        if i+di < 0 or i+di >= h:
            continue
        if j+dj < 0 or j+dj >= w:
            continue
        if field[i+di][j+dj] == "1":
            dfs(field, i+di, j+dj, w, h)


def main():
    while True:
        w, h = [int(s) for s in input().strip().split()]

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

        num_island = 0
    
        field = []
        for _ in range(h):
            field.append(input().strip().split())
       
        #print(field) 
        for i in range(h):
            for j in range(w):
                if field[i][j] == "1":
                    num_island += 1
                    dfs(field, i, j, w, h)
        
        print(num_island)


if __name__ == "__main__":
    main()

Python: from collections import deque
while True:
    W,H = map(int,input().split())
    if W == 0: break
    src = [list(map(int,input().split())) for i in range(H)]
    visited = [[0 for x in range(W)] for y in range(H)]
    dxy = [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]
    ans = 0
    for y in range(H):
        for x in range(W):
            if visited[y][x] or not(src[y][x]): continue
            ans += 1
            visited[y][x] = 1
            q = deque([(x,y)])
            while q:
                x0,y0 = q.popleft()
                for dx,dy in dxy:
                    nx,ny = x0+dx, y0+dy
                    if not(0 <= nx < W) or not(0 <= ny < H): continue
                    if visited[ny][nx] or not(src[ny][nx]): continue
                    visited[ny][nx] = 1
                    q.append((nx,ny))
    print(ans)

Python: import sys
sys.setrecursionlimit(10000)
def f(x,y):
    global w,h,c
    if x<0 or x>=h or y<0 or y>=w or c[x][y]==0:
        return
    c[x][y]=0
    for a in [-1,0,1]:
        for b in [-1,0,1]:
            if a==0 and b==0:
                continue
            f(x+a,y+b)
while True:
    w,h = map(int,input().split())
    if w+h==0:
        break
    c = [list(map(int,input().split())) for _ in range(h)]
    ans = 0
    for x in range(h):
        for y in range(w):
            if c[x][y]==1:
                ans+=1
                f(x,y)
    print(ans)