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 <cstdio>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define all(a) (a).begin(), (a).end()

int w, h;
int c[51][51];

void dfs( int i, int j )
{
  c[i][j] = 0;

  for( int dx = -1; dx <= 1; ++dx ) for( int dy = -1; dy <= 1; ++dy )
  {
    int ni = i+dy, nj = j+dx;

    if( ni >= 0 && ni < h && nj >= 0 && nj < w && c[ni][nj] == 1 )
      dfs( ni, nj );
  }

  return;
}

int main()
{
  while( scanf( "%d%d", &w, &h ), w|h )
  {
    rep( i, h ) rep( j, w )
      scanf( "%d", &c[i][j] );

    int cnt = 0;
    rep( i, h ) rep( j, w ) if( c[i][j] == 1 )
      dfs( i, j ), ++cnt;    

    printf( "%d\n", cnt );
  }
  
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;

int dx[]={1,1,0,-1,-1,-1,0,1}, dy[]={0,-1,-1,-1,0,1,1,1};
int cnt = 0;
int gg[60][60];
int w,h;
void solve(int x, int y){
    //cout<<x<<" "<<y<<"aaaaaa"<<endl;
    gg[x][y]=0;
    //if(x==2 && y == 48) cout<<"probrem"<<endl;
    rep(i,8){
        int nx = x + dx[i], ny = y + dy[i];
        /*if(x==2 && y==48){
            cout<<nx<<" "<<ny<<endl;
        }*/
        if(nx<h && nx>=0 && ny<w && ny>=0 && gg[nx][ny]==1){
            //cout<<nx<<" "<<ny<<endl;
            solve(nx,ny);
        }
    }
}

int main(){
    while(1){
        cin>>w>>h;
        rep(i,60) rep(j,60) gg[i][j]=0;
        cnt = 0;
        if(w==0 && h==0) break;
        rep(i,h) rep(j,w) cin>>gg[i][j];
        rep(i,h) rep(j,w){
            if(gg[i][j]==1) {
                //cout<<i<<" "<<j<<"start"<<endl;
                cnt++;
                solve(i,j);
                /*rep(i,h){
                    rep(j,w) cout<<gg[i][j]<<" ";
                    cout<<endl;
                }*/
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

int m[51][51];
int w, h;
int isl;
const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};

void dfs(int x, int y)
{
    m[x][y] = 0;
    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;
        else if (m[nx][ny] == 1)
            dfs(nx, ny);
    }
}

int main()
{
    while (cin >> w >> h, w || h) {
        isl = 0;

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

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

    return 0;
}

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

int checkMap(int w, int h, int map[52][52]){
	if(map[w][h]){
		map[w][h]=0;
		 checkMap(w+1, h+1, map);
		 checkMap(w+1, h, map);
		 checkMap(w+1, h-1, map);
		 checkMap(w, h-1, map);
		 checkMap(w-1, h-1, map);
		 checkMap(w-1, h, map);
		 checkMap(w-1, h+1, map);
		 checkMap(w, h+1, map);
		 return 1;
	}else return 0;
		 
}


int main(){
	int w,h;

	while(cin >> w >> h, w){
	int map[52][52] = {{0}};
		int ans=0;
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
				cin >> map[i][j];
			}
		}
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
			ans += checkMap(i, j, map);
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
using namespace std;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;
bool flag = false;

void solve(int i,int j){
    //cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    cin >> w >> h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
    if(w == 0 || h == 0){
    }
    else{
        cout << ans << endl;
    }
    return 0;
}