Go: // AOJ 1160: How Many Islands? http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var SEA = 0
var LAND = 1

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func discoverIslands(M, V [][]int, H, W int) int {
	// 島を探す
	queue := list.New()
	islands := 0
	for i := 1; i <= H; i++ {
		for j := 1; j <= W; j++ {
			// 訪問済みの土地ならスキップ
			if V[i][j] == 1 {
				continue
			}

			// 訪問中の土地が海なら訪問済みにしてスキップ
			if M[i][j] == SEA {
				V[i][j] = 1
				continue
			}

			// 未訪問の陸地が見つかったので
			// 島の数を1つ増やし、この陸地から辿れる陸地を訪問していく
			islands++
			queue.PushBack([]int{i, j})

			for queue.Len() > 0 {
				cur := queue.Remove(queue.Front()).([]int)
				h, w := cur[0], cur[1]
				if V[h][w] == 1 {
					continue
				}
				V[h][w] = 1

				// 全方向に未訪問の陸地がないか探索
				for v := -1; v <= 1; v++ {
					for l := -1; l <= 1; l++ {
						dh := h + v
						dw := w + l
						if M[dh][dw] == LAND && V[dh][dw] == 0 {
							queue.PushBack([]int{dh, dw})
						}
					}
				}
				/*
					if M[h-1][w] == LAND {
						queue.PushBack([]int{h - 1, w})
					}
					if M[h-1][w+1] == LAND {
						queue.PushBack([]int{h - 1, w + 1})
					}
					if M[h][w+1] == LAND {
						queue.PushBack([]int{h, w + 1})
					}
					if M[h+1][w+1] == LAND {
						queue.PushBack([]int{h + 1, w + 1})
					}
					if M[h+1][w] == LAND {
						queue.PushBack([]int{h + 1, w})
					}
					if M[h+1][w-1] == LAND {
						queue.PushBack([]int{h + 1, w - 1})
					}
					if M[h][w-1] == LAND {
						queue.PushBack([]int{h, w - 1})
					}
					if M[h-1][w-1] == LAND {
						queue.PushBack([]int{h - 1, w - 1})
					}
				*/
			}
		}
	}
	return islands
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	for {
		W, H := nextInt(sc), nextInt(sc)

		if W == 0 && H == 0 {
			break
		}
		// Initialize map and visted list
		M := make([][]int, H+2)
		V := make([][]int, H+2)
		for i := 0; i < H+2; i++ {
			M[i] = make([]int, W+2)
			V[i] = make([]int, W+2)
		}
		for i := 1; i <= H; i++ {
			for j := 1; j <= W; j++ {
				M[i][j] = nextInt(sc)
			}
		}

		islands := discoverIslands(M, V, H, W)

		fmt.Println(islands)
	}
}

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
				
				
vector<vector<int>> del_island(int i,int j,vector<vector<int>> v){	
	v[i][j] = 0;
	vector<vector<int>> answer = v;
	if(v[i-1][j] == 1){
		answer = del_island(i-1,j,answer);
	}
	if(v[i+1][j] == 1){
		answer = del_island(i+1,j,answer);
	}
	if(v[i][j-1] == 1){
		answer = del_island(i,j-1,answer);
	}
	if(v[i][j+1] == 1){
		answer = del_island(i,j+1,answer);
	}
	if(v[i-1][j-1] == 1){
		answer = del_island(i-1,j-1,answer);
	}
	if(v[i-1][j+1] == 1){
		answer = del_island(i-1,j+1,answer);
	}
	if(v[i+1][j-1] == 1){
		answer = del_island(i+1,j-1,answer);
	}
	if(v[i+1][j+1] == 1){
		answer = del_island(i+1,j+1,answer);
	}
	return answer;
}


int count_island(int w, int h,vector<vector<int>> v){
	int count = 0;
	for(int i = 1 ; i <= h ;i++){
		for(int j = 1 ;j <= w ;j++){
			//cout << v[i][j] ;
			if(v[i][j] == 1){
				count++;
				//cout << "hello" <<endl;
				v = del_island(i,j,v);
			}
		}
	}
	return count;
}
	
