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;
int p[53][53];

class point{
public:
	int x, y;
	point(int newx, int newy){
		x = newx;
		y = newy;
	}
};

void findisland(int i, int j){
	p[i][j] = 2;
	if (p[i - 1][j - 1] == 1)
		findisland(i - 1, j - 1);
	if (p[i][j - 1] == 1)
		findisland(i, j - 1);
	if (p[i + 1][j - 1] == 1)
		findisland(i + 1, j - 1);
	if (p[i - 1][j] == 1)
		findisland(i - 1, j);
	if (p[i + 1][j] == 1)
		findisland(i + 1, j);
	if (p[i - 1][j + 1] == 1)
		findisland(i - 1, j + 1);
	if (p[i][j + 1] == 1)
		findisland(i, j + 1);
	if (p[i + 1][j + 1] == 1)
		findisland(i + 1, j + 1);
}


int main()
{
	int w, h, count;
	while (cin >> w >> h, (w | h)){

		//初期化
		for (int i = 0; i < 53; i++){
			for (int j = 0; j < 53; j++){
				p[i][j] = 0;
			}
		}
		count = 0;

		//入力
		for (int i = 1; i < h + 1; i++){
			for (int j = 1; j < w + 1; j++){
				cin >> p[i][j];
			}
		}

		//探索
		for (int i = 1; i < h + 1; i++){
			for (int j = 1; j < w + 1; j++){
				if (p[i][j] == 1){
					findisland(i, j);
					count++;
				}
			}
		}
		cout << count << endl;



	}
	return 0;
}

C++: #include <iostream>

using namespace std;

int W, H;
int field[50][50];

void input(void){
	cin >> W;
	cin >> H;

	for(int i = 0;i < H;i++){
		for(int j = 0;j < W;j++){
			cin >> field[i][j];
		}
	}
}

void dfs(int x, int y){
	field[x][y] = 0;

	for(int dx = -1;dx <= 1;dx++){
		for(int dy = -1;dy <= 1;dy++){
			int nx = x + dx, ny = y + dy;
			if(0 <= nx && nx <= W && 0 <= ny && ny <= H && field[nx][ny] == 1) dfs(nx, ny);
		}
	}

	return ;
}

int main(){
	while(1){
		int res = 0;
		input();
		if(W == 0 && H == 0) break;

		for(int i = 0;i < H;i++){
			for(int j = 0;j < W;j++){
				if(field[i][j] == 1){
					dfs(i, j);
					res++;
				}
			}
		}

		cout << res << endl;
	}

	return 0;
}

C++: #include <iostream>
using namespace std;
int w, h, cnt;
int c[51][51];
void dfs(int i, int j){
    if(i-1 >= 0 && j-1 >= 0){
        if(c[i-1][j-1] == 1){
            c[i-1][j-1] = 0;
            dfs(i-1,j-1);
        }
    }
    if(i-1 >= 0){
        if(c[i-1][j] == 1){
            c[i-1][j] = 0;
            dfs(i-1,j);
        }
    }
    if(i-1 >= 0 && j+1 < w){
        if(c[i-1][j+1] == 1){
            c[i-1][j+1] = 0;
            dfs(i-1,j+1);
        }
    }
    if(j-1 >= 0){
        if(c[i][j-1] == 1){
            c[i][j-1] = 0;
            dfs(i,j-1);
        }
    }
    if(j+1 < w){
        if(c[i][j+1] == 1){
            c[i][j+1] = 0;
            dfs(i,j+1);
        }
    }
    if(i+1 < h && j-1 >= 0){
        if(c[i+1][j-1] == 1){
            c[i+1][j-1] = 0;
            dfs(i+1,j-1);
        }
    }
    if(i+1 < h){
        if(c[i+1][j] == 1){
            c[i+1][j] = 0;
            dfs(i+1,j);
        }
    }
    if(i+1 < h && j+1 < w){
        if(c[i+1][j+1] == 1){
            c[i+1][j+1] = 0;
            dfs(i+1,j+1);
        }
    }

}

int main(void){
    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 >> c[i][j];
            }
        }

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

C++: #include <iostream>
#include <vector>
#include <stack>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int w,h;
	cin >> w;
	cin >> h;

	while (w!=0||h!=0) {
		int count = 0;
		std::stack<int> st_x,st_y;
		std::stack<int> st_xn,st_yn;
		int xn,yn;
		int c;
		bool all = false;
		std::vector< std::vector<bool> > island(w+2, std::vector<bool>(h+2));
		for (int i = 0; i < h+2; i++) {
			for (int j = 0; j < w+2; j++) {
				if (i==0||i==h+1||j==0||j==w+1) {
					island[i][j] = false;
				} else {
					cin >> c;
					if (c==1) {
						island[i][j] = true;
						st_x.push(j);
						st_y.push(i);
					} else {
						island[i][j] = false;
					}
				}
			}
		}

		while (!st_x.empty()) {
			xn = st_x.top();
			yn = st_y.top();
			st_x.pop();
			st_y.pop();
			if (island[xn][yn]) {
				island[xn][yn] = false;
				count++;
				st_xn.push(xn);
				st_yn.push(yn);

				while (!st_xn.empty()) {
					xn = st_xn.top();
					yn = st_yn.top();
					st_xn.pop();
					st_yn.pop();
					if (island[xn+1][yn]) {
						st_xn.push(xn+1);
						st_yn.push(yn);
						island[xn+1][yn] = false;
					}
					if (island[xn-1][yn]) {
						st_xn.push(xn-1);
						st_yn.push(yn);
						island[xn-1][yn] = false;
					}
					if (island[xn+1][yn+1]) {
						st_xn.push(xn+1);
						st_yn.push(yn+1);
						island[xn+1][yn+1] = false;
					}
					if (island[xn+1][yn-1]) {
						st_xn.push(xn+1);
						st_yn.push(yn-1);
						island[xn+1][yn-1] = false;
					}
					if (island[xn-1][yn+1]) {
						st_xn.push(xn-1);
						st_yn.push(yn+1);
						island[xn-1][yn+1] = false;
					}
					if (island[xn-1][yn-1]) {
						st_xn.push(xn-1);
						st_yn.push(yn-1);
						island[xn-1][yn-1] = false;
					}
					if (island[xn][yn+1]) {
						st_xn.push(xn);
						st_yn.push(yn+1);
						island[xn][yn+1] = false;
					}
					if (island[xn][yn-1]) {
						st_xn.push(xn);
						st_yn.push(yn-1);
						island[xn][yn-1] = false;
					}
				}
			}
		}
		cout << count << endl;

		cin >> w;
		cin >> h;
	}
		return 0;
}