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<bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i ,n) for(int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int>vint;
typedef vector<ll>vll;
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int h,w;
vector<vector<int>> c(51,vector<int>(51));
int di[] = {-1,0,1,0,-1,-1,1,1};
int dj[] = {0,-1,0,1,-1,1,-1,1};

bool dfs(int i,int j){
    if(0 == c[i][j])return false;
    c[i][j] = 0;
    for(int s=0; s<8; s++){
        int ni = i+di[s];
        int nj = j+dj[s];
        if(ni < 0 || nj < 0 || ni >= h || nj >= w)continue;
        if(0 == c[ni][nj])continue;
        dfs(ni,nj);
    }
    return true;
}

int main()
{
    bool flag = true;
    vint ans;
    while(flag){
        cin >> w >> h;
        if(0 == w && 0 == h){
            flag = false;
            break;
        }
        REP(i,h){
            REP(j,w){
                cin >> c[i][j];
            }
        }
        int cnt=0;
        REP(i,h){
            REP(j,w){
                if(dfs(i,j)){
                    cnt++;
                }
            }
        }
        ans.pb(cnt);
    }
    for(auto a:ans){
        cout << a << endl;
    }
}

C++: #include <stdio.h>
#include <iostream>
#include <queue>
#include <vector>


#define rep(i,a,b)(long long i=a;i<b;i++)

using namespace std;


struct POINT {
	int x;
	int y;
	POINT(int X, int Y){
		x = X;y = Y;
	}
};

queue<POINT>	usedWay;
int SizeX,SizeY;
int Map[50][50] = {0};
vector<int>	result;


void SearchIsland(int X, int Y){
	usedWay.push(POINT(X,Y));
	Map[X][Y] = 2;

	while(usedWay.empty() == false){
		POINT NowPos = usedWay.front();
		usedWay.pop();

		if(NowPos.y-1 >= 0){
			//	左上
			if(NowPos.x-1 >= 0 && Map[NowPos.x-1][NowPos.y-1] == 1){
				Map[NowPos.x-1][NowPos.y-1] = 2;
				usedWay.push(POINT(NowPos.x - 1,NowPos.y - 1));
			}
		
			//	真上
			if(					  Map[NowPos.x][NowPos.y-1] == 1){
				Map[NowPos.x][NowPos.y-1] = 2;
				usedWay.push(POINT(NowPos.x,NowPos.y - 1));
			}

			//	右上
			if( NowPos.x + 1 < SizeX && Map[NowPos.x+1][NowPos.y-1] == 1){
				Map[NowPos.x+1][NowPos.y-1] = 2;
				usedWay.push(POINT(NowPos.x + 1,NowPos.y - 1));
			}
		}

		//	左
		if(NowPos.x-1 >= 0 && Map[NowPos.x-1][NowPos.y] == 1){
			Map[NowPos.x-1][NowPos.y] = 2;
			usedWay.push(POINT(NowPos.x - 1,NowPos.y));
		}

		//	右
		if( NowPos.x + 1 < SizeX && Map[NowPos.x+1][NowPos.y] == 1){
			Map[NowPos.x+1][NowPos.y] = 2;
			usedWay.push(POINT(NowPos.x + 1,NowPos.y));
		}

		if(NowPos.y + 1 < SizeY){
			//	左下
			if(NowPos.x-1 >= 0 && Map[NowPos.x-1][NowPos.y+1] == 1){
				Map[NowPos.x-1][NowPos.y+1] = 2;
				usedWay.push(POINT(NowPos.x - 1,NowPos.y + 1));
			}
		
			//	真下
			if(					  Map[NowPos.x][NowPos.y+1] == 1){
				Map[NowPos.x][NowPos.y+1] = 2;
				usedWay.push(POINT(NowPos.x,NowPos.y + 1));
			}

			//	右下
			if( NowPos.x + 1 < SizeX && Map[NowPos.x+1][NowPos.y+1] == 1){
				Map[NowPos.x+1][NowPos.y+1] = 2;
				usedWay.push(POINT(NowPos.x + 1,NowPos.y + 1));
			}
		}


	}
}

int main(void){

	while(true){

		int NumIsland = 0;

		cin >> SizeX;
		cin >> SizeY;

		if(SizeX == 0 && SizeY == 0)
			break;

		int tmp = SizeX;
		SizeX = SizeY;
		SizeY = tmp;

		//	読み込み
		for(int i = 0 ; i < SizeX; ++i)
			for(int j = 0 ; j < SizeY ; ++j)
				cin >> Map[i][j];


		for(int i = 0 ; i < SizeX ; ++i){
			for(int j = 0 ; j < SizeY ; ++j){
				if(Map[i][j] == 1){
					SearchIsland(i,j);
					NumIsland++;
				}
			}
		}

		result.push_back(NumIsland);
	}

	for(auto it = result.begin() ; it != result.end(); ++it)
		cout << *it << endl;

	return 0;
}

C++: #include<iostream>
#include<cstring>
int map[99][99];
int xi[]={-1,-1,-1,0,0,1,1,1};
int yi[]={-1,0,1,-1,1,-1,0,1};

using namespace std;

void map_clear(int x,int y)
{
  map[x][y]=0;
  for(int i=0;i<8;i++)
    {
      if(x+xi>=0 && y+yi>=0 && map[x+xi[i]][y+yi[i]] == 1)
	map_clear(x+xi[i],y+yi[i]);
    }
}

int main()
{
  int w,h;
  for(;cin>>w>>h,w;)  
    {
      memset(map,0,sizeof(map));
      for(int i=0;i<h;i++)
	for(int j=0;j<w;j++)
	  cin>>map[i][j];
      int c=0;
      for(int i=0;i<h;i++)
	for(int j=0;j<w;j++)
	  if(map[i][j]==1)
	    {
	      c++;
	      //   cout<<i<<"  "<<j<<endl;
	      map_clear(i,j);
	    }
      cout<<c<<endl;
    }
}