Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

Python: # -*- coding: utf-8 -*-
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 < h and 0 <= ny < w and c[nx][ny] == 1:
                dfs(nx, ny)

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

    print(count)

Python: import os

import itertools
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


def solve(H, W):
    C = [list(map(int, sys.stdin.readline().split())) for _ in range(H)]

    def ok(h, w):
        return 0 <= h < H and 0 <= w < W

    def dfs(h, w):
        C[h][w] = 0
        for dh, dw in itertools.product([-1, 0, 1], repeat=2):
            if ok(h + dh, w + dw) and C[h + dh][w + dw] == 1:
                dfs(h + dh, w + dw)

    ans = 0
    for i in range(H):
        for j in range(W):
            if C[i][j] == 1:
                dfs(i, j)
                ans += 1
    return ans


while True:
    W, H = list(map(int, sys.stdin.readline().split()))
    if W == H == 0:
        break
    print(solve(H, W))

Python: import sys
sys.setrecursionlimit(10000)
def fill(y,x):
    global w,h,l
    if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
        l[y][x+1]=0
        fill(y,x+1)
    if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
        l[y][x-1]=0
        fill(y,x-1)
    if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
        l[y+1][x]=0
        fill(y+1,x)
    if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
        l[y-1][x]=0
        fill(y-1,x)
    
    if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
        l[y+1][x+1]=0
        fill(y+1,x+1)
    if 0<=y-1<h and 0<=x+1<w and l[y-1][x+1]==1:
        l[y-1][x+1]=0
        fill(y-1,x+1)
    if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
        l[y+1][x-1]=0
        fill(y+1,x-1)
    if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
        l[y-1][x-1]=0
        fill(y-1,x-1)
    
while 1:
    w,h=map(int,input().split())
    if not w and not h:break
    l=[map(int,input().split()) for _ in range(h)]

    cnt=0
    while 1:
        cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
        if not cds:
            break
        else:
            y,x=cds[0]
        l[y][x]=0
        fill(y,x)
        cnt+=1
    print(cnt)

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

import sys

import numpy as np

sys.setswitchinterval(10**8)


next_s = ((-1, 1), (0, 1), (1, 1),
          (-1, 0), (1, 0),
          (-1, -1), (0, -1), (1, -1))


def DFS(x, y):
    if not (0 <= x < W and 0 <= y < H):
        return
    if not map_[y, x]:
        return

    map_[y, x] = 0

    for dx, dy in next_s:
        DFS(x + dx, y + dy)


while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break

    map_ = np.array([list(map(int, input().split())) for h in range(H)])
    # print(map_)
    island_num = 0
    while np.amax(map_):
        x = np.argmax(map_) % W
        y = np.argmax(map_) // W
        # print(map_)
        # print("x: {} | y: {}".format(x, y))
        DFS(x, y)
        island_num += 1

    print(island_num)

Python: class Union_find():

    def __init__(self, num):
        self.num = num
        self.par = list(range(num))
        self.rank = [0]*num

    def find(self, x):
        px = self.par[x]
        if px == x:
            return x
        else:
            rx = self.find(px)
            self.par[x] = rx
            return rx

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] > self.rank[y]:
                self.par[y] = x
            else:
                self.par[x] = y
                if self.rank[x] == self.rank[y]:
                    self.rank[y] = self.rank[y] + 1
        return self

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

class Group_map():
    #dx, dyは原点対象となる要素を省く
    def __init__(self, H, W, dx, dy, Mat):
        self.H = H
        self.W = W
        self.dx = dx
        self.dy = dy
        self.Mat = Mat
        self.group = Union_find(H*W)

    def to_num(self, i, j):
        return i*(self.W) + j

    def grouping(self):
        for i in range(self.H):
            for j in range(self.W):
                for k in range(len(self.dx)):
                    (ni, nj) = (i + self.dx[k], j + self.dy[k])
                    if ni in range(self.H) and nj in range(self.W) and self.Mat[i][j] == self.Mat[ni][nj]:
                        self.group.union(self.to_num(i, j), self.to_num(ni, nj))
        return self

    def num_group(self, k):
        self.grouping()
        l = set()
        for i in range(self.H):
            for j in range(self.W):
                if self.Mat[i][j] == k:
                    l.add(self.group.find(self.to_num(i, j)))
        return len(l)

    def same(self, x, y):
        return self.group.same(self.to_num(x[0], x[1]), self.to_num(y[0], y[1]))

(w, h) = map(int, input().split(" "))
while w != 0 and h != 0:
    Map = []
    for i in range(h):
        Map.append(list(map(int, input().split(" "))))
    g = Group_map(h, w, [1, 1, 1, 0], [-1, 0, 1, 1], Map)
    print(g.num_group(1))
    (w, h) = map(int, input().split(" "))