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 <sstream>

using namespace std;

void dfs(int i, int j, int** c,bool **used, int h, int w){
		if(used[i][j])return;
		used[i][j] = true;
		for(int x = -1; x < 2; x++){
				for(int y = -1; y < 2; y++){
						int dx = i + x;
						int dy = j + y;
						if(dx > -1 && dx < h && dy > -1 && dy < w){
								if(!used[dx][dy] && c[dx][dy] == 1){
										dfs(dx, dy, c, used, h, w);
								}
						}
				}
		}
}

int main(int argc, char const* argv[])
{
	int w, h;
	cin >> w >> h;
	while(w != 0 || h != 0){
			int **c = new int*[h];
			bool **used = new bool*[h];
			for(int i = 0; i < h; i++){
					c[i] = new int[w];
					used[i] = new bool[w];
					for(int j = 0; j < w; j++){
							cin >> c[i][j];
							used[i][j] = false;
					}
			}
			int res = 0;
			for(int i = 0; i < h; i++){
					for(int j = 0; j < w; j++){
							if(used[i][j])continue;
							if(c[i][j] == 0)continue;
							dfs(i, j, c, used, h, w);
							res++;
					}
			}
			cout << res << endl;
			cin >> w >> h;
	}
	return 0;
}

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

bool MAP[50][50];
int X, Y;
const int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1},
          dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

void init() {
  for (int i = 0; i < 50; i++)
    for (int j = 0; j < 50; j++)
      MAP[i][j] = false;
}

void sink_one_island(int x, int y) {
  if (x < 0 or X < x or y < 0 or Y < y or !MAP[x][y])
    return;

  MAP[x][y] = false;
  for (int i = 0; i < 8; i++)
    sink_one_island(x + dx[i], y + dy[i]);
}

int sink_all_islands() {
  int islands = 0;

  for (int x = 0; x < X; x++) {
    for (int y = 0; y < Y; y++) {
      if (MAP[x][y]) {
        sink_one_island(x, y);
        islands++;
      }
    }
  }

  return islands;
}

int main() {
  while (cin >> X >> Y and (X != 0 or Y != 0)) {
    int s;

    for (int y = 0; y < Y; y++) {
      for (int x = 0; x < X; x++) {
        cin >> s;
        MAP[x][y] = (s == 1)? true:false;
      }
    }

    int ids = sink_all_islands();

    cout << ids << endl;
  }
}

C++: #include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <tuple>
#include <set>
#include <stack>
#include <queue>
#define REP(i, n) for(LL i = 0;i < n;i++)
#define REPR(i, n) for(LL i = n;i >= 0;i--)
#define FOR(i, m, n) for(LL i = m;i < n;i++)
#define FORR(i, m, n) for(LL i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

#define INF 999999999
#define MOD 1000000007
using namespace std;
typedef long long LL;
//typedef LL int;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> PP;
typedef pair<LL, LP> LPP;
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

int main(){
    int w, h;
    while(cin >> w >> h, w + h){
        int c[51][51];
        REP(i, h){
            REP(j, w){
            cin >> c[i][j];
            }
        }

        int ans = 0;

        REP(i, h){
            REP(j, w){
                if(c[i][j] == 2) continue;
                else if(c[i][j] == 0) continue;
                else if(c[i][j] == 1){
                    queue<int> que;
                    que.push(i); que.push(j);
                    
                    while(!que.empty()){
                        int x, y;
                        y = que.front(); que.pop();
                        x = que.front(); que.pop();
                        
                        c[y][x] = 0;

                        if(y-1 >= 0 && y-1 < h && x-1 >= 0 && x-1 < w && c[y-1][x-1] == 1){
                            que.push(y-1); que.push(x-1);
                        }else if(y-1 >= 0 && y-1 < h && x >= 0 && x < w && c[y-1][x] == 1){
                            que.push(y-1); que.push(x);
                        }else if(y-1 >= 0 && y-1 < h && x+1 >= 0 && x+1 < w && c[y-1][x+1] == 1){
                            que.push(y-1); que.push(x+1);
                        }else if(y >= 0 && y < h && x-1 >= 0 && x-1 < w && c[y][x-1] == 1){
                            que.push(y); que.push(x-1);
                        }else if(y >= 0 && y < h && x+1 >= 0 && x+1 < w && c[y][x+1] == 1){
                            que.push(y); que.push(x+1);
                        }else if(y+1 >= 0 && y+1 < h && x-1 >= 0 && x-1 < w && c[y+1][x-1] == 1){
                            que.push(y+1); que.push(x-1);
                        }else if(y+1 >= 0 && y+1 < h && x >= 0 && x < w && c[y+1][x] == 1){
                            que.push(y+1); que.push(x);
                        }else if(y+1 >= 0 && y+1 < h && x+1 >= 0 && x+1 < w && c[y+1][x+1] == 1){
                            que.push(y+1); que.push(x+1);
                        }
                    }
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }
}