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;
#define rp(i,j) for(long long i=0;i<j;i++)
#define rep(i,j,k) for(long long i=j;i<k;i++)
#define mod 1000000007
#define NIL -1
#define MAX 50
const int INFTY=(1<<21);
typedef long long ll;
int H,W;
int c[MAX][MAX];
int checked[MAX][MAX]={0};
void dfs(int x,int y){
    if(x<0||x>=H||y<0||y>=W||c[x][y]==0)return;
    if(checked[x][y]==1)return;
    checked[x][y]=1;
    rep(i,-1,2){
        rep(j,-1,2){
            dfs(x+i,y+j);
        }
    }
}
int main(){
    while(1){
        int ans=0;
        cin>>W>>H;
        if(H==0&&W==0)break;
        else {
            rp(i,H){
                rp(j,W){
                    cin>>c[i][j];
                    checked[i][j]=0;
                }
            }
            rp(i,H){
                rp(j,W){
                    if(c[i][j]==1&&checked[i][j]==0){
                        dfs(i,j);
                        ans++;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int H,W,k[100][100],n[100][100],color=1,i,j,l,m,o,r;
int main(){cin>>H>>W;for(i=0;i<H;i++){for(j=0;j<W;j++){cin>>k[i][j];}}
for(i=0;i<H;i++){for(j=0;j<W;j++){if(r==1){color++;}r=0;if(k[i][j]==1 && n[i][j]==0){n[i][j]=color;for(l=0;l<100;l++){
	for(m=0;m<H;m++){for(o=0;o<W;o++){if(n[m][o]==color){
		for(int p=-1;p<=1;p++){for(int q=-1;q<=1;q++){if(m-p>=0 && m-p<H){if(o-q>=0 && o-q<W){n[m][o]=color;}}}}
	}}}
}}}}
cout<<color<<endl;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
using namespace std;
#define N (1000000000+7)
typedef pair<int,int> P;
typedef long long ll;

int h,w;
int board[60][60];
bool check(int i,int j){
    return (i>=1)&&(i<=h)&&(j>=1)&&(j<=w)&&(board[i][j]==1);
}

void dfs(int i,int j){
    if(check(i,j)){
        board[i][j]=-1;
        for(int x=i-1;x<=i+1;++x){
            for(int y=j-1;y<=j+1;++y){
                dfs(x,y);
            }
        }
    }
    else return;
}

int main(void){
    while(true){
        cin>>w>>h;
        int sx=0,sy=0;
        int ans=0;
        if((h==0)&&(w==0))break;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                cin>>board[i][j];
            }
        }
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                if(board[i][j]==1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

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

vector<int> xw = {-1,  0,  1, -1,  1, -1,  0,  1 };
vector<int> yh = {-1, -1, -1,  0,  0,  1,  1,  1 };
void dfs( vector<vector<int>> &vec, int X, int Y, int &w, int &h, vector<vector<bool>> &ck ){
  if ( ck.at(Y).at(X) ) return;
  
  ck.at(Y).at(X) = true;
  for (int i=0; i<8; i++) {
    int x = X + xw.at(i);
    int y = Y + yh.at(i);
    if( x < 0 || w <= x ) continue;
    if( y < 0 || h <= y ) continue;
    if ( vec.at(y).at(x) ) dfs( vec, x, y, w, h, ck);
  }
}

int main() {
  
  while (true) {
  int W,H;
  cin >> W >> H;
  if( W == 0 || H == 0) break;
    
  vector<vector<int>> P(H, vector<int>(W));
  for (int i=0; i<H; i++) {
    for (int j=0; j<W; j++) {
      cin >> P.at(i).at(j);  
    }
  }
  
  vector<vector<bool>> checked(H, vector<bool>(W, false));
  int res = 0;
  for (int h=0; h<H; h++) {
    for (int w=0; w<W; w++) {
      if( !checked.at(h).at(w) && P.at(h).at(w) ) {
         res++; dfs( P, w, h, W, H, checked);
      }
    }
  }
  
  cout << res << endl;
    
  }
  return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <queue>
#include <math.h>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
using namespace std;

vector<vector<int>> c;
int w, h;

void search(int i, int j){
    if(i-1 >= 0 && j-1 >= 0 && c[i-1][j-1] == 1){
        c[i-1][j-1] = 0;
        search(i-1, j-1);
    }
    if(j-1 >= 0 && c[i][j-1] == 1){
        c[i][j-1] = 0;
        search(i, j-1);
    }
    if(i+1 < h && j-1 >= 0 && c[i+1][j-1] == 1){
        c[i+1][j-1] = 0;
        search(i+1, j-1);
    }
    if(i-1 >= 0 && c[i-1][j] == 1){
        c[i-1][j] = 0;
        search(i-1, j);
    }
    if(i+1 < h && c[i+1][j] == 1){
        c[i+1][j] = 0;
        search(i+1, j);
    }
    if(i-1 >= 0 && j+1 < w && c[i-1][j+1] == 1){
        c[i-1][j+1] = 0;
        search(i-1, j+1);
    }
    if(j+1 < w && c[i][j+1] == 1){
        c[i][j+1] = 0;
        search(i, j+1);
    }
    if(i+1 < h && j+1 < w && c[i+1][j+1] == 1){
        c[i+1][j+1] = 0;
        search(i+1, j+1);
    }
    return;
}

int main()
{
    int ans;
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0){
            break;
        }
        // init
        ans = 0;
        c.resize(h);
        for(int i=0; i<h; ++i){
            c[i].resize(w);
            for(int j=0; j<w; ++j){
                cin >> c[i][j];
            }
        }
        // search
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                if(c[i][j] == 1){
                    ans++;
                    c[i][j] = 0;
                    search(i, j);
                }
            }
        }
        // output
        cout << ans << endl;
        // clear
        for(int i=0; i<h; ++i){
            c[i].clear();
        }
        c.clear();
    }
    return 0;
}