Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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)