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>

#define ll long long

using namespace std;

int w,h;

int f[100][100]={};
int cf[100][100] = {};
int LUT[1000] ={};



int main(){

    while(1){
        scanf("%d %d",&w,&h);
        if(w == 0 && h==0)break;

        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                scanf("%d",&f[i][j]);
            }
        }
        int ans = 0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(f[i][j] == 1 && cf[i][j]==0){
                    ans++;
                    LUT[ans]=ans;
                    cf[i][j] = ans;
                }
                
                for(int k=-1;k<2;k++){
                    if(i+k<0 || h <= i+k)continue;
                    for(int t=-1;t<2;t++){
                        if(j+t<0 || w <= j+t)continue;
                        if(f[i][j] == 1 && f[i+k][j+t]==1 ){
                            if(cf[i+k][j+t]==0){
                                cf[i+k][j+t]= cf[i][j];
                            }else if(cf[i+k][j+t] > cf[i][j]){
                                LUT[cf[i+k][j+t]] = cf[i][j];
                            }
                        }
                    }
                }
            }
        }
        for(int i=0;i<1000;i++){
            if(LUT[i] != 0 && LUT[i] != i){
                ans--;
            }
        }
        cout << ans << endl;
    }

}

C++: #include<stdio.h>
int main(){
  int w,h,a[51][51],i,j,i2,j2,c,k;
  while(1){
    //syokika
    for(i=0;i<51;i++){
      for(j=0;j<51;j++){
	a[i][j]=0;
      }
    }
    c=2;
  
    scanf("%d%d",&w,&h);
    if(w==0&&h==0)break;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	scanf("%d",&a[i][j]);
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(a[i][j]==1){
	  a[i][j]=c;
	  k=1;
	  while(k!=3){
	    k=1;
	    //go
	    for(i2=0;i2<h;i2++){
	      for(j2=0;j2<w;j2++){
		//someru
		if(a[i2][j2]==1){
		  if(j2!=0){
		    if(a[i2][j2-1]==c){
		      a[i2][j2]=c; //left
		      k=2;
		    }
		  }
		  if(i2!=0){
		    if(a[i2-1][j2]==c){
		      a[i2][j2]=c; //up
		      k=2;
		    }		    

		    if(j2!=0){
		      if(a[i2-1][j2-1]==c){
			a[i2][j2]=c; //left up
			k=2;
		      }
		    }
		    if(a[i2-1][j2+1]==c){
		      a[i2][j2]=c; //right up
		      k=2;
		    }
		    
		  }
		}
		//some end
	     
	      }
	    }

	    //back
	    for(i2=h-1;i2>=0;i2--){
	      for(j2=w-1;j2>=0;j2--){
		//someru
		if(a[i2][j2]==1){
		  if(a[i2][j2+1]==c){
		    a[i2][j2]=c; //right
		    k=2;
		  }    
		  if(a[i2+1][j2]==c){
		    a[i2][j2]=c; //down
		    k=2;
		  }		
		  if(a[i2+1][j2+1]==c){
		    a[i2][j2]=c; //right do
		    k=2;
		  }
		  if(j2!=0){
		    if(a[i2+1][j2-1]==c){
		      a[i2][j2]=c; //left do
		      k=2;
		    }
		  }
		}
		
		
		//some end
		
	      }
	    }
	    if(k==1)k=3;
	    //sima end
	  }
	  c++;
	}
      }
    }
    printf("%d\n",c-2);
  }
  return 0;
}

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

int W, H;

int tansaku(vector<vector<int>> &data, int h, int w) {
    int ret = 0;
    if(data[h][w] == 1) {
        ret = 1;
        data[h][w] =0;
        if(h-1 >= 0          ) ret |= tansaku(data, h-1, w);
        if(h+1 <  H          ) ret |= tansaku(data, h+1, w);
        if(           w-1 >=0) ret |= tansaku(data, h  , w-1);
        if(           w+1 < W) ret |= tansaku(data, h  , w+1);
        if(h-1 >=0 && w-1 >=0) ret |= tansaku(data, h-1, w-1);
        if(h-1 >=0 && w+1 < W) ret |= tansaku(data, h-1, w+1);
        if(h+1 < H && w-1 >=0) ret |= tansaku(data, h+1, w-1);
        if(h+1 < H && w+1 < W) ret |= tansaku(data, h+1, w+1);
    }
    return ret;
}

int func() {
    cin >> W >> H;
    if(W==0 && H==0) return 0;
    //int data[H][W];
    vector<vector<int>> data;
    data.resize(H);

    for(int h=0; h<H; h++) {
        for(int w=0; w<W; w++) {
            int tmp;
            cin >> tmp;
            //data[h][w] = tmp;
            data[h].push_back(tmp);
        }
    }


    int counter = 0;
    for(int h=0; h<H; h++) {
        for(int w=0; w<W; w++) {
            counter += tansaku(data, h, w);
        }
    }
    cout << counter << endl;
    
    return 1;
}

int main() {
    while(func()) {}
    return 0;
}

C++:  #include<bits/stdc++.h>
 using namespace std;
#define INF 1000000000
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
typedef long long LL;
int w,h;
int c[101][101];
int dx[8]={0,0,1,1,1,-1,-1,-1};
int dy[8]={1,-1,0,1,-1,0,1,-1};
void dfs(int x,int y){
     c[x][y]=0;
     REP(i,8){
         if(c[x+dx[i]][y+dy[i]]==1){
            dfs(x+dx[i],y+dy[i]);
         }
     }
}
int main(){
    while(cin>>w>>h,w,h){
    REP(i,h+1){
        REP(j,w+1){
            c[i][j]=0;
        }
    }
    REP(i,h){
        REP(j,w){
            cin>>c[i+1][j+1];
        }
    }
    int ans=0;
    REP(i,h+1){
        REP(j,w+1){
            if(c[i][j]==1){
               dfs(i,j);
               ans++;
            }
        }
    }
    cout<<ans<<endl;
    }
    return 0;
}