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<bits/stdc++.h>
using namespace std;
#define OUT(x) cout<<x<<endl
#define DEBUG(x) cout<<#x<<": "<<x
const int BA = 1000;
const int INFTY = (1<<21);
int c[51][51];
int w,h;
int del(int x,int y){
  if(x<0)return 0;
  if(c[x][y]){
    c[x][y] = 0;
    return del(x+1,y)+del(x,y+1)+del(x+1,y+1)+del(x-1,y+1);
    
  }
  else return 0;
  
}
int solve(int x,int y){
  if(x==0 && y == h)return 0;
  else if(x==w){
    return solve(0,y+1);
  }
  else if(c[x][y]==1){
    del(x,y);
    return solve(x+1,y)+1;
  }
  else return solve(x+1,y);
}
int main(){
  while(cin>>w>>h&&w&&h){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> c[i][j];
      }
    }
    cout << solve(0,0) << endl;
  }
}

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

int w, h;
bool data[99][99]; //y,x

int solve(int x, int y){

  if(x < 0 || y < 0 || x == w || y == h) return 0;
  if(!data[y][x]) return 0;

  data[y][x] = false;

  for(int i=-1;i<2;i++) for(int j=-1;j<2;j++) solve(x+j, y+i);

  return 1;
}

int main(){

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

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

    int ret = 0;
    for(int i=0;i<h;i++) for(int j=0;j<w;j++) if(data[i][j]) ret += solve(j, i);

    cout << ret << endl;
  }

}

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

struct UnionFind{
    vector<int> data;

    UnionFind(int N) : data(vector<int>(N,-1)){}

    int root(int x){
        if(data[x] < 0){
            return x;
        }else{
            return data[x] = root(data[x]);
        }
    }

    bool same(int x,int y){
        return root(x) == root(y);
    }

    void unite(int x,int y){
        if(!same(x,y)){
            x = root(x);
            y = root(y);
            if(data[x] < data[y]){
                data[x] += data[y];
                data[y] = x;
            }else{
                data[y] += data[x];
                data[x] = y;
            }
        }
        return;
    }

};

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

void solve(int w,int h){
    swap(w,h);
    int c[w][h];
    for(int i = 0;i < w;i++){
        for(int j = 0;j < h;j++){
            cin >> c[i][j];
        }
    }
    UnionFind uf(w*h);
    for(int i = 0;i < w;i++){
        for(int j = 0;j < h;j++){
            for(int k = 0;k < 8;k++){
                int x = i + dx[k];
                int y = j + dy[k];
                if(0 <= x && x < w && 0 <= y && y < h){
                    if(c[i][j] && c[x][y]){
                        uf.unite(i*h+j,x*h+y);
                    }
                }
            }
        }
    }
    set<int> s;
    for(int i = 0;i < w*h;i++)s.insert(uf.root(i));
    int ans = 0;
    for(auto x:s){
        ans += c[x/h][x%h];
    }
    cout << ans << endl;
}

int main(){
    int w,h;
    while(true){
        cin >> w >> h;
        if(!w && !h)return 0;
        solve(w,h);
    }
}

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) {
			c[i][j] = 0;
			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] = 1-c[i][j];
		}
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			if(func(j,i)) {
				mapst m, ms;
				m.x = j; m.y = i;
				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;
	}
	return 0;
}

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