Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

C++: #define _GLIBCXX_DEBUG
#include "bits/stdc++.h"
//#include <intrin.h>  //AtCoder (gcc) 上ではこれがあると動かない。__popcnt用のincludeファイル。
using namespace std;

typedef long long ll;
typedef long double ld;


#define int long long
#define rep(i, n) for(long long i = 0; i < (n); i++)
#define sqrt(d) pow((long double) (d), 0.50)

const int INF = 2000000000; //2e9
const int large_P = 1000000007; //1e9 + 7



//繰り返し2乗法
//N^aの、Mで割った余りを求める。
ll my_pow(ll N, ll a, ll M) {
	ll tempo;
	if (a == 0) {
		return 1;
	}
	else {
		if (a % 2 == 0) {
			tempo = my_pow(N, a / 2, M);
			return (tempo * tempo) % M;
		}
		else {
			tempo = my_pow(N, a - 1, M);
			return (tempo * N) % M;
		}
	}
}



//N_C_a を M で割った余り
ll my_combination(ll N, ll a, ll M) {
	ll answer = 1;

	rep(i, a) {
		answer *= (N - i);
		answer %= M;
	}

	rep(i, a) {
		answer *= my_pow(i + 1, M - 2, M);
		answer %= M;
	}

	return answer;
}



ll my_gcd(ll& a, ll& b) {
	if (b == 0) return a;
	ll temp = a % b;
	return my_gcd(b, temp);
}



// base を底としたときの、n の i桁目を、v.at(i) に入れる。(桁数は n に応じて自動で設定する。)
void ll_to_vector(int base, long long n, vector<int>& v) {
	long long tempo = n;
	long long tempo2 = n;

	//ll n_digit = floor(log10(n) / log10(base)) + 1;  // 桁の計算を1行で。ただ誤差があるのでよくない。
	int n_digit = 1;
	while (tempo2 >= 10) {
		tempo2 /= 10;
		n_digit++;
	}

	v.assign(n_digit, 0);

	//ll n_digit = v.size();  // v のサイズをそのままにする場合。

	for (int i = 0; i < n_digit; i++) {
		v.at(i) = tempo / pow(base, n_digit - 1 - i);
		tempo -= v.at(i) * pow(base, n_digit - 1 - i);
	}
}



int char_to_int(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4;
	case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9;
	default: return 0;
	}
}


//エラトステネスの篩で、prime で ないところに false を入れる。
//true で初期化された vector を代入する必要がある。
void prime_judge(vector<bool>& prime_or_not) {

	prime_or_not.at(0) = false;
	prime_or_not.at(1) = false;

	ll n = prime_or_not.size() - 1;
	ll region = (ll)sqrt(n) + 1;

	for (ll i = 2; i <= n / 2; i++) {
		prime_or_not.at(2 * i) = false;
	}


	for (ll i2 = 1; i2 < n / 2; i2++) {
		ll i = 2 * i2 + 1;  //ここからは奇数のみ探索
		if (i < region && prime_or_not.at(i)) {
			ll j = i * i;
			while (j < n + 1) {
				prime_or_not.at(j) = false;
				j += 2 * i;
			}
		}
	}
};



map<ll, ll> divide_to_prime(int target) {
	map<ll, ll> res;

	//sqrt(target) まで調べる。
	ll upper_lim = ceil(sqrt(target));
	vector<bool> prime_or_not(upper_lim + 1, true);

	if (upper_lim < 20) prime_or_not.assign(25, true);
	prime_judge(prime_or_not);

	ll tempo = target;
	rep(i, (upper_lim + 1)) {
		if (prime_or_not.at(i)) {
			while (tempo % i == 0) {
				tempo /= i;
				res[i]++;
			}
		}
	}

	if (tempo != 1) res[tempo]++; //sqrt(target) より大きな素因数は高々1つしかない。



	return res;
}



