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

#ifdef LOCAL
#include "../dump.hpp"
#else
#define dump(...)
#endif

#define rep(i,n) for(ll i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(ll i=(a),i##_cond=(b);i<i##_cond;++i)
#define ROF(i,a,b) for(ll i=(a)-1,i##_cond=(b);i>=i##_cond;--i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend() //sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
#define SUM(a) accumulate(all(a),0)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll,ll> pii;

const ll inf = 1ll<<62;
const ll mod = 1e9+7;

#define int ll

vi dx = {1, 0, -1, 0, 1, -1, -1, 1}, dy = {0, 1, 0, -1, 1, 1, -1, -1};

main()
{
  while(1){
    int h,w; cin >> w >> h;
    if(h == 0) break;
    vvi s(h,vi(w));
    rep(i,h) rep(j,w) cin >> s[i][j];
    vvi look(h,vi(w));

    int ans = 0;
    rep(i,h) rep(j,w){
      if(not look[i][j] and s[i][j]){
	ans++;
	queue<pii> q;

	q.push(pii(i,j));
	look[i][j] = 1;

	while(not q.empty()){
	  pii p = q.front();
	  q.pop();

	  rep(k, sz(dx)){
	    int nx = p.fst + dx[k], ny = p.snd + dy[k];
	    if(0 <= nx and nx < h and 0 <= ny and ny < w and s[nx][ny] and look[nx][ny] == 0){
	      q.push(pii(nx, ny));
	      look[nx][ny] = 1;
	    }
	  }
	}
      }
    }
    //dump(look);
    cout << ans << endl;
  }
}

C++: #include<bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		ans.push_back(res);

		if (w == 0&&h == 0) {
			break;
		}
	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}

C++: /*
    Problem 25
    https://onlinejudge.u-aizu.ac.jp/problems/1160
*/
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 55;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
int h, w;
vector<vector<bool>> visited(MAX, vector<bool>(MAX));
vector<vector<int>> G(MAX, vector<int>(MAX));
/* function */
void init() {
    // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) G[i][j] = false;
    // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) visited[i][j] = false;
    for (int i = 0; i < h*w; i++) G[i/w][i%w] = false;
    for (int i = 0; i < h*w; i++) visited[i/w][i%w] = false;
}
void inputG() { // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> G[i][j];
    for (int i = 0; i < h*w; i++) cin >> G[i/w][i%w];}
bool check(int x, int y) {
    if (x < 0 || h < x || y < 0 || w < y) return false;
    if (visited[x][y] || !G[x][y]) return false;
    return true;
}
void bfs(int sx, int sy) {
    queue<pii> q;
    q.push(pii(sx, sy));
    visited[sx][sy] = true;
    while (!q.empty()) {
        pii p = q.front(); q.pop();
        int x = p.first, y = p.second;
        // 八方向を探索
        for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) {
            int nx = x + dx, ny = y + dy;
            if (check(nx, ny)) {
                q.push(pii(nx, ny));
                visited[nx][ny] = true;
            }
        }
    }
}
void dfs(int x, int y) {
    // 訪れた
    visited[x][y] = true;
    // 八方向を探索
    for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) {
        int nx = x + dx, ny = y + dy;
        // 範囲外 or 訪れたことがある(visited[i][j] == true) or 海(G[i][j] = 0)
        if (check(nx, ny))
            // 再帰的に探索
            dfs(nx, ny);
    }
}
void solve() {
    int ans = 0;
    for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
        if (!visited[i][j] && G[i][j]) {
            ans++;
            //bfs(i, j);
            dfs(i, j);
        }
    }
    cout << ans << '\n';
}

/* main */
int main(){
    while (1) {
        cin >> w >> h;
        if (h == 0) return 0;
        init();
        inputG();
        solve();
    }
}