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>
#include <queue>
using namespace std;
const int dx[]={1,0,-1,0,1,-1,1,-1}, dy[]={0,-1,0,1,1,-1,-1,1};

bool valid(int x,int y,int W,int H)
{
    return (x>=0&&x<W)&&(y>=0&&y<H);
}
int main()
{
    int W,H,i,j,k,x,y;
    queue<int> Q;
    cin >> H >> W;
    while(W && H)
    {
        int num=0;
        int A[W][H];
        for(i=0;i<W;i++)
            for(j=0;j<H;j++)
                cin >> A[i][j];
        for(i=0;i<W;i++)
            for(j=0;j<H;j++)
            {
                if(A[i][j]==1)
                {
                    Q.push(i);
                    Q.push(j);
                    A[i][j]=0;
                    num++;
                    while(!Q.empty())
                    {
                        x=Q.front();
                        Q.pop();
                        y=Q.front();
                        Q.pop();
                        for(k=0;k<8;k++)
                        {
                            if(valid(x+dx[k],y+dy[k],W,H))
                            {
                                if(A[x+dx[k]][y+dy[k]]==1)
                                {
                                    Q.push(x+dx[k]);
                                    Q.push(y+dy[k]);
                                    A[x+dx[k]][y+dy[k]]=0;
                                }
                            }
                        }
                    }
                }
            }
        cout << num << endl;
        cin >> H >> W;
    }
    return 0;
}

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

void	dfs(int w, int h, int grid[50][50], bool seen[50][50],
			int dx[], int dy[], int x, int y)
{
	seen[y][x] = true;
	int	nx, ny;
	for (int i = 0; i < 8; ++i) {
		nx = x + dx[i];
		ny = y + dy[i];
		if (nx < 0 || w <= nx || ny < 0 || h <= ny)
			continue;
		if (grid[ny][nx] == 0)
			continue;
		if (seen[ny][nx])
			continue;
		dfs(w, h, grid, seen, dx, dy, nx, ny);
	}
}

int	main()
{
	int		w, h;
	int		grid[50][50];
	bool	seen[50][50];
	int		dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
	int		dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
	while (cin >> w >> h) {
		if (w == 0 && h == 0)
			return 0;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				cin >> grid[i][j];
				seen[i][j] = false;
			}
		}
		int	num_island = 0;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				if (grid[i][j] == 0)
					continue;
				if (seen[i][j])
					continue;
				++num_island;
				dfs(w, h, grid, seen, dx, dy, j, i);
			}
		}
		cout << num_island << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <numeric>
#include <algorithm>
#include <stack>
using namespace std;
int grid[50][50];
int w, h, ans;
void dfs(int i, int j){ //iが縦jが横
    if(i<0||j<0||grid[i][j]==0) return ;
    grid[i][j]=0;
    
    for(int n=-1; n<=1; ++n){
        for(int m=-1; m<=1; ++m){
            dfs(i+n, j+m);
        }
    }
    return ;
}
int main() {
    while(cin >> w >> h, w||h){
        
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cin >> grid[i][j];
            }
        }
        ans = 0;
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                if(grid[i][j]==1){
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
        
    return 0;
}

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

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

int W, H, maze[64][64];
bool visited[64][64];

bool check(int x, int y){
	return x >= 0 && x < W && y >= 0 && y < H && maze[y][x] == 1 && !visited[y][x];
}

void dfs(int x, int y){
	visited[y][x] = true;
	
	for(int i = 0; i < 8; ++i){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(check(nx, ny)){
			dfs(nx, ny);
		}
	}
	return;
}

int main() {
	while(cin >> W >> H && (W || H)){
		memset(visited, 0, sizeof(visited));
		
		for(int i = 0; i < H; ++i){
			for(int j = 0; j < W; ++j){
				scanf("%d", &maze[i][j]);
			}
		}
		int ans = 0;
		for(int i = 0; i < H; ++i){
			for(int j = 0; j < W; ++j){
				if(maze[i][j] == 1 && !visited[i][j]){
					ans++;
					dfs(j, i);
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

int w, h;
int graph[50][50];
int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy[] = {-1, -1, -1, 0, 1, 1, 1, 0};

void solve(int y, int x) {
    graph[y][x] = 0;
    for(int i = 0; i < 8; ++i) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(ny >= 0 && ny < h && nx >= 0 && nx < w && graph[ny][nx]) {
            solve(ny, nx);
        }
    }
}

int main() {

    while(cin >> w >> h, w | h) {

        int cnt = 0;

        for(int i = 0; i < h; ++i) {
            for(int j = 0; j < w; ++j) {
                cin >> graph[i][j];
            }
        }

        for(int i = 0; i < h; ++i) {
            for(int j = 0; j < w; ++j) {
                if(graph[i][j]) {
                    solve(i, j);
                    ++cnt;
                }
            }
        }

        cout << cnt << endl;

    }

}