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 <stdio.h>
#include <iostream>
#include <queue>
#include <vector>


#define rep(i,a,b)(long long i=a;i<b;i++)

using namespace std;


struct POINT {
	int x;
	int y;
	POINT(int X, int Y){
		x = X;y = Y;
	}
};

queue<POINT>	usedWay;
int SizeX,SizeY;
int Map[50][50] = {0};
vector<int>	result;


void SearchIsland(int X, int Y){
	usedWay.push(POINT(X,Y));
	Map[X][Y] = 2;

	while(usedWay.empty() == false){
		POINT NowPos = usedWay.front();
		usedWay.pop();

		if(NowPos.y-1 >= 0){
			//	左上
			if(NowPos.x-1 >= 0 && Map[NowPos.x-1][NowPos.y-1] == 1){
				Map[NowPos.x-1][NowPos.y-1] = 2;
				usedWay.push(POINT(NowPos.x - 1,NowPos.y - 1));
			}
		
			//	真上
			if(					  Map[NowPos.x][NowPos.y-1] == 1){
				Map[NowPos.x][NowPos.y-1] = 2;
				usedWay.push(POINT(NowPos.x,NowPos.y - 1));
			}

			//	右上
			if( NowPos.x + 1 < SizeX && Map[NowPos.x+1][NowPos.y-1] == 1){
				Map[NowPos.x+1][NowPos.y-1] = 2;
				usedWay.push(POINT(NowPos.x + 1,NowPos.y - 1));
			}
		}

		//	左
		if(NowPos.x-1 >= 0 && Map[NowPos.x-1][NowPos.y] == 1){
			Map[NowPos.x-1][NowPos.y] = 2;
			usedWay.push(POINT(NowPos.x - 1,NowPos.y));
		}

		//	右
		if( NowPos.x + 1 < SizeX && Map[NowPos.x+1][NowPos.y] == 1){
			Map[NowPos.x+1][NowPos.y] = 2;
			usedWay.push(POINT(NowPos.x + 1,NowPos.y));
		}

		if(NowPos.y + 1 < SizeY){
			//	左下
			if(NowPos.x-1 >= 0 && Map[NowPos.x-1][NowPos.y+1] == 1){
				Map[NowPos.x-1][NowPos.y+1] = 2;
				usedWay.push(POINT(NowPos.x - 1,NowPos.y + 1));
			}
		
			//	真下
			if(					  Map[NowPos.x][NowPos.y+1] == 1){
				Map[NowPos.x][NowPos.y+1] = 2;
				usedWay.push(POINT(NowPos.x,NowPos.y + 1));
			}

			//	右下
			if( NowPos.x + 1 < SizeX && Map[NowPos.x+1][NowPos.y+1] == 1){
				Map[NowPos.x+1][NowPos.y+1] = 2;
				usedWay.push(POINT(NowPos.x + 1,NowPos.y + 1));
			}
		}


	}
}

int main(void){

	while(true){

		int NumIsland = 0;

		cin >> SizeX;
		cin >> SizeY;

		if(SizeX == 0 && SizeY == 0)
			break;

		int tmp = SizeX;
		SizeX = SizeY;
		SizeY = tmp;

		//	読み込み
		for(int i = 0 ; i < SizeX; ++i)
			for(int j = 0 ; j < SizeY ; ++j)
				cin >> Map[i][j];


		for(int i = 0 ; i < SizeX ; ++i){
			for(int j = 0 ; j < SizeY ; ++j){
				if(Map[i][j] == 1){
					SearchIsland(i,j);
					NumIsland++;
				}
			}
		}

		result.push_back(NumIsland);
	}

	for(vector<int>::iterator it = result.begin() ; it != result.end(); ++it)
		cout << *it << endl;

	return 0;
}

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

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

int main() {
	int w, h, ans, x, y;
	int c[52][52], f[52][52];
	stack< pair<int, int> > s;
	while(1) {
		cin >> w >> h;
		if(!w&&!h) break;
		for(int i=0; i<h+2; ++i) for(int j=0; j<w+2; ++j) {
			c[i][j] = 0; f[i][j] = 0;
		}
		ans = 0;
		
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			cin >> c[i][j];
		}
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			if(c[i][j]&&!f[i][j]) {
				ans++;
				s.push( pair<int, int>(i, j) );
				while(!s.empty()) {
					y = s.top().first; x = s.top().second;
					s.pop();
					f[y][x] = 1;
					for(int k=0; k<8; ++k) {
						if(c[y+dy[k]][x+dx[k]]&&!f[y+dy[k]][x+dx[k]]) {
							s.push( pair<int, int>(y+dy[k], x+dx[k]) );
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

int H, W;
int field[60][60];


void dfs(int h, int w) {
    field[h][w]=0;
    
    for (int dx = -1; dx <= 1; dx++) {
        for(int dy=-1;dy<=1;dy++){
            int nh = h + dx;
            int nw = w + dy;

            // 場外アウトしたり、移動先が壁の場合はスルー
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if (field[nh][nw] == 0) continue;

            // 再帰的に探索
            dfs(nh, nw);
        }
        
    }
}

int main() {

    while(1){
        cin >> W >> H;
        if(H==0 && W==0) break;
        rep(i,H) rep(j,W) cin>>field[i][j];

        int ans=0;
        rep(i,H){
            rep(j,W){
                if(field[i][j]==0) continue;
                dfs(i,j);
                ans++;
            }
        }

        cout<<ans<<endl;
    }
}