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 <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <stack>
# include <bitset>
# include <algorithm>
# include <functional>
# include <numeric>
# include <utility>
# include <sstream>
# include <iostream>
# include <iomanip>
# include <cstdio>
# include <cmath>
# include <cstdlib>
# include <cctype>
# include <string>
# include <cstring>
# include <ctime>
# include <queue>


# define FOR(i, a, b) for(int i{a}; i < (b); ++i)
# define REP(i, n)    FOR(i, 0, n)

using namespace std;

using ll = long long;

template<typename T>
using Vec = vector<T>;


template<typename T>
using vec = vector<T>;
template <class T>
using vv = vector<vector<T>>;

constexpr int ocean = 0;

void dfs(const vv<int>& map, vv<bool>& reached, int r, int c) {
    reached[r][c] = true;
    if (reached[r][c] == ocean)
        return;
    for (int i = -1; i <= 1; ++i)
        for (int j = -1; j <= 1; ++j)
        {
            if (i == 0 && j == 0)
                continue;
            int y = r + i;
            int x = c + j;
            if (!reached[y][x] && map[y][x] != ocean)
                dfs(map, reached, y, x);
        }
}

int main()
{

    while (true)
    {
        int w, h;
        cin >> w >> h;

        if (w == 0)
            break;

        vv<int> map(h + 2, vec<int>(w + 2, ocean));

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

        vv<bool> hasReached(h + 2, vec<bool>(w + 2, false));

        // init
        

        int count{};

        for (int i = 1; i < map.size(); ++i)
            for (int j = 1; j < map[0].size(); ++j)
            {
                if (!hasReached[i][j] && map[i][j] != ocean)
                {
                    ++count;
                    dfs(map, hasReached, i, j);
                }
            }

        cout << count << endl;
    }

    return 0;
}

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

class UnionFind{
private:
	vector<int32> par;
	int32 n;

public:
	UnionFind(){}
	UnionFind(int32 n):n(n){
		par.resize(n, -1);
	}

	int32 find(int32 x){
		return par[x] < 0 ? x : par[x] = find(par[x]);
	}

	int32 size(int32 x){
		return -par[find(x)];
	}

	void unite(int32 x, int32 y){
		x = find(x);
		y = find(y);
		if(x == y) return;
		if(size(x) < size(y)) swap(x, y);
		par[x] += par[y];
		par[y] = x;
	}

	bool same(int32 x, int32 y){
		return find(x) == find(y);
	}
};

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int32 w, h;
	while(cin >> w >> h && w && h){
		UnionFind uf((w+2)*(h+2));
		vector<vector<int32>> f(h+2, vector<int32>(w+2, '0'));
		REP(i, h){
			REP(j, w){
				cin >> f[i+1][j+1];
			}
		}
		int32 dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
		int32 dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
		FOR(y, 1, h+1){
			FOR(x, 1, w+1){
				REP(k, 8){
					int32 yy = y+dy[k], xx = x+dx[k];
					if(f[y][x] == 1 && f[yy][xx] == 1 && !uf.same(yy*(w+2)+xx, y*(w+2)+x)){
						uf.unite(yy*(w+2)+xx, y*(w+2)+x);
					}
				}
			}
		}
		int32 cnt = 0;
		REP(y, h+2){
			REP(x, w+2){
				if(f[y][x] == 1 && uf.find(y*(w+2)+x) == y*(w+2)+x)
					cnt++;
			}
		}
		cout << cnt << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
 
int w=50,h=50;
vector<vector<int>>data(h,vector<int>(w));


void dfs(int x,int y){
	data[x][y]=0;
	for(int dx=-1;dx<=1;dx++){
    	for(int dy=-1;dy<=1;dy++){
        //四方の移動先を考え、陸を探す。
          int nx=x+dx, ny=y+dy;
        	if( 0<=nx && nx<h && 0<=ny && ny<w &&data[nx][ny]==1){
              dfs(nx,ny);
        	}
        }
    }     
}

int main() {
 while(1){w=0;h=0;
  cin >>w>>h;
  if(w==0&&h==0)return 0;
  for(int i=0;i<h;i++){
  	for(int j=0;j<w;j++){
    cin>>data.at(i).at(j);
    } 
  }
  
  int ans=0;
  for(int i=0;i<h;i++){
  	for(int j=0;j<w;j++){
   		if(data[i][j]==1){
        	dfs(i,j);
        	ans++;
        }
    } 
  }
 	cout <<ans <<endl; 
 }  
}

C++: #include<iostream>

using namespace std;

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

const int vx[] = {1,0,-1,0}, vy[] = {0,1,0,-1};

void island(int _w, int _h) {
	map[_w][_h] = 2;
	for (int i = 0;i < 4;i++) {
		if (_w + vy[i] >= 1 && _w + vy[i] <= w&&_h + vx[i] >= 1 && _h + vx[i] <= h&&map[_w+vy[i]][_h+vx[i]] == 1) {
			island(_w + vy[i], _h + vx[i]);
		}
	}
}

int main() {
	while (cin >> w >> h, w + h) {
		for (int i = 1;i <= w;i++) {
			for (int j = 1;j <= h;j++) {
				cin >> map[i][j];
			}
		}
		int count = 0;
		for (int i = 1;i <= w;i++) {
			for (int j = 1;j <= h;j++) {
				if (map[i][j] == 1) {
					count++, island(i, j);
				}
			}
		}
		cout << count << endl;
		for (int i = 1;i <= w;i++)for (int j = 1;j <= h;j++)map[i][j] = -1;
	}
	return 0;
}