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

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int main() {
	vvi map(52, vi(52, 0));

	int w, h;

	while(cin >> w >> h, (w!=0 && h!=0)) {
		FOR(i, 1, h+1) {
			FOR(j, 1, w+1) {
				int p;
				cin >> p;
				map[i][j] = p;
			}
		}
	
		int num = 0;

		FOR(i, 1, h+1) {
			FOR(j, 1, w+1) {
				if(map[i][j]) {
					if(map[i][j+1] || map[i+1][j-1] || map[i+1][j] || map[i+1][j+1]) {

					} else {
						num++;
					}
				}
			}
		}

		cout << num << endl;

	}
}

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

using ll=long long;
#define int ll

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

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

int land[1000][1000]={-1};
int flag[1000][1000]={-1};
int dy[]={-1,-1,-1,0,0,1,1,1};
int dx[]={-1,0,1,-1,1,-1,0,1};
int cnt=0;

void isLand(int i,int j){
	REP(k,8){
		int newx=i+dx[k];
		int newy=j+dy[k];
		if(land[newx][newy]){
			if(flag[newx][newy]!=-1){
				flag[i][j]=flag[newx][newy];
				return;
			}
		}
	}
	cnt++;
	flag[i][j] = cnt;
}

signed main(){
	int w,h;
	while( w=read(),h=read(),w||h){
		cnt=0;
		
		REP(i,1000){
			REP(j,1000){
				land[i][j]=-1;
				flag[i][j]=-1;
			}
		}
		
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				land[i][j]=read();
			}
		}
		
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				if(land[i][j])isLand(i,j);
			}
		}
		
		cout << cnt << endl;
	}
	
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

/*
 * Union-Find tree
 * header requirement: vector
 */
class UnionFind {
private:
  std::vector<int> disj;
  std::vector<int> rank;
public:
  UnionFind(int n) : disj(n), rank(n) {
    for (int i = 0; i < n; ++i) {
      disj[i] = i;
      rank[i] = 0;
    }
  }
  int root(int x) {
    if (disj[x] == x) {
      return x;
    }
    return disj[x] = root(disj[x]);
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return;
    }
    if (rank[x] < rank[y]) {
      disj[x] = y;
    } else {
      disj[y] = x;
      if (rank[x] == rank[y]) {
	++rank[x];
      }
    }
  }
  bool is_same_set(int x, int y) {
    return root(x) == root(y);
  }
};


#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;



int main(void){
  int w, h;
  while ((cin >> w >> h) && (w || h)) {
    vector<VI> mp(h, VI(w, 0));
    REP(i, 0, h) {
      REP(j, 0, w) {
	cin >> mp[i][j];
      }
    }
    UnionFind uf(w * h);
    REP(i, 0, h) {
      REP(j, 0, w) {
	if (mp[i][j] == 0) { continue; }
	REP(dx, -1, 2) {
	  REP(dy, -1, 2) {
	    if (dx == 0 && dy == 0) { continue; }
	    int nx = i + dx;
	    int ny = j + dy;
	    if (0 <= nx && nx < h && 0 <= ny && ny < w && mp[nx][ny]) {
	      // unite
	      uf.unite(i * w + j, nx * w + ny);
	    }
	  }
	}
      }
    }
    set<int> roots;
    REP(i, 0, h * w) {
      int x = i / w;
      int y = i % w;
      if (mp[x][y]) {
	roots.insert(uf.root(i));
      }
    }
    cout << roots.size() << endl;
  }
}

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

int c[51][51];
int w, h;
int dx[8] = { 0,1,1,1,0,-1,-1,-1 };
int dy[8] = { -1,-1,0,1,1,1,0,-1 };

void dfs(int i, int j) {
  if(i < 0 || i >= h || j < 0 || j >= w || c[i][j] == 0)
    return;

  c[i][j] = 0;
  for(int k=0;k<8;k++) {
    dfs(i+dy[k], j+dx[k]);
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  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];
      }
    }

    ll ans = 0;
    for(int i=0;i<h;i++) {
      for(int j=0;j<w;j++) {
        if(c[i][j] == 1) {
          ans++;
          dfs(i, j);
        }
      }
    }
    cout << ans << endl;

  }
}