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 totalRow, totalColumn;


void dfs(int input[50][50], bool visitedIsland[50][50],int row, int col)
{
    //cout << row << " " << col << endl ;
    visitedIsland[row][col] = true;
    if (row < totalRow   && col < totalColumn )
    {
        //Up
        if (row > 0 && input[row - 1][col] == 1 && visitedIsland[row - 1][col] == false)
            dfs(input,visitedIsland,row - 1,col);
        //cout << "up " << row << " " << col << input[row - 1][col] << " " << visitedIsland[row - 1][col] << endl;
        //Down
        if ((row + 1 <= totalRow - 1) && input[row + 1][col] == 1 && visitedIsland[row + 1][col]== false)
            dfs(input,visitedIsland,row + 1,col);
        //cout << "down " << input[1][col] << " " <<visitedIsland[row + 1][col] << endl;
        //Left
        if (col > 0 && input[row][col-1] == 1 && visitedIsland[row][col-1]== false)
            dfs(input,visitedIsland,row,col - 1);
        //cout << "left" << endl;
        //Right
        if ((col + 1 <= totalColumn - 1)&&input[row][col + 1] == 1 && visitedIsland[row][col+1]== false)
            dfs(input,visitedIsland,row,col + 1);
        //Left bottom diagonal
        if (row > 0 && col > 0 && input[row - 1][col - 1] == 1 && visitedIsland[row - 1][col - 1]== false)
            dfs(input,visitedIsland,row - 1,col - 1);
        //cout << "Left bottom " << row << " " << col << input[row - 1][col-1] << " " << visitedIsland[row - 1][col-1] << endl;
        //Left top diagonal
        if ((row + 1 <= totalRow - 1) && col > 0 && input[row + 1][col - 1] == 1 && visitedIsland[row + 1][col - 1]== false)
            dfs(input,visitedIsland,row + 1,col - 1);
        //Right top diagonal
        if ((col + 1 <= totalColumn - 1)&&row > 0 && input[row - 1][col + 1] == 1 && visitedIsland[row - 1][col + 1]== false)
            dfs(input,visitedIsland,row - 1,col +1);
        //cout << "Right top " << row << " " << col << input[row - 1][col+1] << " " << visitedIsland[row - 1][col+1] << endl;
        //Right bottom diagonal
        if ((row + 1 <= totalRow - 1)&&(col + 1 <= totalColumn - 1)&& input[row + 1][col + 1] == 1 && visitedIsland[row + 1][col + 1]== false)
            dfs(input,visitedIsland,row + 1,col +1);
    }
    
}

int main(int argc, const char * argv[]) {
    int row, col;
    while (cin >> col >> row)
    {
        totalRow = row;
        totalColumn = col;
        int numofIsland = 0;
        int input[50][50] = {0};
        bool visitedIsland[50][50] = {false};
        for (int i= 0; i < row; i++)
            for (int j = 0; j < col; j++)
                cin >> input[i][j];
        //checking
        //cout << input[1][0] << " " << input[0][0]<< endl;
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                if (input[i][j] == 1 && !visitedIsland[i][j])
                {
                    //push neighbour
                    numofIsland++;
                    //processed = true;
                    //cout << i << " " << j << endl ;
                    dfs(input,visitedIsland,i,j);
                }
            }
        }
        if (row > 0 &&  col > 0)
            cout << numofIsland << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

int w, h;
int field[51][51];
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};

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

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

        if (nx < 0 || ny < 0 || nx >= w || ny >= h)
            continue;

        if (field[ny][nx] == 1)
            dfs(nx, ny);
    }
}

int main()
{
    while (cin >> w >> h, w || h)
    {
        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                cin >> field[y][x];
            }
        }

        int ans = 0;

        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                if (field[y][x] == 1)
                {
                    dfs(x, y);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}