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<vector>
#include<stack>
using namespace std;

typedef long long itemType;

struct node
{long long v; struct node *next;};

void visit(stack<long long> stack, long long k, struct node *z, struct node **adj, long long id, long long val[]){
    struct node *t;
    t = new node;
    stack.push(k);
    while(!stack.empty()){
        k = stack.top();
        stack.pop();
        val[k] = ++id;
        for(t = adj[k]; t != z; t = t -> next){
            if(val[t -> v] == 0){
                stack.push(t -> v);
                val[t -> v] = -1;
            }
        }
    }
}

void search(stack<long long>stack, long long V, struct node *z, struct node **adj, long long id, long long val[], long long &comp, vector<vector<bool> > c, long long w){
    long long k;
    for(k = 1; k <= V; k++) val[k] = 0;
    for(k = 1; k <= V; k++){
        if(val[k] == 0){
            if(c[(k-1)/w + 1][((k-1)%w) + 1]) comp++;
            visit(stack, k, z, adj, id, val);
        }
    }
}

int main(){
    long long w, h;
    vector<bool> sub;
    bool c_sub;
    long long i, j;
    for(i = 0; i <= 50; i++){
        sub.push_back(0);
    }
    vector<vector<bool> > c;
    for(i = 0; i <= 50; i++){
        c.push_back(sub);
    }
    long long V;
    struct node *z;
    z = new node;
    z -> next = z;
    struct node *adj[2501];
    struct node *t;
    long long x, y;
    while(1){

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

        //adjacency list representation
        V = w * h;
        stack<long long> stack;
        for(i = 1; i <= V; i++){
            adj[i] = z;
        }
        for(j = 1; j <= w; j++){
            for(i = 1; i <= h-1; i++){
                if(c[i][j] & c[i+1][j]){
                    x = (i-1)*w + j;
                    y = i*w + j;
                    t = new node;
                    t -> v = x;
                    t -> next = adj[y];
                    adj[y] = t;
                    t = new node;
                    t -> v = y;
                    t -> next = adj[x];
                    adj[x] = t;
                    
                }
            }
        }
        
        for(i = 1; i <= h; i++){
            for(j = 1; j <= w-1; j++){
                if(c[i][j] & c[i][j+1]){
                    x = (i-1)*w + j;
                    y = (i-1)*w + j+1;
                    t = new node;
                    t -> v = x;
                    t -> next = adj[y];
                    adj[y] = t;
                    t = new node;
                    t -> v = y;
                    t -> next = adj[x];
                    adj[x] = t;
                    
                }
            }
        }
        for(i = 1; i <= h-1; i++){
            for(j = 1; j <= w-1; j++){
                if(c[i][j] & c[i+1][j+1]){
                    x = (i-1)*w + j;
                    y = i*w + j+1;
                    t = new node;
                    t -> v = x;
                    t -> next = adj[y];
                    adj[y] = t;
                    t = new node;
                    t -> v = y;
                    t -> next = adj[x];
                    adj[x] = t;
                    
                }
            }
        }
        for(i = 1; i <= h-1; i++){
            for(j = 1; j <= w-1; j++){
                if(c[i+1][j] & c[i][j+1]){
                    x = i*w + j;
                    y = (i-1)*w + j+1;
                    t = new node;
                    t -> v = x;
                    t -> next = adj[y];
                    adj[y] = t;
                    t = new node;
                    t -> v = y;
                    t -> next = adj[x];
                    adj[x] = t;
                    
                }
            }
        }

        //depth first search
        long long id = 0;
        long long val[V + 1];
        long long comp = 0;
        search(stack, V, z, adj, id, val, comp, c, w);
        cout << comp << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

int c[50][50];

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

int ret;

int w,h;

void dfs(int x,int y){
	if(c[x][y] == 0) return;
	c[x][y] = 0;
	for(int i=0;i<8;i++){
		if(x+dx[i] < 0 || x+dx[i] >= w) continue;
		if(y+dy[i] < 0 || y+dy[i] >= h) continue;
		dfs(x+dx[i],y+dy[i]);
	}
}

int main(){
	while(cin >> w >> h,w){
		ret = 0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> c[j][i];
			}
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(c[i][j] == 1){
					ret++;
					dfs(j,i);
				}
			}
		}
		cout << ret << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

#define vec vector
#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,-1, 0, 1,-1, 0, 1};

int dp[55][55];
int c[55][55];

int check(int x, int y)
{
	if (dp[x][y]) return 0;
	if (c[x][y] == 0) return 0;
	dp[x][y] = 1;
	
	rep(i, 8) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		int take = check(xx, yy);
	}
	return 1;
}

int main(void)
{
	while(1) {
		int w, h;
		cin >> w >> h;
		if (w == 0) break;
		rep(i, 55) rep(j, 55) c[i + 1][j + 1] = 0;
		rep(i, h) rep (j, w) cin >> c[i + 1][j + 1];
		rep(i, 55) rep(j, 55) dp[i][j] = 0;
		rep(i, 55) {
			dp[0][i] = 1;
			dp[i][0] = 1;
			dp[h + 1][i] = 1;
			dp[i][w + 1] = 1;
		}

		int ans = 0;
		rep(i, h) rep(j, w) if (c[i + 1][j + 1]) ans += check(i + 1, j + 1);
		cout << ans << endl;
	}
	return 0;
}