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<vector>
using namespace std;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,1,-1};

bool dfs(int sx,int sy,vector<vector<bool> >&c){
    if(c[sx][sy]==false) return false;
    c[sx][sy]=false;
    for(int i=0;i<8;i++){
        int nx=sx+dx[i];
        int ny=sy+dy[i];
        bool flag=dfs(nx,ny,c);
    }
    return true;
}

int main(void){
    int h,w,temp;
    while(1){
        cin>>w>>h;
        if(w==0) break;
        vector<vector<bool> > c(h+2,vector<bool>(w+2,false)) ;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                cin>>temp;
                if(temp==1) c[i][j]=true;
            }
        }
        int t=0;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                bool flag=dfs(i,j,c);
                if(flag) t++;
            }
        }
        cout<<t<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <stack>
#include <queue>
#include <vector>
using namespace std;

int MAP(vector<vector<int> > &m, int i, int j){
	int w = m[0].size();
	int h = m.size();

	if(!(0 <= i && i < h && 0 <= j && j < w)){
		return 0;
	}

	if(m[i][j] == 0){
		return 0;
	}
	else if(m[i][j] = 1){
		m[i][j] = 0;
		MAP(m, i - 1, j - 1);
		MAP(m, i, j - 1);
		MAP(m, i + 1, j - 1);
		MAP(m, i - 1, j);
		MAP(m, i + 1, j);
		MAP(m, i - 1, j + 1);
		MAP(m, i, j + 1);
		MAP(m, i + 1, j + 1);
	}
	return 1;
}

int main(){
	int w, h;

	while(true){
		int count = 0;
		cin >> w >> h;

		if(w == 0 && h == 0){
			break;
		}

		vector<vector<int> > map(h, vector<int>(w, 0));

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

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				count += MAP(map, i , j);
			}
		}

		cout << count << endl;
	}
}

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

int land[50][50], flag[50][50];
int w, h;
int xroot[] = {0,0,1,1,1,-1,-1,-1};
int yroot[] = {1,-1,0,-1,1,0,1,-1};
int cnt = 1;


void dfs(int x, int y){


  if(land[y][x] != 1) return;
  land[y][x] = cnt;

  for(int i = 0; i < 8; i++){
    int nx = x + xroot[i], ny = y + yroot[i];
    if(ny >= h || nx >= w || ny < 0 || nx < 0) continue;
    // cout << nx << " " << ny << endl;
    dfs(nx,ny);
  }
}

int main(){

  while(cin >> w >> h && (w || h)){
    cnt = 1;
    memset(flag,0,sizeof(flag));

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



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

  }



  return 0;
}

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

#define rep(i,N) for(int i=0;i<N;i++)
#define rep2(i,N) for(int i=1;i<=N;i++)
using namespace std;
long long  INF=1e18;
long long mod=1e9+7;
int maze[55][55]={};
bool reached[55][55]={};
int dx[8]={0,1,0,-1,1,-1,1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
int w,h;
int islands;

void search(int nowy,int nowx,bool start){

    if(reached[nowy][nowx]||maze[nowy][nowx]==0)return;

    if(start)islands++;

    reached[nowy][nowx]=true;
    //cout<<nowy<<" "<<nowx<<" "<<endl;
    rep(i,8)
    {
        int nx=nowx+dx[i];
        int ny=nowy+dy[i];

        if(nx<1 || nx>w || ny<1 || ny>h||reached[ny][nx]|| !maze[ny][nx])continue;

        search(ny,nx,false);
    }
    //cout<<islands<<" end"<<endl;
    return;
}
int main(){
    

    while(1){
    
    cin>>w>>h;
    if(w==0 && h==0)break;
    //cout<<w<<" "<<h<<endl;
    islands=0;

    rep2(i,50)rep2(k,50){
        reached[i][k]=false;
        maze[i][k]=0;
    }
    rep2(i,h)rep2(k,w){
        int t;
        cin>>t;
        maze[i][k]=t;
    }

    rep2(i,h)rep2(k,w)search(i,k,true);

    cout<<islands<<endl;
    
    }
    return 0;

}
/*


*/