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)
		}
	}
}

// -----------------------------------------------------------------------------

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define rrep(i, a, b) for (int i = (int)(b) - 1; i >= ((int)a); --i)
#define sz(c) ((int)c.size())
#define all(c) c.begin(),c.end()

using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

void dump_impl(string str) {}
template <class H, class... T>
void dump_impl(string s, const H &head, const T&... tail) {
    int par = 0;
    rep(i, 0, sz(s)) {
        char ch = s[i];
        if (ch == ',' && par == 0) {
            cerr << " = " << head << ", ";
            dump_impl(s.substr(i + 1), tail...);
            return;
        } else {
            cerr << ch;
            if (ch == '(') par++;
            if (ch == ')') par--;
        }
    }
}
#define dump(...) do { cerr << "\x1b[33;1m"; dump_impl(#__VA_ARGS__ ",", __VA_ARGS__); cerr << "\x1b[0m" << endl; } while (0)

signed main() {
    int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
    int dy[] = {-1, 0, 1, 0, -1, 1, 1, -1};
    while (1) {
        int W, H;
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        vvi A(H, vi(W));
        rep(i, 0, H) {
            rep(k, 0, W) {
                cin >> A[i][k];
            }
        }
        vector<vector<bool>> vis(H, vector<bool>(W));
        int ans = 0;
        rep(y, 0, H) {
            rep(x, 0, W) {
                if (vis[y][x]) continue;
                if (A[y][x] == 0) continue;
                queue<pii> q;
                q.push({y, x});
                vis[y][x] = true;
                while (sz(q)) {
                    int py, px;
                    tie(py, px) = q.front(); q.pop();
                    rep(dir, 0, 8) {
                        int yy = py + dy[dir];
                        int xx = px + dx[dir];
                        if (yy < 0 || xx < 0 || yy >= H || xx >= W) continue;
                        if (vis[yy][xx]) continue;
                        if (A[yy][xx] == 0) continue;
                        q.push({yy, xx});
                        vis[yy][xx] = true;
                    }
                }
                ans++;
            }
        }
        cout << ans << endl;
    }
}

C++: #include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <queue>
#include <string>
#include <complex>

#define REP(i,n) for (int i = 0; i < (n); i++)
#define FOR(i,s,n) for (int i = (s); i < (n); i++)
#define ALLOF(c) c.begin(), c.end()
#define RANGEOF(c,i,n) c.begin() + (i), c.begin() + (i) + (n)
#define EPS 1e-9
#define INF 1000000000
using namespace std;

template <class T> T read() { T t; cin >> t; return t; }
template <class T> vector<T> read(int n) { vector<T> v; REP(i,n) { v.push_back(read<T>()); } return v; }
template <class T> vector<vector<T>> read(int n, int m)
{
	vector<vector<T>> v;
	REP(i,n) { v.push_back(read<T>(m)); }
	return v;
}

template <class T> void write(const T &t) { cout << t << endl; }
template <class T> void write(const T &t, const T &t2) { cout << t << ' ' << t2 << endl; }
template <class T> void write(const vector<T> &v)
{
	ostringstream ss; 
	for (auto x : v) ss << x << ' ';
	auto s = ss.str();
	cout << s.substr(0, s.length() - 1) << endl;
}

struct _Reader { template <class T> _Reader operator ,(T &rhs) { cin >> rhs; return *this; } };
#define READ(t,...) t __VA_ARGS__; _Reader(), __VA_ARGS__

void solve();
int main() { solve(); return 0; }

/// template end ///



void solve()
{
	while (true)
	{
		READ(int, H, W);
		if (!(W|H)) { break; }
		auto field = read<char>(W, H);
		for (auto &f : field) { f.insert(f.begin(), '0'); f.push_back('0'); }
		field.insert(field.begin(), vector<char>(H+2, '0'));
		field.push_back(vector<char>(H+2, '0'));
		W += 2; H += 2;

		int count = 0;
		REP(sx,W) REP(sy,H) if (field[sx][sy] == '1')
		{
			typedef complex<int> P;
			queue<P> q;
			q.push(P(sx, sy));
			while (!q.empty())
			{
				P cur = q.front(); q.pop();
				if (field[cur.real()][cur.imag()] == '0') { continue; }
				field[cur.real()][cur.imag()] = '0';
				P d[] = { P(-1,-1), P(0,-1), P(1,-1), P(-1, 0), P(1, 0), P(-1,1), P(0,1), P(1,1) };
				REP(i, 8)
				{
					P next = cur + d[i];
					if (field[next.real()][next.imag()] == '1') q.push(next);
				}
			}
			count++;
		}
		write(count);
	}
}