Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

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