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>
#include <map>

using namespace std;

typedef pair<int, int> P;

int main() {
    int w, h;
    int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
    int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
    int map[50][50];
    while (true) {
        int count = 0;
        cin >> w >> h;
        if (w == 0) {
            return 0;
        }
        for (int y=0; y<h; y++) {
            for (int x=0; x<w; x++) {
                cin >> map[x][y];
            }
        }
        for (int y0=0; y0<h; y0++) {
            for (int x0=0; x0<w; x0++) {
                queue<P> q;
                if (map[x0][y0] == 0) {
                    continue;
                }
                count++;
                q.push(P(x0, y0));
                while (!q.empty()) {
                    int x = q.front().first;
                    int y = q.front().second;
                    q.pop();
                    for (int i=0; i<8; i++) {
                        int nx = x+dx[i];
                        int ny = y+dy[i];
                        if (nx < 0 || w <= nx || ny < 0 || h <= ny) {
                            continue;
                        }
                        if (map[nx][ny] == 0) {
                            continue;
                        }
                        map[nx][ny] = 0;
                        q.push(P(nx, ny));
                    }
                }
            }
        }
        cout << count << endl;
    }
}

C++: #include<iostream>
#include<string>
using namespace std;

int w,h;
int maze[100][100];

void solve(int x,int y){

  if(x < 0 || y < 0 || x == w || y == h) return;
  if(!maze[y][x]) return;

  maze[y][x] = 0;

  for(int i=-1;i<2;i++){
    for(int j=-1;j<2;j++){
      solve(x+i,y+j);
    }
  }

}
int main(){

  while(true){
    cin >> w >> h;
    if(!w && !h) break;

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++) cin >> 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) solve(j,i), ans++;
      }
    }

    cout << ans << endl;

  }
}

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

// typedef
typedef unsigned long long ull;
typedef unsigned int uint;

// macro
#define REP(index, num) for (auto index = 0; index < num; index++)
#define REP1(index, num) for (auto index = 1; index <= num; index++)

#define MAX_NUM 60

// using namespace 
using namespace std;

const int ExistIsland(const int inX, const int inY);

// global
int W, H;
int c[MAX_NUM][MAX_NUM] = { 0 };

int main()
{
  while (1)
  {
    cin >> W >> H;

    // 入力が 0 0 なら終了
    if (W * H == 0)
      break;

    REP(hId, H)
      REP(wId, W)
      cin >> c[wId][hId];

    // 深さ優先探索
    int count = 0;
    REP(hId, H)
      REP(wId, W)
      count += ExistIsland(wId, hId);

    cout << count << endl;
  } 
}

const int ExistIsland(const int inX, const int inY)
{
  if (inX < 0 || W <= inX) return 0;
  if (inY < 0 || H <= inY) return 0;

  if (c[inX][inY] == 0)
    return 0;
  else
    c[inX][inY] = 0;

  //bool existFlag = true;

  // 周りに移動
  ExistIsland(inX + 1, inY);
  ExistIsland(inX + 1, inY + 1);
  ExistIsland(inX, inY + 1);
  ExistIsland(inX - 1, inY + 1);
  ExistIsland(inX - 1, inY);
  ExistIsland(inX - 1, inY - 1);
  ExistIsland(inX, inY - 1);
  ExistIsland(inX + 1, inY - 1);

  return 1;
}

C++: #include<iostream>
using namespace std;
struct Point
{
	int x;
	int y;
	Point(int x = 0, int y = 0) :x(x), y(y) {}
};
const int MAX = 50;
int map[MAX][MAX];
bool book[MAX][MAX];
void depthFSearch(int h, int w, Point start)
{
	int next[9][2] = { { -1,-1 },{ -1,0 },{ -1,1 },{ 0,-1 },{ 0,0 },{ 0,1 },{ 1,-1 },{ 1,0 },{ 1,1 } };
	for (int k = 0; k <= 8; k++)
	{
		int nx = start.x + next[k][0];
		int ny = start.y + next[k][1];
		if (nx < 0 || nx >= h || ny < 0 || ny >= w || !map[nx][ny])
			continue;
		if (!book[nx][ny] && map[nx][ny])
		{
			book[nx][ny] = true;
			Point p = { nx,ny };
			depthFSearch(h, w, p);
		}
	}
	return;
}
int main()
{
	while (true)
	{
		int w, h;
		cin >> w >> h;
		if (!w && !h)
			break;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				cin >> map[i][j];
				book[i][j] = false;
			}
		int sum = 0;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				if (!book[i][j] && map[i][j])
				{
					book[i][j] = true;
					Point start(i, j);
					depthFSearch(h, w, start);
					sum++;
				}
			}
		cout << sum << endl;
	}
}