Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

Python: #!/usr/bin/env python3

from collections import defaultdict
import sys

sys.setrecursionlimit(2000)


class Graph(object):
    """ Graph data structure, undirected by default. """

    def __init__(self, connections, directed=False):
        self._graph = defaultdict(set)
        self._directed = directed
        self.add_connections(connections)

    def add_connections(self, connections):
        """ Add connections (list of tuple pairs) to graph """

        for u, v in connections:
            self.add(u, v)

    def add(self, u, v):
        """ Add connection between node u and node v """

        self._graph[u].add(v)
        if not self._directed:
            self._graph[v].add(u)

    def remove(self, v):
        """ Remove all references to node v """

        for _, cxns in self._graph.items():
            try:
                cxns.remove(v)
            except KeyError:
                pass
        try:
            del self._graph[v]
        except KeyError:
            pass

    def get_adj(self, v):
        """ Get adjacency list of node v """
        return self._graph[v]

    def is_connected(self, u, v):
        """ Is node u directly connected to node v """

        return u in self._graph and v in self._graph[u]

    def find_path(self, src, dst, path=[]):
        """ Find any path from src node to dst node (may not be shortest) """

        path = path + [src]
        if src == dst:
            return path
        if src not in self._graph:
            return None
        for src_next in self._graph[src]:
            if src_next not in path:
                new_path = self.find_path(src_next, dst, path)
                if new_path:
                    return new_path
        return None

    def find_all_paths(self, src, dst, path=[]):
        """ Find all paths from src node to dst node """
        path = path + [src]
        if src == dst:
            return path
        if src not in self._graph:
            return None
        paths = []
        for src_next in self._graph[src]:
            if src_next not in path:
                new_path = self.find_all_paths(src_next, dst, path)
                if new_path:
                    paths.append(new_path)
        if len(paths) > 0:
            return paths
        return None

    def __str__(self):
        return '{}({})'.format(self.__class__.__name__, dict(self._graph))


def adj_list(c, i, j):
    top, bot = [i - 1, j], [i + 1, j]
    left, right = [i, j - 1], [i, j + 1]
    top_left, top_right = [i - 1, j - 1], [i - 1, j + 1]
    bot_left, bot_right = [i + 1, j - 1], [i + 1, j + 1]
    if i == 0:
        if j == 0:
            return [right, bot, bot_right]
        elif j == w - 1:
            return [left, bot, bot_left]
        else:
            return [left, right, bot_left, bot, bot_right]
    elif i == h - 1:
        if j == 0:
            return [top, right, top_right]
        elif j == w - 1:
            return [top, left, top_left]
        else:
            return [left, right, top, top_left, top_right]
    else:
        if j == 0:
            return [top, right, top_right, bot, bot_right]
        elif j == w - 1:
            return [top, left, top_left, bot, bot_left]
        else:
            return [top, bot, left, right, top_left, top_right, bot_left, bot_right]


def is_reachable(c, i, j, i_adj, j_adj):
    if c[i][j] == 1 and c[i_adj][j_adj] == 1:
        return True
    return False


def dfs(g, v):
    seen[v] = True
    for v_next in g.get_adj(v):
        if not seen.get(v_next):
            dfs(g, v_next)


#-----
# 1 <= w, h <= 50
# c_{i, j} = 0, 1

while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    c = []
    if w == 1 and h == 1:
        c = int(input())
        if c == 1:
            print(1)
        else:
            print(0)
    else:
        for _ in range(h):
            line = list(map(int, input().split()))
            c.append(line)
        # print(h, w, c)

        g = Graph([])
        for i in range(0, h):
            for j in range(w):
                # print(f"(i, j): ({i}, {j})")
                u = (i * w) + j
                is_connected = False
                for adj in adj_list(c, i, j):
                    i_adj, j_adj = adj[0], adj[1]
                    # print(f"(i_adj, j_adj): ({i_adj}, {j_adj})")
                    if is_reachable(c, i, j, i_adj, j_adj):
                        is_connected = True
                        v = i_adj * w + j_adj
                        g.add(u, v)
                if c[i][j] == 1 and not is_connected:
                    g.add(u, u)
        # print(g)

        keys = [v for v in range(h * w)]
        values = [False for _ in range(h * w)]
        seen = dict(zip(keys, values))

        num_connected_components = 0
        for v in range(h * w):
            if not seen[v]:
                if len(g.get_adj(v)) > 0:
                    num_connected_components += 1
                dfs(g, v)

        print(num_connected_components)

Python: import collections

while True:
    w, h = map(int, input().split())
    if not w and not h:
        break
    graph = [list(map(int, input().split())) for _ in range(h)]
    seen = [[False] * w for i in range(h)]

    dy = [0, 1, 0, -1, 1, 1, -1, -1]
    dx = [1, 0, -1, 0, 1, -1, 1, -1]

    def dfs(v):
        stack = collections.deque()
        stack.append(v)
        while stack:
            y, x = stack.pop()
            for i in range(8):
                ny = y + dy[i]
                nx = x + dx[i]
                if not (-1 < ny < h and -1 < nx < w):
                    continue
                if not graph[ny][nx]:
                    continue
                if seen[ny][nx]:
                    continue
                seen[ny][nx] = True
                stack.append([ny, nx])

    count = 0
    for i in range(h):
        for j in range(w):
            if graph[i][j] and not seen[i][j]:
                dfs([i, j])
                count += 1

    print(count)