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 <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9+7;
using Graph = vector<vector<ll>>;
using pll = pair<ll, ll>;
template<class T> inline bool chmin(T &a, T b) { if(a > b){ a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b) { if(a < b){ a = b; return true;} return false;}

// 0 false, 1 true 
// string to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

ll w, h; 
vector<pll> direction = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
//vector<vector<ll>> vec(h, vector<ll>(w));
//vector<vector<bool>> seen(h, vector<bool>(w));

void dfs(vector<vector<ll>> &vec, vector<vector<bool>> &seen, ll y, ll x){
    seen[y][x] = true;

    /*
    cout << "---" << endl;
    rep(i, h){
        rep(j, w){
            cout << vec[i][j] << " ";
        }
        br;
    }
    cout << "---" << endl;
    */

    if(vec[y][x] == 1){
        for(ll i = 0; i < direction.size(); i++){
            ll next_y = y + direction[i].first;
            ll next_x = x + direction[i].second;

            //場外なら終わり
            if(next_y < 0 || next_y >= h){
                continue;
            }
            if(next_x < 0 || next_x >= w){
                continue;
            }

            if(seen[next_y][next_x] == false && vec[next_y][next_x] == 1){
                dfs(vec, seen, next_y, next_x);
            }
        }
    }
}

int main() {
    std::cout << std::fixed << std::setprecision(15);

    vector<ll> res;

    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0){
            break;
        }
        vector<vector<ll>> vec(h, vector<ll>(w));
        vector<vector<bool>> seen(h, vector<bool>(w));
        vec.resize(h, vector<ll>(w, 0));
        seen.resize(h, vector<bool>(w, false));
        
        rep(i, h){
            rep(j, w){
                cin >> vec[i][j];
            }
        }

        ll ans = 0;
        rep(i, h){
            rep(j, w){
                if(vec[i][j] == 1 && seen[i][j] == false){
                    ans++;
                    dfs(vec, seen, i, j);
                }
            }
        }
        res.push_back(ans);

    }

    rep(i, res.size()){
        cout << res[i] << endl;
    }

}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int> P;

int di[] = { 1, 1, 1, 0, 0,-1,-1,-1};
int dj[] = { 1, 0,-1, 1,-1, 1, 0,-1};


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int w,h;
    while(cin >> w >> h){
        if(!w && !h) break;

        int island[h][w];
        rep(i,h) rep(j,w) cin >> island[i][j];

        stack<P> stack;

        int ans = 0;
        rep(i,h) rep(j,w) {
            if(island[i][j] == 0) continue;
            ans++;
            stack.emplace(i,j);
            while(!stack.empty()){
                P p = stack.top(); stack.pop();
                int i = p.first, j = p.second;
                island[i][j] = 0;
                rep(dir,8){
                    int ni = i + di[dir];
                    int nj = j + dj[dir];
                    if(ni<0||ni>=h||nj<0||nj>=w) continue;
                    if(island[ni][nj] == 0) continue;
                    stack.emplace(ni,nj);
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}

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

#define REP(i, n) for(int i = 0; i < n; i++)

const int MAX_N = 50;
int w, h; // wは横の個数 hは縦の個数
int c[MAX_N][MAX_N]; //マップ
int dx = 1, dy = 1; //移動単位
int island; //記憶

void sch(int x, int y) { //ひとつ前のx座標とy座標

	c[x][y] = 0;

	if ((x + dx < w) && (c[x + dx][y] == 1)) {
		sch(x + dx, y);
		return;
	}
	if (((x - dx >= 0) && (c[x - dx][y] == 1))) {
		sch(x - dx, y);
		return;
	}
	if ((y + dy < h) && (c[x][y + dy] == 1)) {
		sch(x, y + dy);
		return;
	}
	if ((y - dy >= 0) && (c[x][y - dy] == 1)) { 
		sch(y, y - dy); 
		return;
	}
	if (x + dx < w && y + dy < h && c[x + dx][y + dy] == 1) {
		sch(x + dx, y + dy); 
		return;
	}
	if (x - dx >= 0 && y + dy < h && c[x - dx][y + dy] == 1) {
		sch(x - dx, y + dy);
		return;
	}
	if (x + dx < w && y - dy >= 0 && c[x + dx][y - dy] == 1) {
		sch(x + dx, y - dy);
		return;
	}
	if (x - dx >= 0 && y - dy >= 0 && c[x - dx][y - dy] == 1) {
		sch(x - dx, y - dy);
		return;
	}

	island++;
	return;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

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

		REP(i, w) {
			REP(j, h) cin >> c[i][j];
		}

		REP(i, w) {
			REP(j, h) if (c[i][j] == 1) sch(i, j);
		}
		cout << island << "\n";
	}

	return 0;
}