Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

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)

Python: import sys
from collections import deque

sys.setrecursionlimit(10**5)

def solve():
    while 1:
        w, h = map(int, sys.stdin.readline().split())

        if w == h == 0:
            return

        room = [[int(j) for j in sys.stdin.readline().split()] for i in range(h)]

        cnt = 0

        for i in range(h):
            for j in range(w):
                if room[i][j]:
                    cnt += 1
                    bfs(w, h, room, i, j)

        print(cnt)

dx = (1, 0, -1, 0, 1, 1, -1, -1)
dy = (0, 1, 0, -1, 1, -1, 1, -1)

def bfs(w, h, room, i, j):
    room[i][j] = 0
    q = deque([(i, j)])

    while q:
        ci, cj = q.popleft()

        for k in range(len(dx)):
            ni = ci + dy[k]
            nj = cj + dx[k]

            if 0 <= ni < h and 0 <= nj < w and room[ni][nj]:
                room[ni][nj] = 0
                q.append((ni, nj))

    return

def dfs(w, h, room, i, j):
    if i < 0 or i >= h or j < 0 or j >= w or (not room[i][j]):
        return

    room[i][j] = 0

    dfs(w, h, room, i + 1, j)
    dfs(w, h, room, i - 1, j)
    dfs(w, h, room, i, j + 1)
    dfs(w, h, room, i, j - 1)
    dfs(w, h, room, i + 1, j + 1)
    dfs(w, h, room, i + 1, j - 1)
    dfs(w, h, room, i - 1, j + 1)
    dfs(w, h, room, i - 1, j - 1)

if __name__ == '__main__':
    solve()