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 <string>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i=0;i < (int)(n);i++)

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

int maze[51][51];
bool seen[51][51];
void dfs(int x,int y){
     seen[x][y]=true;
     rep(i,8){
          int nx,ny;
          nx=x+dx[i];ny=y+dy[i];
          if (nx >= 0 && nx < h && ny >= 0 && ny < w && maze[nx][ny] == 1){ 
               if (seen[nx][ny]) continue;
               dfs(nx,ny);
          }   
     }
     
}

int main(){
     bool can = true;
     while(can){
          cin >> w >> h;
          if (h == 0 && w==0) {
               can = false;
               break;
          }
          int ans=0;
          rep(i,h) rep(j,w) seen[i][j]=false;

          rep(i,h) rep(j,w) cin >> maze[i][j];
          rep(i,h) rep(j,w){
               if (!seen[i][j] && maze[i][j] == 1) {
                    ans++;
                    //printf("(i,j) = (%d,%d)\n",i,j);
                    dfs(i,j);
               }
          }
          cout << ans << endl;
     }
    
     
     return 0;
}

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 seamap[52][52];
static int num = 0;

void dfs(int x, int y) {
	if(seamap[x][y] == 1) {

	if(seamap[x-1][y-1]) {
		seamap[x-1][y-1] = 0;
		dfs(x-1, y-1);
	}
	else if(seamap[x][y-1]) {
		seamap[x][y-1] = 0;
		dfs(x, y-1);
	}
	else if(seamap[x+1][y-1]) {
		seamap[x+1][y-1] = 0;
		dfs(x+1, y-1);
	}
	else if(seamap[x-1][y]) {
		seamap[x-1][y] = 0;
		dfs(x-1, y);
	}
	else if(seamap[x+1][y]) {
		seamap[x+1][y] = 0;
		dfs(x+1, y);
	}
	else if(seamap[x-1][y+1]) {
		seamap[x-1][y+1] = 0;
		dfs(x-1, y+1);
	}
	else if(seamap[x][y+1]) {
		seamap[x][y+1] = 0;
		dfs(x, y+1);
	}
	else if(seamap[x+1][y+1]) {
		seamap[x+1][y+1] = 0;
		dfs(x+1, y+1);
	}
	else {
		++num;
	}

	}
}


int main() {
	int w, h;

	REP(i, 52) {
		REP(j, 52) {
			seamap[i][j] = 0;
		}
	}

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

		FOR(i, 1, h+1) {
			seamap[i][w+1] = 0;
		}

		FOR(j, 1, w+1) {
			seamap[h+1][j] = 0;
		}
	
		num = 0;

		FOR(i, 1, 50) {
			FOR(j, 1, 50) {
				dfs(i, j);
			}
		}

		cout << num << endl;
	}
}

C++: #include <iostream>

#include <string>
#include <queue>

using namespace std;

int main(){
	int w,h;
	int data[60][60];
	int flag[60][60];
	int dx[8] ={-1,-1,-1,0,1,1,1,0};
	int dy[8] ={-1,0,1,1,1,0,-1,-1};

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

	for(int i=0;i<60;i++){
		for(int j=0;j<60;j++){
			data[i][j]=-1;
			flag[i][j]=0;
		}
	}

	char tmp;
	int startX;
	int startY;
	int cnt=0;

	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			cin>>tmp;
			if(tmp=='1')data[i][j] = 0;
		}
	}

	queue<int> qX,qY;
	
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			if(data[i][j]==0 && flag[i][j]==0){
				flag[i][j]=1;
				qX.push(j);
				qY.push(i);
				cnt++;
			}
			else continue;

			while(qX.size()!=0){
				int topX = qX.front();
				int topY = qY.front();
				qX.pop();
				qY.pop();

				for(int i=0;i<8;i++){
					int nowX = topX + dx[i];
					int nowY = topY + dy[i];

					if(data[nowY][nowX] ==0 && flag[nowY][nowX] == 0){
						qX.push(nowX);
						qY.push(nowY);
						flag[nowY][nowX]=1;
					}
				}
			}
		}
	}

	cout<<cnt<<endl;
	}

	return 0;
}