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

C++: #include <iostream>
using namespace std;

#define REP(i, n) for(int i = 0; i < n; i++)

const int MAX_N = 50;
int w, h; // wは横の個数 hは縦の個数
int c[MAX_N][MAX_N]; //マップ
int dx = 1, dy = 1; //移動単位
int island; //記憶

void sch(int x, int y) { //ひとつ前のx座標とy座標

	c[x][y] = 0;

	if ((x + dx < w) && (c[x + dx][y] == 1)) {
		sch(x + dx, y);
		return;
	}
	if (((x - dx >= 0) && (c[x - dx][y] == 1))) {
		sch(x - dx, y);
		return;
	}
	if ((y + dy < h) && (c[x][y + dy] == 1)) {
		sch(x, y + dy);
		return;
	}
	if ((y - dy >= 0) && (c[x][y - dy] == 1)) { 
		sch(y, y - dy); 
		return;
	}
	if (x + dx < w && y + dy < h && c[x + dx][y + dy] == 1) {
		sch(x + dx, y + dy); 
		return;
	}
	if (x - dx >= 0 && y + dy < h && c[x - dx][y + dy] == 1) {
		sch(x - dx, y + dy);
		return;
	}
	if (x + dx < w && y - dy >= 0 && c[x + dx][y - dy] == 1) {
		sch(x + dx, y - dy);
		return;
	}
	if (x - dx >= 0 && y - dy >= 0 && c[x - dx][y - dy] == 1) {
		sch(x - dx, y - dy);
		return;
	}

	island++;
	return;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while (1) {
		island = 0;
		cin >> w >> h;
		if (w == 0 && h == 0) break;

		REP(i, w) {
			REP(j, h) cin >> c[i][j];
		}

		REP(i, w) {
			REP(j, h) if (c[i][j] == 1) sch(i, j);
		}
		cout << island << "\n";
	}

	return 0;
}

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