int main(){ 
	int w,h;
	vector<vector<int>> v(60,vector<int>(60,0));
	int count;

	while(1){
		cin >> w >> h;
		if(w == 0 && h == 0){
			break;
		}
		count = 0; 

		for(int i = 1;i <= h; i++){
			for(int j = 1; j <= w; j++){
				cin >> v[i][j];
			}
		}

		count = count_island(w,h,v);


		cout << count << endl;
	}
}

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

struct mapst {
	int x, y;
};

#define func(x,y) (c[y][x]&&!f[y][x])

int main() {
	int w, h, ans=0;
	int c[52][52]={{0}}, f[52][52]={{1}};
	int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
	int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
	queue<mapst> q;
	while(1) {
		cin >> w >> h;
		if(!w&&!h) break;
		ans = 0;
		for(int i=0; i<52; ++i) for(int j=0; j<52; ++j) f[i][j] = 1;
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			cin >> c[i][j];
			f[i][j] = 0;
		}
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			if(func(j,i)) {
				mapst m = {j, i}, ms;
				q.push(m);
				ans++;
				do {
					m = q.front();
					q.pop();
					f[m.y][m.x] = 1;
					for(int k=0; k<8; k++) {
						if(func(m.x+dx[k],m.y+dy[k])) {
							ms.x = m.x+dx[k]; ms.y = m.y+dy[k];
							q.push(ms);
						}
					}
				}while(!q.empty());
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
#define INF 100000000
#define IINF 9999999999999;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

bool earth[51][51];
ofstream outputfile("output.txt");
int depth = 0;
int inputw, inputh;

void erase_island(int h, int w) {
    //++depth;
    //outputfile << depth << endl;
    earth[h][w] = false;
    if(h != 0 && w != 0) {
        if (earth[h - 1][w - 1]) erase_island(h - 1, w - 1);
    }
    if(h != 0 && w != inputw - 1) {
        if (earth[h - 1][w]) erase_island(h - 1, w);
    }
    if(h != 0) {
        if(earth[h - 1][w + 1]) erase_island(h - 1, w + 1);
    }
    if(w != 0 && h != inputh - 1) {
        if(earth[h + 1][w - 1]) erase_island(h + 1, w - 1);
    }
    if(w != 0) {
        if(earth[h][w - 1]) erase_island(h, w - 1);
    }
    if(h != inputh - 1 && w != inputw - 1) {
        if(earth[h + 1][w + 1]) erase_island(h + 1, w + 1);
    }
    if(h != inputh - 1) {
        if(earth[h + 1][w]) erase_island(h + 1, w);
    }
    if(w != inputw - 1) {
        if(earth[h][w + 1]) erase_island(h, w + 1);
    }
    //cout << "# " << h << " " << w << endl;
    depth = 0;
}

int main() {
    while(cin >> inputw >> inputh, inputw + inputh) {
        int temp;
        rep(i, inputh) {
            rep(j, inputw) {
                cin >> temp;
                if(temp == 0) earth[i][j] = false;
                else earth[i][j] = true;
            }
        }

        int ans = 0;
        rep(i, inputh) {
            rep(j, inputw) {
                if(earth[i][j]) {
                    erase_island(i, j);
                    ++ans;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

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

const int ALIVE = 1, DEAD = 2;
int h, w;
vector<vector<int>> field;
bool out_field(int i, int j) { return i < 0 || i >= h || j < 0 || j >= w; }

void dfs(int i, int j) {
    field[i][j] = DEAD;
    for (int di: {-1, 0, 1}) for (int dj: {-1, 0, 1}) {
        int ni = i + di, nj = j + dj;
        if (out_field(ni, nj) || field[ni][nj] != ALIVE) continue;
        dfs(ni, nj);
    }
}

int main() {
    while (cin >> w >> h, w) {
        field.assign(h, vector<int>(w));
        for (auto &row: field) for (auto &c: row) cin >> c;
        int cnt = 0;
        for (int si = 0; si < h; si++) for (int sj = 0; sj < w; sj++) {
            if (field[si][sj] != ALIVE) continue;
            cnt++;
            dfs(si, sj);
        }
        cout << cnt << endl;
    }
    return 0;
}