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 <queue>
using namespace std;
#define PIAR pair<int, int>

int main(){
    int w, h;
    while(cin >> w >> h, w != 0 && h != 0){
        int field[52][52] = {{0}};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                cin >> field[i][j];
            }
        }

        queue<PIAR> st;
        int island = 0;
        int dx[] = {-1,  0, 1, 0, -1,  1, 1, -1};
        int dy[] = {0 , -1, 0, 1, -1, -1, 1,  1};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                //島を探す
                if(field[i][j] == 1){
                    st.push(PIAR(i, j));
                    field[i][j] = 0;
                    while(!st.empty()){
                    	int x = st.front().first;
                    	int y = st.front().second;
                        st.pop();
                        for(int k = 0; k < 8; k++){
                            int nextX = x + dx[k];
                            int nextY = y + dy[k];
                            if(field[nextX][nextY] == 1){
                                st.push(PIAR(nextX, nextY));
                                field[nextX][nextY] = 0;
                            }
                        }
                    }
                    island++;
                }

            }
        }

        cout << island << endl;
    }
}

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

int w,h;
int map[50][50];

void solve(int x,int y){
	if(map[y][x] == 1){
		map[y][x] = 0;
		for(int i=-1;i<2;i++){
			for(int j=-1;j<2;j++){
				if(i==0&&j==0){
					continue;
				}
				if(y+i >= 0 && y+i < h && x+i >= 0 && x+i <= w){
					solve(x+j,y+i);
				}
			}
		}
	}
}

int main(){
	while(true){
		int count=0;

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

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

		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(map[i][j] == 1){
					solve(j,i);
					count++;
				}
			}
		}

		cout << count << endl;
	}
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#define EPS 1e-10
using namespace std;
typedef long long llong;
vector<vector<char> >v;
int cnt;
int w, h;

bool dfs(int x, int y) {

	if (x < 0 || h <= x)return false;
	if (y < 0 || w <= y)return false;

	if (v[x][y] == '1') {
		v[x][y] = '0';
		dfs(x - 1, y - 1);
		dfs(x - 1, y);
		dfs(x - 1, y + 1);
		dfs(x, y - 1);
		dfs(x, y + 1);
		dfs(x + 1, y - 1);
		dfs(x + 1, y);
		dfs(x + 1, y + 1);
		return true;
	}

	return false;

}


int main() {
	while (cin >> w >> h, h) {
		v = vector<vector<char> >(h, vector<char>(w));
		cnt = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
			cin >> v[i][j];

			}
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (dfs(i, j))cnt++;
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define MOD 1000000007
#define INF 1e9
#define INFL 1e16
 
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
template<class T>using arr=vector<vector<T>>;
template<class T>void pr(T x){cout << x << endl;}
template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){cout << a[i] << " ";} cout << a[a.size()-1] << endl;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
 
arr<int> land;
int ans;
int h, w;

void dfs(int x, int y){
    land[x][y] = 0;
    FOR(i, -1, 2){
        FOR(j, -1, 2){
            if(i==0 && j==0) continue;
            if(x+i < 0 || x+i >= h || y+j < 0 || y+j >= w) continue;
            if(land[x+i][y+j]==1){
                dfs(x+i, y+j);
            }
        }
    }
}

int main()
{
    while(1){
        cin >> w >> h;
        if(w==0 && h==0) break;
        land.resize(h);
        rep(i, h) land[i].resize(w);

        rep(i, h) rep(j, w) cin >> land[i][j];
        ans = 0;
        rep(i, h) rep(j, w){
            if(land[i][j]==0) continue;
            ans++;
            dfs(i, j);
        }
        pr(ans);
    }
    return 0;}