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>
using namespace std;

// 入力
int H, W;
vector<vector<int>> field;

// 探索
void dfs(int h, int w) {
    field[h][w] = 0;

    // 八方向を探索
    for (int dh = -1; dh <= 1; ++dh) {
        for (int dw = -1; dw <= 1; ++dw) {
            int nh = h + dh, nw = w + dw;

            // 場外アウトしたり、0 だったりはスルー
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if (field[nh][nw] == 0) continue;

            // 再帰的に探索
            dfs(nh, nw);
        }
    }
}

int main() {
    // 入力受け取り
    while (cin >> W >> H) {
        if (H == 0 || W == 0) break;
        field.assign(H, vector<int>(W, 0));
        for (int h = 0; h < H; ++h) for (int w = 0; w < W; ++w) cin >> field[h][w];

        // 探索開始
        int count = 0;
        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                if (field[h][w] == 0) continue; // 残島じゃなかったらスルー
                dfs(h, w);
                ++count;
            }
        }
        cout << count << endl;
    }
}

C++: #include <stdio.h>
#include <string.h>

void island_delete(int data[][60], int w, int h, int self_i, int self_j) {
  data[self_i][self_j] = 0;
  if (self_j-1 >= 0 && data[self_i][self_j-1] == 1) {
	island_delete(data,w,h,self_i,self_j-1);
  }
  if (self_i-1 >= 0 && data[self_i-1][self_j] == 1) {
	island_delete(data,w,h,self_i-1,self_j);
  }
  if (self_j+1 <= w && data[self_i][self_j+1] == 1) {
	island_delete(data,w,h,self_i,self_j+1);
  }
  if (self_i+1 <= h && data[self_i+1][self_j] == 1) {
	island_delete(data,w,h,self_i+1,self_j);
  }

  if (self_j-1 >= 0 && self_i-1 >= 0 && data[self_i-1][self_j-1] == 1) {
	island_delete(data,w,h,self_i-1,self_j-1);
  }
  if (self_j+1 <= w && self_i-1 >= 0 && data[self_i-1][self_j+1] == 1) {
	island_delete(data,w,h,self_i-1,self_j+1);
  }
  if (self_j-1 >= 0 && self_i+1 <= h && data[self_i+1][self_j-1] == 1) {
	island_delete(data,w,h,self_i+1,self_j-1);
  }
  if (self_j+1 <= w && self_i+1 <= h && data[self_i+1][self_j+1] == 1) {
	island_delete(data,w,h,self_i+1,self_j+1);
  }


  
}

int main(void) {
  while(1) {
	int w, h;
	int cnt = 0;
	int data[60][60] = {{0}};
	scanf("%d%d", &w, &h);
	if (w == h && w == 0) break;
	for (int i = 0; i < h; i++) {
	  for (int j = 0; j < w; j++) {
		scanf("%d", &data[i][j]);
	  }
	}
	int self_i, self_j;
	for (int i = 0; i < h; i++) {
	  for (int j = 0; j < w; j++) {
		if (data[i][j] == 1) {
		  self_i = i; self_j = j; cnt++;
		  island_delete(data,w,h,self_i,self_j);
		}
	  }
	}
	printf("%d\n",cnt);
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

void dfs(vector< vector<int> > &island, int x, int y) {
    island[x][y] = 0;

    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            int nx = x + dx;
            int ny = y + dy;
            if (nx >= 0 && nx < island.size() && ny >= 0 && ny < island[0].size() && island[nx][ny] == 1) {
                dfs(island, nx, ny);
            }
        }
    }
}

int main() {
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }

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

        int res = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (island[i][j] == 1) {
                    dfs(island, i, j);
                    res++;
                }
            }
        }

        cout << res << endl;
    }

    return 0;
}

C++: #include<stdio.h>
#include <stdlib.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<time.h>
using namespace std;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define vint vector<int> 
#define vvint vector<vint>
#define ct(a) cout<<a<<endl
typedef long long ll;

int w, h,cn[1000];
bool field[51][51];

void dfs(int i,int k) {
	field[i][k] = 0;
	FOR(x, -1, 2) {
		FOR(y, -1, 2) {
			int nx = k + x, ny = i + y;
			if (nx < w&&nx >= 0 && ny < h&&ny >= 0 && field[ny][nx] == 1)dfs(ny, nx);
		}
	}
	return;
}

int main(void)
{
	int c = 0;
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0)break;
		FOR(i, 0, h)FOR(k, 0, w)cin >> field[i][k];

		FOR(i, 0, h)FOR(k, 0, w) {
			if (field[i][k] == 1) {
				dfs(i, k);
				cn[c]++;
			}
		}
		ct(cn[c]);
		c++;
	}

	return 0;
}

C++: #include <iostream>
#include <map>
// #include <cstdio>
#include <stack>

using namespace std;

int main() {
	while(1) {
	int w, h, data[52][52]={{0}};
	cin >> w >> h;
	if (w == 0 && h ==0) break;
	// memset(data, 0, sizeof(data));
	for (int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			cin >> data[i][j];
		}
	}
	// for (int i=0; i<h; i++) {
	// 	for (int j=0; j<w; j++) {
	// 		cout << data[i][j] << "," ;
	// 	}cout <<endl;
	// }
	stack<pair<int, int> > s;
	int count=0;
	int H[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, W[8] ={-1, 0, 1, -1, 1, -1, 0, 1};
	for(int i=1; i<=h; i++) {
		for (int j=1; j<=w; j++) {
			if (data[i][j]) {
				count++;
				s.push(make_pair(i, j));
				while (!(s.empty())) {
					pair<int, int> now;
					now = s.top();
					s.pop();
					data[now.first][now.second]=0;
					for (int k=0; k<8; k++) {
						if (data[H[k]+now.first][W[k]+now.second])
							s.push(make_pair(H[k]+now.first, W[k]+now.second));
					}
				}
			}
		}
	}
	cout << count <<endl;
}
}