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: #!/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)