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>

using namespace std;

bool isVisited[50][50] = { false };
int isLand[50][50] = { 0 };

void dfs(int x, int y, int w, int h)
{
  if (x < 0 || x >= w || y < 0 || y >= h) return;
  if (isVisited[x][y] == true) return;
  if (isLand[x][y] == 0) return;

  isVisited[x][y] = true;

  int dx[8] = { -1, 0, 1, -1, 1, -1,  0,  1 };
  int dy[8] = {  1, 1, 1,  0, 0, -1, -1, -1 };

  for (int i = 0; i < 8; i++) {
    dfs(x + dx[i], y + dy[i], w, h);
  }
}

int main()
{
  int w, h;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;
    
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> isLand[j][i];
        isVisited[j][i] = false;
      }
    }

    int count = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (isVisited[j][i] == false && isLand[j][i] == 1) {
          dfs(j, i, w, h);
          count++;
        }
      }
    }
    cout << count << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define FOR(i, m, n) for(ll i = m; i < n; i++)
#define all(vec) vec.begin(), vec.end()
#define tmax(x, y, z) max((x), max((y), (z)))
#define tmin(x, y, z) min((x), min((y), (z)))
ll MM = 1000000000; ll mod = MM + 7; ll MMM=9223372036854775807;//2^63 -1
ll GCD(ll x, ll y){ if(y == 0) return x; else return GCD(y, x % y);}
ll LCM(ll x, ll y){ return x / GCD(x, y) * y;}
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;}
const ll INF = 1LL << 60;
using Graph = vector<vector<ll>>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
//cout << fixed << setprecision(10);

int field[100][100];
ll w, h;


void dfs(ll y, ll x){
    field[y][x] = 0;
    rep(i, 8){
        ll ny = y + ddy[i], nx = x + ddx[i];
        if(0 <= ny && ny < h && 0 <= nx && nx < w && field[ny][nx]){
            dfs(ny, nx);
        }
    }
}


int main(){
    while(1){
        cin >> w >> h;
        if(!w && !h) break;
        rep(hi, h){
            rep(wi, w){
                cin >> field[hi][wi];
            }
        }

        ll ans = 0;
        rep(hi, h){
            rep(wi, w){
                if(field[hi][wi]){
                    dfs(hi, wi);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <unordered_map>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
typedef long long ll;

using namespace std;

template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

typedef vector<vector<int>> vv;

int hashp(int i, int j){
    return i*100 + j;
}

void bfs(const vv& table, vv& is_visited, int si, int sj) {
    queue<int> q;
    q.push(hashp(si,sj));
    while(!q.empty()) {
        int hash_value = q.front();
        q.pop();
        int i = hash_value/100;
        int j = hash_value%100;

        if(i >= table.size() || i < 0 || j >= table[0].size() || j < 0)
            continue;
        if(!table[i][j])
            continue;
        if(is_visited[i][j])
            continue;
        is_visited[i][j] = 1;
        q.push(hashp(i+1,j));
        q.push(hashp(i-1,j));
        q.push(hashp(i,j+1));
        q.push(hashp(i,j-1));
        q.push(hashp(i+1,j+1));
        q.push(hashp(i+1,j-1));
        q.push(hashp(i-1,j+1));
        q.push(hashp(i-1,j-1));
        // cout << "pushed" << endl;
    }
    return;
}

int main(){
    int w,h;
    while(cin >> w >> h) {
        if (w == 0 && h ==0)
            break;
        vector<vector<int>> table;
        vector<vector<int>> is_visited;
        rep(i, h) {
            vector<int> tmp(w, 0);
            table.push_back(tmp);
            is_visited.push_back(tmp);
            rep(j, w) {
                cin >> table[i][j];
            }
        }
        int count = 0;
        rep(i, h) {
            rep(j, w) {
                if(table[i][j] == 1 && is_visited[i][j] == 0) {
                    count++;
                    bfs(table, is_visited, i, j);
                }
            }
        }
        cout << count << endl;
    }
}