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++: #include <cstdio>
#include <cstring>
using namespace std;

int w, h;
int map[100][100];
int island_number[100][100];

int angle[][2] = {
	{-1, 0}, {-1, -1}, {0, -1}, {1, -1},
	{1, 0}, {1, 1}, {0, 1}, {-1, 1}
};

void dfs(int y, int x)
{
	for (int i = 0; i < 8; i++){
		if (
		y + angle[i][0] >= 0 &&
		y + angle[i][0] < h &&
		x + angle[i][1] >= 0 &&
		x + angle[i][1] < w &&
		island_number[y + angle[i][0]][x + angle[i][1]] == (y + angle[i][0]) * h + x + angle[i][1]
		){
			island_number[y + angle[i][0]][x + angle[i][1]] = island_number[y][x];
			dfs(y + angle[i][0], x + angle[i][1]);
		}
	}
}

int main()
{
	
	while (scanf("%d %d", &w, &h), w + h){
		memset(island_number, 0, sizeof(island_number));
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				scanf("%d", &map[i][j]);
				if (map[i][j] == 1){
					island_number[i][j] = i * h + j;
				}
				else {
					island_number[i][j] = -1;
				}
			}
		}
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (island_number[i][j] == i * h + j && island_number[i][j] >= 0){
					dfs(i, j);
				}
			}
		}
		
		bool ex[100 * 100] = {false};
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (island_number[i][j] >= 0){
					ex[island_number[i][j]] = true;
				}
//				printf("%d ", island_number[i][j]);
			}
//			puts("");
		}
		
		int cnt = 0;
		for (int i = 0; i < w * h; i++){
			cnt += ex[i];
		}
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long llong;

int H, W;
vector<vector<int> > field;

void dfs(int y, int x) {
	field[y][x] = 0;

	for (int dy = -1; dy <= 1; dy++) {
		for (int dx = -1; dx <= 1; dx++) {
			int ny = y + dy;
			int nx = x + dx;

			if (ny < 0 || H <= ny) continue;
			if (nx < 0 || W <= nx) continue;
			if (field[ny][nx] == 0) continue;

			dfs(ny, nx);
		}
	}
}

int main(void) {

	while (cin >> W >> H) {
		if (H == 0 || W == 0) break;

		// フィールド入力
		field.assign(H, vector<int>(W, 0));
		for (int y = 0; y < H; y++) for (int x = 0; x < W; x++) cin >> field[y][x];

		// 探索
		int count = 0;
		for (int y = 0; y < H; y++) {
			for (int x = 0; x < W; x++) {
				if (field[y][x] == 0) continue;
				dfs(y, x);
				count++;
			}
		}
		cout << count << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
typedef unsigned int ui;
int main(){
	int w, h;
	int i, j,k,l;
	while (cin >> w >> h) {
		if (w == 0)break;
		int lan[52][52];
		for (i = 0; i < 52; i++) {
			for (j = 0; j < 52; j++) {
				lan[i][j] = 0;
			}
		}
		for (j = 1; j <= h; j++) {
			for (i = 1; i <= w; i++) {
				cin >> lan[i][j];
			}
		}
		int count = 0;
		for (j = 1; j <= h; j++) {
			for (i = 1; i <= w; i++) {
				if (lan[i][j] == 1) {
					count++; lan[i][j] = 0;
					vector<P> q; q.push_back(P { i,j });
					while (!q.empty()) {
						int nx = q[0].first; int ny = q[0].second;
						q.erase(q.begin() + 0);
						for (k = nx - 1; k <= nx + 1; k++) {
							for (l = ny - 1; l <= ny + 1; l++) {
								if (lan[k][l] == 1) {
									q.push_back(P{k,l}); lan[k][l] = 0;
								}
							}
						}
					}
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int seamap[52][52];
static int num = 0;

void dfs(int x, int y) {
	if(seamap[x][y] == 1) {
		if(seamap[x-1][y-1]) {
			seamap[x-1][y-1] = 0;
			dfs(x-1, y-1);
		}
		if(seamap[x][y-1]) {
			seamap[x][y-1] = 0;
			dfs(x, y-1);
		}
		if(seamap[x+1][y-1]) {
			seamap[x+1][y-1] = 0;
			dfs(x+1, y-1);
		}
		if(seamap[x-1][y]) {
			seamap[x-1][y] = 0;
			dfs(x-1, y);
		}
		if(seamap[x+1][y]) {
			seamap[x+1][y] = 0;
			dfs(x+1, y);
		}
		if(seamap[x-1][y+1]) {
			seamap[x-1][y+1] = 0;
			dfs(x-1, y+1);
		}
		if(seamap[x][y+1]) {
			seamap[x][y+1] = 0;
			dfs(x, y+1);
		}
		if(seamap[x+1][y+1]) {
			seamap[x+1][y+1] = 0;
			dfs(x+1, y+1);
		}
	}
}


int main() {
	int w, h;

	REP(i, 52) {
		REP(j, 52) {
			seamap[i][j] = 0;
		}
	}

	while(cin >> w >> h, (w!=0 && h!=0)) {
		FOR(i, 1, h+1) {
			FOR(j, 1, w+1) {
				int p;
				cin >> p;
				seamap[i][j] = p;
			}
		}

		FOR(i, 1, h+1) {
			seamap[i][w+1] = 0;
		}

		FOR(j, 1, w+1) {
			seamap[h+1][j] = 0;
		}
	
		num = 0;

		FOR(i, 1, 50) {
			FOR(j, 1, 50) {
				if(seamap[i][j]) {
					num++;
					dfs(i, j);
				}
			}
		}

		cout << num << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;

int w, h;
int hw[51][51];

void solve2(int y, int x) {
    using P = pair<int, int>;
    int dx[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
    int dy[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
    queue<P> que;
    que.push(P(y, x));
    while (que.size()) {
        P p = que.front(); que.pop();
        rep(d, 8) {
            int py = p.first + dy[d];
            int px = p.second + dx[d];
            if (0 <= py && py < h && 0 <= px && px < w && hw[py][px] == 1) {
                hw[py][px] = 0;
                que.push(P(py, px));
            }
        }
    }
}

int solve() {

    int ans = 0;

    rep(y, h) {
        rep(x, w) {
            if (hw[y][x] == 1) {
                hw[y][x] = 0;
                solve2(y, x);
                ans++;
            }
        }
    }

    return ans;

}

int main() {

    while (cin >> w >> h and (w or h)) {
        rep(i, h) rep(j, w) cin >> hw[i][j];
        cout << solve() << endl;
    }
}