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 <cmath>
#include <vector>

using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

int c[200][200];
int vis[200][200];
int w,h;


void dfs(int ci, int cj){
    vis[ci][cj] = true;
    int di[] = {0,1,0,-1, 1,1,-1,-1};
    int dj[] = {1,0,-1,0, -1,1,-1,1};
    rep(d,8){
        int ni = ci+di[d];
        int nj = cj+dj[d];
        if(ni < 0 || nj < 0) continue;
        if(ni >= h || nj >= w) continue;
        if(vis[ni][nj]) continue;
        if(c[ni][nj]==0) continue;
        dfs(ni,nj);
    }
}

int main(){
    while(cin >> w >> h){
        rep(i,h)rep(j,w) cin >> c[i][j], vis[i][j] = 0;
        int ans = 0;
        rep(i,h)rep(j,w){
            if(vis[i][j]) continue;
            if(c[i][j] == 0) continue;
            dfs(i,j);
            ans++;
        }
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int,int> P;

int W, H;
int map[50][50], used[50][50];

void walk(int x, int y){
    used[y][x] = 1;
    REP(i, 8){
        int nx = x+dx[i], ny = y+dy[i];
        if(0 <= nx && nx < W && 0 <= ny && ny < H && !used[ny][nx] && map[ny][nx] == 1){
            walk(nx, ny);
        }
    }
}

int main(){
    while(std::cin >> W >> H, W){
        REP(i, H){
            REP(j, W){
                std::cin >> map[i][j];
            }
        }

        REP(i, H){
            REP(j, W){
                used[i][j] = 0;
            }
        }

        int island_n = 0;
        REP(i, H){
            REP(j, W){
                if(map[i][j] == 1 && !used[i][j]){
                    walk(j, i);
                    island_n++;
                }
            }
        }

        std::cout << island_n << std::endl;
    }
}

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
				
				
void del_island(int i,int j,vector<vector<int>> &v){	
	v[i][j] = 0;
	if(v[i-1][j] == 1){
		del_island(i-1,j,v);
	}
	if(v[i+1][j] == 1){
		del_island(i+1,j,v);
	}
	if(v[i][j-1] == 1){
		del_island(i,j-1,v);
	}
	if(v[i][j+1] == 1){
		del_island(i,j+1,v);
	}
	if(v[i-1][j-1] == 1){
		del_island(i-1,j-1,v);
	}
	if(v[i-1][j+1] == 1){
		del_island(i-1,j+1,v);
	}
	if(v[i+1][j-1] == 1){
		del_island(i+1,j-1,v);
	}
	if(v[i+1][j+1] == 1){
		del_island(i+1,j+1,v);
	}

}


int count_island(int w, int h,vector<vector<int>> v){
	int count = 0;
	for(int i = 1 ; i <= h ;i++){
		for(int j = 1 ;j <= w ;j++){
			//cout << v[i][j] ;
			if(v[i][j] == 1){
				count++;
				//cout << "hello" <<endl;
				del_island(i,j,v);
			}
		}
	}
	return count;
}
	
int main(){ 
	int w,h;
	vector<vector<int>> v(60,vector<int>(60,0));
	int count;

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

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

		count = count_island(w,h,v);


		cout << count << endl;
	}
}

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

int w, h;
int sima[51][51];
int move_X[9] = {-1,-1,-1,0,0,0,1,1,1};
int move_Y[9] = {-1,0,1,-1,0,1,-1,0,1};

void dfs(int y, int x){
    
    // 現在の位置を海に変える
    sima[y][x] = 0;
    
    for(int i = 0; i < 9; i++){
        int next_X = x + move_X[i];
        int next_Y = y + move_Y[i];
        
        if(sima[next_Y][next_X] == 1 && next_X >= 0 && next_Y >= 0 && next_X < w && next_Y < h)
            dfs(next_Y, next_X);
    }
    
    return ;
    
}

int main(void){
    
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0) return 0;
    
        // 島と海の入力
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> sima[i][j];
            }
        }
        
        // 島の個数
        int ans = 0;
        
        // 島の個数を調べる
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(sima[i][j] == 1){
                    dfs(i, j);
                    ans++;
                }
            }
        }
        
        cout << ans << endl;
        
    }
    
    return 0;
    
}