Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <limits>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
using ll = long long;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
#define reps(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep(i, N) reps(i, 0, N-1)
#define deps(i, E, S) for (ll i = (E); i >= (S); i--)
#define dep(i, N) deps(i, N-1, 0)
const ll INF = 1LL << 60;
const int INF_INT = 1 << 30;

template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; }return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; }return false; }
struct mll {
	static ll MOD;
	ll val;
	mll(ll v = 0) : val(v % MOD) { if (val < 0) val += MOD; }
	mll operator - () const { return -val; }
	mll operator + (const mll &b) const { return val + b.val; }
	mll operator - (const mll &b) const { return val - b.val; }
	mll operator * (const mll &b) const { return val * b.val; }
	mll operator / (const mll &b) const { return mll(*this) /= b; }
	mll operator + (ll b) const { return *this + mll(b); }
	mll operator - (ll b) const { return *this - mll(b); }
	mll operator * (ll b) const { return *this * mll(b); }
	friend mll operator + (ll a, const mll &b) { return b + a; }
	friend mll operator - (ll a, const mll &b) { return -b + a; }
	friend mll operator * (ll a, const mll &b) { return b * a; }
	mll &operator += (const mll &b) { val = (val + b.val) % MOD; return *this; }
	mll &operator -= (const mll &b) { val = (val + MOD - b.val) % MOD; return *this; }
	mll &operator *= (const mll &b) { val = (val*b.val) % MOD; return *this; }
	mll &operator /= (const mll &b) {
		ll c = b.val, d = MOD, u = 1, v = 0;
		while (d) {
			ll t = c / d;
			c -= t * d; swap(c, d);
			u -= t * v; swap(u, v);
		}
		val = val * u % MOD;
		if (val < 0) val += MOD;
		return *this;
	}
	mll &operator += (ll b) { return *this += mll(b); }
	mll &operator -= (ll b) { return *this -= mll(b); }
	mll &operator *= (ll b) { return *this *= mll(b); }
	mll &operator /= (ll b) { return *this /= mll(b); }
	bool operator == (const mll &b) { return val == b.val; }
	bool operator != (const mll &b) { return val != b.val; }
	bool operator == (ll b) { return *this == mll(b); }
	bool operator != (ll b) { return *this != mll(b); }
	friend bool operator == (ll a, const mll &b) { return mll(a) == b.val; }
	friend bool operator != (ll a, const mll &b) { return mll(a) != b.val; }
	friend ostream &operator << (ostream &os, const mll &a) { return os << a.val; }
	friend istream &operator >> (istream &is, mll &a) { return is >> a.val; }
	static mll Combination(ll a, ll b) {
		chmin(b, a - b);
		if (b < 0) return mll(0);
		mll c = 1;
		rep(i, b) c *= a - i;
		rep(i, b) c /= i + 1;
		return c;
	}
};
using vmll = std::vector<mll>;
using vvmll = std::vector<vmll>;
using vvvmll = std::vector<vvmll>;
using vvvvmll = std::vector<vvvmll>;

struct Fast {
	Fast() {
		cin.tie(0);
		ios::sync_with_stdio(false);
		cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
	}
} fast; //cin,cout高速化のおまじない＋桁数指定

ll mll::MOD = (ll)(1e9 + 7);// 998244353ll;


vvll g; //グリッドグラフ
vvll f; //発見済み
ll W;
ll H;
void DFS(ll y, ll x) {
	reps(j, -1, 1) reps(i, -1, 1) {
		ll v = y + j;
		ll u = x + i;
		if (v < 0 || u < 0 || v >= H || u >= W) {
			continue;
		}

		if (g[v][u] == 0 || f[v][u] > 0) {
			continue;
		}

		f[v][u] = 1;
		DFS(v, u);
	}
}

int main() {
	while (true) {
		cin >> W;
		cin >> H;

		if (W == 0 && H == 0) {
			break;
		}

		g.clear();
		f.clear();
		g.resize(H);
		f.resize(H);
		rep(i, H) {
			g[i].resize(W);
			f[i].resize(W, 0);
		}
		rep(j, H) rep(i, W) {
			cin >> g[j][i];
		}

		ll result = 0;

		rep(j, H) rep(i, W) {
			if (g[j][i] > 0 && f[j][i] == 0) {
				result++;
				f[j][i] = 1;
				DFS(j, i);
			}
		}

		cout << result << endl;
	}

	return 0;
}

C++: #include <iostream>

#define MAX_NM 50

using namespace std;

void solve();
void dfs(int x, int y);

int w,h;
int field[MAX_NM][MAX_NM];

int main()
{
    while (1) {
        cin>>w>>h;
        if (w==0&&h==0) break;
        for (int i=0; i<h; i++) {
            for (int j=0; j<w; j++) {
                cin>>field[i][j];
            }
        }
        solve();
    }
    return 0;
}

void solve()
{
    int count=0;
    for (int i=0; i<h; i++) {
        for (int j=0; j<w; j++) {
            if (field[i][j]==1) {
                dfs(i,j);
                count++;
            }
        }
    }
    cout<<count<<endl;
}

void dfs(int x, int y)
{
    field[x][y]=0;
    
    for (int dx=-1; dx<=1; dx++) {
        for (int dy=-1; dy<=1; dy++) {
            int nx=x+dx, ny=y+dy;
            
            if (nx>=0&&nx<h&&ny>=0&&ny<w&&field[nx][ny]==1) {
                dfs(nx,ny);
            }
        }
    }
}

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

typedef pair<int, int> P;

int dx[8] = { 1,0,-1,0,1,1,-1,-1 }, dy[8] = { 0,1,0,-1,1,-1,1,-1 };

int main() {
	int w, h;
	while (cin >> w >> h, w) {
		vector<vector<int>>ma(w, vector<int>(h));
		for (int i = 0; i < h;i++) {
			for (int j = 0; j < w;j++) {
				cin >> ma[j][i];
			}
		}
		int ans = 0;
		for (int i = 0; i < w;i++) {
			for (int j = 0; j < h;j++) {
				if (ma[i][j] == 1) {
					ans++;
					queue<P> q;
					q.push(P(i, j));
					ma[i][j] = 0;
					while (!q.empty()) {
						P p = q.front(); q.pop();
						for (int k = 0; k < 8; k++) {
							int nx = p.first + dx[k], ny = p.second + dy[k];
							if (nx < w && nx>=0 && ny < h && ny>=0 && ma[nx][ny]==1) {
								ma[nx][ny] = 0; q.push(P(nx, ny));
							}
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
}