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;

int main() {
    while (true) {
        bool seen[100][100] = {false};
        vector<vector<int>> field;
        stack<pair<int, int>> to_visit;
        int cnt = 0;

        int w, h;
        cin >> w >> h;

        if (w == 0 || h == 0) {
            break;
        }

        field.assign(h, vector<int>(w, 0));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> field[i][j];
            }
        }

        // for (int i = 0; i < h; i++) {
        //     for (int j = 0; j < w; j++) {
        //         if (field[i][j] == 0) {
        //             cout << "^";
        //         }
        //         else {
        //             cout << "O";
        //         }
        //     }
        //     cout << endl;
        // }

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (field[i][j] == 0) {
                    continue;
                }
                if (seen[i][j] == true) {
                    continue;
                }

                cnt++;
                to_visit.push(make_pair(i, j));
                seen[i][j] = true;
                while (to_visit.empty() == false) {
                    pair<int, int> cur_pos = to_visit.top(); to_visit.pop();
                    int cur_h = cur_pos.first;
                    int cur_w = cur_pos.second;
                    for (int dh = -1; dh <= 1; dh++) {
                        for (int dw = -1; dw <= 1; dw++) {
                            if (dh == 0 && dw == 0) {
                                continue;
                            }

                            int next_h = cur_h + dh;
                            int next_w = cur_w + dw;
                            if (next_h < 0 || next_h >= h || next_w < 0 || next_w >= w) {
                                continue;
                            }
                            if (seen[next_h][next_w] == true) {
                                continue;
                            }
                            if (field[next_h][next_w] == 0) {
                                continue;
                            }
                            to_visit.push(make_pair(next_h, next_w));
                            seen[next_h][next_w] = true;
                        }
                    }
                }
            }
        }

        cout << cnt << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

#define MAX_L 50

int graph[MAX_L+2][MAX_L+2];

void search(int x, int y, int num){
	graph[x][y]=num;
	for(int i=-1; i<=1; i++){
	    for(int j=-1; j<=1; j++){
	      if(graph[x+i][y+j] == 1){search(x+i, y+j, num);}
	    }
	}
}

int main()
{
	int w, h, label;
 
	while(true){
		scanf("%d%d\n",&w, &h);
		if(w==0 && h==0){return 0;}

		for(int i=0; i<h+2; i++){
			for(int j=0; j<w+2; j++){
				graph[i][j]=0;
			}
		}

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

		label = 1;
		for(int i=1; i<h+1; i++){
			for(int j=1; j<w+1; j++){
				if(graph[i][j]==1){
					label++;
					search(i,j,label);
				}
			}
		}
		cout << label-1 << endl;		
	}	
}