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 <cmath>
#include <algorithm>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i,k,n) for(int i = k; i < (int)(n); i++)
using namespace std;

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

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

        int dx[] = {-1,-1,-1,0,0,1,1,1};
        int dy[] = {-1,0,1,-1,1,-1,0,1,};
        rep(i,8){
                int nx = x+dx[i];
                int ny = y+dy[i];
                if(nx>=0 && nx<w && ny>=0 && ny<h && field[ny][nx] == 1) dfs(ny,nx);
        }
        return;
}

int main(){
        while(true) {
                cin >> w >> h;
                if(w==0) break;
                rep(y,h) rep(x,w){
                        cin >> field[y][x];
                }
                int ans = 0;
                rep(y,h) rep(x,w){
                        if(field[y][x] == 1) {
                                dfs(y,x);
                                ans++;
                        }
                }
                cout << ans << endl;
        }

        return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
typedef long long ll;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

#define EPS (1e-7)
#define INF (1e15)
#define PI (acos(-1))
#define MOD 1000000007

#define REP(i,n) for(int i=0;i<n;i++)
#define REPS(i,f,n) for(int i=(f);i<(n);i++)
#define EACH(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)

#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define arrdump(n,a) for(int i=0;i<n;i++) {cout<<((i>0)? " ":"")<<a[i];}cout<<"\n";


typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int dy4[]={0, 0, 1, -1};
int dx4[]={1, -1, 0, 0};
int dx8[]={0, 0, 1, -1, 1, 1, -1, -1};
int dy8[]={1, -1, 0, 0, 1, -1, -1, 1};

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

int C[99][99];
int w, h;

static void dfs(int i , int j) {
    C[i][j] = 0;

    REP(k, 8) {
        int nx = j + dx8[k];
        int ny = i + dy8[k];
        if (nx >= 0 && nx < w && ny >= 0 && ny < h) {
            if (C[ny][nx] == 1) {
                dfs(ny, nx);
            }
        }
    }          
}

int main() {

    while (true) {
        cin >> w >> h;
        if (w==0&&h==0) break;

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

        int cnt = 0;
        while(true) {
            bool found = false;
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    if (C[i][j] == 1) {
                        dfs(i, j);
                        cnt++;
                    }
                }
            }

            if (!found) break;
        }

        cout << cnt << "\n";
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int H,W;
bool fld[50][50];
void del(int x,int y){
	fld[x][y]=0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			int nx=x+i,ny=y+j;
			if(nx>=0 && nx<W && ny>=0 && ny<H && fld[nx][ny])del(nx,ny);
		}
	}
}
int main(){
	while(cin>>W>>H,W,H){
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>fld[j][i];
			}
		}
		int cnt=0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(fld[j][i]){
					cnt++;
					fld[j][i]=0;
					del(j,i);
				}
			}
		}
		cout<<cnt<<endl;
	}
}

C++: #include<bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		if (w == 0&&h == 0) {
			break;
		}
		ans.push_back(res);


	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}