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;
            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 <iostream>
using namespace std;
 
int main(void){
    while (1) {
        int Y, X;
        int c[55][55];
        cin >> X >> Y;
         
        //終了
        if (Y == 0 && X == 0) break;
         
        for (int i = 1; i <= Y; i++) {
            for (int j = 1; j <= X; j++) cin >> c[i][j];
        }
         
        //深さ別にリストを作成
        int vy[2555][155], vx[2555][155], pl[2555];
        for (int i = 0; i < 2000; i++) {
            pl[i] = 0;
            vy[i][0] = vx[i][0] = -1;
            for (int j = 1; j <= 553; j++) {
                vy[i][j] = vx[i][j] = 0;
            }
        }  
 
        //移動方向の指定
        int dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
        int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
     
        //通過済みかどうか
        bool psd[55][55];
        
        for (int i = 0; i <= 53; i++) {
            for (int j = 0; j <= 53; j++) psd[i][j] = false;
        }
        
        for (int i = 1; i <= Y; i++) {
            for (int j = 1; j <= X; j++) {
                psd[i][j] = true;
                if (c[i][j] == 0) psd[i][j] = false;
            }
        }
         
        for (int i = 0; i <= Y + 1; i++) psd[i][0] = psd[i][X + 1] = false;
        for (int i = 0; i <= X + 1; i++) psd[0][i] = psd[Y + 1][i] = false;
         
        bool fns = false;
        int isl = 0;
        while (1) {
            for (int inf = 0; true; inf++) {
                bool spot = false;
                if (inf == 0) {
                    vy[0][0] = -1;
                    for (int i = 1; i <= Y; i++) {
                        for (int j = 1; j <= X; j++) {
                            if (psd[i][j] == true) {
                                vy[0][0] = i;
                                vx[0][0] = j;
                                isl++;
                                pl[0] = 1;
                                psd[i][j] = false;
                                spot = true;
                                break;
                            }
                        }
                        if (spot) break;
                    }
                    if (vy[0][0] == -1) {
                        fns = true;
                        break; //4, 全部の島の確認終了
                    }
                }
                else {
                    int pt = 0;
                    for (int i = 0; i < pl[inf - 1]; i++) {
                        for (int dir = 0; dir < 8; dir++) {
                            
                            if (vy[inf - 1][i] + dy[dir] != -1 && vy[inf - 1][i] + dy[dir] != Y + 2 && vx[inf - 1][i] + dx[dir] != -1 && vx[inf - 1][i] + dx[dir] != X + 2) {
                                if (psd[vy[inf - 1][i] + dy[dir]][vx[inf - 1][i] + dx[dir]] == true) {
                                    vy[inf][pt] = vy[inf - 1][i] + dy[dir];
                                    vx[inf][pt] = vx[inf - 1][i] + dx[dir];
                                    pl[inf]++;
                                    psd[vy[inf][pt]][vx[inf][pt]] = false;
                                    pt++;
                                }
                            }
                        }
                    }
                    if (pt == 0) break; //4, 1つの島の確認終了
                }
//                cout << "*" << inf << endl;
//                for (int i = 0; i < pl[inf]; i++) cout << vy[inf][i] << ", " << vx[inf][i] << endl;
            }
            if (fns) {
                cout << isl << endl;
                break; // 2, query終了
            }
        }
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i,a) for(int i=0;i<(a);i++)
#define MOD 1000000007

int main(){
  while(1){
    int h,w;
    cin>>w>>h;
    if(h==0&&w==0) break;
    int a[h][w];
    bool used[h][w];
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin>>a[i][j];
        used[i][j]=0;
      }
    }
    int ans=0;
    queue<pair<int,int>> q;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(a[i][j]&&!used[i][j]){
          //cout<<i<<" "<<j<<endl;
          ans++;
          q.push(make_pair(i,j));
          while(!q.empty()){
            pair<int,int> p=q.front();
            q.pop();
            int x=p.first, y=p.second;
            if(used[x][y]) continue;
            used[x][y]=1;
            for(int i=-1;i<2;i++){
              for(int j=-1;j<2;j++){
                if(x+i==-1||x+i==h) continue;
                if(y+j==-1||y+j==w) continue;
                if(a[x+i][y+j]&&!used[x+i][y+j]){
                  q.push(make_pair(x+i,y+j));
                }
              }
            }
          }
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}