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;

void search(int y, int x, int h, int w, int** field) {
    field[y][x] = 0;
    for (int ny = y - 1; ny <= y + 1; ny++) {
        for (int nx = x - 1; nx <= x + 1; nx++) {
            if (0 <= ny && ny < h && 0 <= nx && nx < w && field[ny][nx] == 1) {
                search(ny, nx, h, w, field);
            }
        }
    }
}

int main() {
    while (true) {
        int w, h;
        cin >> w >> h;
        if ((w | h) == 0) {
            break;
        }
        int** field = new int* [h];
        for (int i = 0; i < h; i++) {
            field[i] = new int [w];
        }
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                cin >> field[y][x];
            }
        }
        int count = 0;
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (field[y][x] == 1) {
                    search(y, x, h, w, field);
                    count++;
                }
            }
        }
        cout << count << endl;
        for (int i = 0; i < h; i++) {
            delete[] field[i];
        }
        delete[] field;
    }
}

C++: #include<iostream>
#include<queue>
using namespace std;
int dx[] = {1,1,0,-1,-1,-1,0,1};
int dy[] = {0,1,1,1,0,-1,-1,-1};
int c[51][51];
int main()
{

	int w, h;

	while (cin >> w && cin >> h && w != 0 && h != 0)
	{
		for (int i = 1; i < h + 1; i++)
		{
			for (int j = 1; j < w + 1; j++)
			{
				int tmp;
				cin >> tmp;
				if (tmp == 0)
				{
					c[i][j] = -1;
				}
				else
				{
					c[i][j] = 0;
				}
			}
		}

		int count = 0;
		queue<pair<int,int>> q;
		for (int i = 1; i < h + 1; i++)
		{
			for (int j = 1; j < w + 1; j++)
			{
				if (c[i][j] != 0)continue;
				q.push(make_pair(i,j));
				c[i][j] = 1;
				while (q.size() > 0)
				{
					pair<int, int> p = q.front();
					q.pop();
					int y = p.first;
					int x = p.second;

					for (int d = 0; d < 8; d++)
					{
						int nx = x + dx[d];
						int ny = y + dy[d];

						if (nx < 1 || ny < 1 || nx > w || ny > h)continue;
						if (c[ny][nx] != 0)continue;
						q.push(make_pair(ny, nx));
						c[ny][nx] = 1;
					}
				}
				count++;
			}
		}

		cout << count << endl;


	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define pb         push_back

const int INF = 100000000;

int w, h;
vector<int> ma[60];

void rec(int y, int x){
	int ny, nx;
	if(ma[y][x] != 1){
		return;
	}
	ma[y][x] = 0;
	for(int i=-1; i<=1; i++){
		for(int j=-1; j<=1; j++){
			if(i == 0 && j == 0) continue;
			nx =  x + i;
			ny =  y + j;
			if(ny >= 0 && ny < h && nx >= 0 && nx < w){
				rec(ny, nx);
			}
		}
	}
}
int main(void) {
	while(1){
		for(int i=0; i<60; i++){
			ma[i] = vector<int>();
		}
		cin >> w >> h;
		if(w == 0 && h == 0) break;

		for(int i = 0; i < h; i++){
			for(int j=0; j<w; j++){
				int t;
				cin >> t;
				ma[i].pb(t);
			}
		}

		int ans = 0;
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				if(ma[i][j] == 1){
					ans++;
					rec(i, j);
				}
			}
		}
		/*
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				cout << ma[i][j] << ' ';
			}
			cout << endl;
		}
		*/
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <map>
#include <queue>
#include <vector>

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

using namespace std;
vector <vector<int> > seamap, visit;

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

void bfs(int x, int y){
    queue <pair <int, int> > Q;
    visit[x][y] = 1;
    Q.push(pair<int,int>(x, y));
    while(!Q.empty()){
        pair<int,int> cur = Q.front();
        Q.pop();
        REP(i,8){
            nx = cur.first + dx[i];
            ny = cur.second + dy[i];
            if(seamap[nx][ny] && !visit[nx][ny]){
                visit[nx][ny] = 1;
                Q.push(pair<int,int>(nx, ny));
            }
        }
    }
}

int main(){
    int w,h;
    while(cin >> w >> h && !(w == 0 && h == 0)){
        int island = 0;
        seamap.resize(w + 2);
        REP(i,w + 2){
            seamap[i] = vector<int>(h + 2, 0);
        }
        visit = seamap;
        FOR(y,1,h + 1){
            FOR(x,1,w + 1){
                cin >> seamap[x][y];
            }
        }
       FOR(x,1,w + 1){
            FOR(y,1,h + 1){
                if(seamap[x][y] && !visit[x][y]){
                    island++;
                    bfs(x,y);
                }
            }
        }
        cout << island << endl;
    }
}