void BFS_labyrinth(queue<pair<int, int>>& que, vector<vector<int>>& dist, int& area) {
	int n = dist.size();
	int m = dist.at(0).size();

	while (!que.empty()) {
		int temp_i, temp_j;
		pair<int, int> tempo = que.front(); que.pop();

		temp_i = tempo.first;
		temp_j = tempo.second;
		//cout << temp_i << " " << temp_j << endl;

		for (int i = -1; i <= 1; i++) {
			for (int j = -1; j <= 1; j++) {
				if (temp_i + i < 0 || n <= temp_i + i) continue;  //範囲外
				if (temp_j + j < 0 || m <= temp_j + j) continue;  //範囲外
				if (i == 0 && j == 0) continue; //動いていない
				//if (i * j != 0) continue; //右上など
				if (dist.at(temp_i + i).at(temp_j + j) != -1) continue; //行けない領域に、既に INF などが代入されている場合はこの条件だけで ok

				dist.at(temp_i + i).at(temp_j + j) = dist.at(temp_i).at(temp_j) + 1;
				que.push(make_pair(temp_i + i, temp_j + j));
			}
		}

		//何か所も領域がある場合だけ必要
		if (que.empty()) {
			rep(i, n) {
				rep(j, m) {
					if (dist.at(i).at(j) == -1) {
						que.push(make_pair(i, j));
						dist.at(i).at(j) = 0;
						area++;
						break;
					}
				}
				if (!que.empty()) break;
			}
		}
	}
}



signed main() {
	int w, h;
	cin >> w >> h;
	vector<int> res;

	while (w != 0 && h != 0) {
		int area = 0;
		vector<vector<int>> c(h, vector<int>(w));
		vector<vector<int>> dist(h, vector<int>(w, -1));

		rep(i, h) {
			rep(j, w) {
				cin >> c.at(i).at(j);
				if (c.at(i).at(j) == 0) dist.at(i).at(j) = INF;
			}
		}

		queue<pair<int, int>> que;
		rep(i, h) {
			rep(j, w) {
				if (c.at(i).at(j) == 1) {
					que.push(make_pair(i, j));
					dist.at(i).at(j) = 0;
					area++;
					break;
				}
			}
			if (!que.empty()) break;
		}
		
		BFS_labyrinth(que, dist, area);

		res.push_back(area);
		//cout << area << endl;
		

		//cout << endl;
		//rep(i, h) {
			//rep(j, w) {
				//cout << dist.at(i).at(j) << " ";
			//}
			//cout << endl;
		//}
		//cout << endl;

		cin >> w >> h;
	}

	rep(i, res.size()) {
		cout << res.at(i) << endl;
	}

}

C++: #include <iostream>

using namespace std;

int w, h;
int c[50][50];
int ax[8] = {-1,-1,-1, 0, 0, 1, 1, 1};
int ay[8] = {-1, 0, 1,-1, 1,-1, 0, 1};
int cnt=0;

void solve(int y, int x){
	if(y<0 || h<=y || x<0 || w<=x) return;
	if(c[y][x] == 1){
		c[y][x] = 0;
		for(int i=0; i<8; i++){
			for(int j=0; j<8; j++){
				solve(y+ay[i], x+ax[j]);
			}
		}
	}
}

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

		cnt = 0;
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				if(c[i][j] == 1){
					solve(i, j);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

void solve(int h, int w) {
  vector<vi> g(h + 2, vi(w + 2, -1));
  rep2(i, 1, h + 1) rep2(j, 1, w + 1) {
    int c;
    cin >> c;
    if(c == 1) g[i][j] = 0;
  }
  int ans = 0;
  rep2(a, 1, h + 1) rep2(b, 1, w + 1) {
    if(g[a][b] != 0) continue;
    ans++;
    g[a][b] = ans;
    stack<pair<int, int>> st;
    st.push(P(a, b));
    while(!st.empty()) {
      int i = st.top().first;
      int j = st.top().second;
      st.pop();
      rep(k, 8) {
        int ni = i + dir[k];
        int nj = j + dir[k + 1];
        if(g[ni][nj] != 0) continue;
        g[ni][nj] = g[i][j];
        st.push(P(ni, nj));
      }
    }
  }
  cout << ans << endl;
}

int main() {
  while(true) {
    int h, w;
    cin >> w >> h;
    if(h == 0 && w == 0) break;
    solve(h, w);
  }
}