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<queue>
#include<utility>
using namespace std;
void search(int, int);
int c[50][50] = {}, map[50][50] = {};
void mapDraw(int,int);
queue< pair<int, int> > q;
int w, h;

int main() {

	while (1) {
		
		//initialize
		int count = 0;
		for (int i = 0; i < 50; i++) {
			for (int j = 0; j < 50; j++) {
				c[i][j] = 0;
				map[i][j] = 0;
			}
		}


		cin >> w >> h;
		if (w == 0 && h == 0) break;
		//input
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[i][j];
			}
		}

		//search
		//unresearched:0, land:2, sea:1
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 0) {
					map[i][j] = c[i][j] + 1;
					if (c[i][j] == 1) {
						q.push(make_pair(i, j));
						count++;
					}
				}

				pair<int, int> cur = make_pair(i, j);
				while (!q.empty()) {
					cur = q.front();
					int x = cur.second;
					int y = cur.first;
					q.pop();

					//cerr << "( " << y << ", " << x << " )now"<< endl;

					for (int a = -1; a <= 1; a++) {
						for (int b = -1; b <= 1; b++) search(y + a, x + b);
					}
				}
			}
		}

		cout << count << endl;
	}


	return 0;
}

void search(int i, int j) {
	if (map[i][j] == 0) {
		map[i][j] = c[i][j] + 1;
		if (c[i][j] == 1) q.push(make_pair(i, j));
	}
	//mapDraw(w, h);
}

void mapDraw(int w, int h) {
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cerr << map[i][j];
		}
		cerr << endl;
	}
	cerr << endl;
}

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

struct pos{
	int x;
	int y;
};

int main(){
	int w, h;
	while(1){
		cin >> w >> h;
		if(w == 0 || h == 0) break;

		int marine[52][52];
		for(int i = 0; i < 52; i++){
			for(int j = 0; j < 52; j++){
				marine[i][j] = 0;
			}
		}

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

		stack<pos> st;
		int island = 0;
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
				//島を探す
				if(marine[i][j] == 1){
					pos p;
					p.y = i;
					p.x = j;
					st.push(p);
					marine[i][j] = 0;
					while(!st.empty()){
						pos p1 = st.top();
						st.pop();
						int x1[] = {-1,  0, 1, 0, -1,  1, 1, -1};
						int y1[] = {0 , -1, 0, 1, -1, -1, 1,  1};
						for(int k = 0; k < 8; k++){
							p = p1;
							p.y += y1[k];
							p.x += x1[k];
							if(marine[p.y][p.x] == 1){
								st.push(p);
								marine[p.y][p.x] = 0;
							}
						}
					}
					island++;
				}


			}
		}

		cout << island << endl;
	}
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int a[52][52]={};

int main(){
  while(1){
    int count=0,w,h;
    cin>>w>>h;
    if(w+h==0)break;
    
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++)
	cin>>a[i][j];

    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++)
	if(a[i][j]==1){
	  dfs(j,i);
	  count++;
	}
    cout<<count<<endl;  
  }
  return 0;
}

  void dfs(int x,int y){
    a[y][x]=0;
    int nx,ny;
    int dx[8]={1,1,1,0,-1,-1,-1,0};
    int dy[8]={1,0,-1,-1,-1,0,1,1};
    for(int k=0;k<8;k++){
	nx=x+dx[k];
	ny=y+dy[k];
	if(a[ny][nx]==1){
	  dfs(nx,ny);
	}
    }
  }