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<algorithm>
#include<cstring>
using namespace std;
int H,W,k[100][100],n[100][100],color=1,i,j,l,m,o,r;
int main(){while(true){memset(n,0,sizeof(n));r=0;cin>>H>>W;if(H==0){break;}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<20;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>
using namespace std;
void kazoeruyo(int p,int P,int box[50][50]){
  box[p][P]=0;
    if(box[p][P+1]==1) {
      box[p][P+1]=0;
      P=P+1; 
      kazoeruyo(p,P,box);
    }
    if(box[p][P-1]==1){
      box[p][P-1]=0;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P]==1){ 
      box[p+1][P]=0;
      p=p+1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P]==1){
      box[p-1][P]=0;
      p=p-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P-1]==1){
      box[p-1][P-1]=0;
      p=p-1;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P+1]==1){
      box[p-1][P+1]=0;
      p=p-1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P+1]==1){
      box[p+1][P+1]=0;
      p=p+1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P-1]==1){
      box[p+1][P-1]=0;
      p=p+1;
      P=P-1;
      kazoeruyo(p,P,box);
    }	
}
int main(){
  int w,h,i,I,p,P,o,t,T,y,Y;
  int box[10][10];
  while(1){
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	cin>>box[i][I];
      }
    }
    o=0;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) {
	  p=i;
	  P=I;
	  kazoeruyo(p,P,box);
	  box[p][P]=1;
	}
      }
    }
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) o++;
      }
    }
    cout<<o<<endl;
    o=0;
  }
  return 0;
}

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

int w,h;
int maps[50][50];

void dfs(int y, int x)
{
  int dx[]={1,-1,0,0};
  int dy[]={0,0,1,-1};

  maps[y][x]=0;

  for(int i=0;i < 4;i++){
    for(int j=0;j < 4;j++){
      int xx = x + dx[j];
      int yy = y + dy[i];
      if(maps[yy][xx] == 1) dfs(yy,xx);
    }
  }
}
int main(){
  while(cin >> w >> h && w)
    {
      for(int i=0;i < h;i++){
	for(int j=0;j < w;j++){
	  cin >> maps[i][j];
	}
      }
      int island=0;

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

C++: #include<iostream>
#include<vector>
#include<queue>
#include<utility>

using namespace std;

pair<int,int> finish(vector<vector<int>> a,int h,int w){
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(a[i][j]==1){return make_pair(i,j);}
    }
  }
  return make_pair(h,w);
}

//沈めてく
void idlands(int H,int W){
  vector<vector<int>> d(H,vector<int>(W,0));
  queue< pair<int,int> > x;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin >> d[i][j];
    }
  }
  int l=1;
  int c=0;
  while(finish(d,H,W)!=make_pair(H,W)){
    x.push(finish(d,H,W));
    c+=1;l=x.size();d[x.front().first][x.front().second]=0;
    while(l!=0){
      for(int i=0;i<l;i++){
        int h,w;h=x.front().first;w=x.front().second;
        if(0<h and d[h-1][w]==1){
          x.push(make_pair(h-1,w));d[h-1][w]=0;}
        if(h<H-1 and d[h+1][w]==1){
          x.push(make_pair(h+1,w));d[h+1][w]=0;}
        if(0<w and d[h][w-1]==1){
          x.push(make_pair(h,w-1));d[h][w-1]=0;}
        if(w<W-1 and d[h][w+1]==1){
          x.push(make_pair(h,w+1));d[h][w+1]=0;}
        if(0<h and w<W-1 and d[h-1][w+1]==1){
          x.push(make_pair(h-1,w+1));d[h-1][w+1]=0;}
        if(h<H-1 and 0<w and d[h+1][w-1]==1){
          x.push(make_pair(h+1,w-1));d[h+1][w-1]=0;}
        if(0<h and 0<w and d[h-1][w-1]==1){
          x.push(make_pair(h-1,w-1));d[h-1][w-1]=0;}
        if(h<H-1 and w<W-1 and d[h+1][w+1]==1){
          x.push(make_pair(h+1,w+1));d[h+1][w+1]=0;}
        x.pop();
      }
      l=x.size();
    }
  }
  cout << c << endl;
}

int main(){
  int W,H;
  while(cin >> W >> H and W!=0){
    idlands(H,W);
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;

vector<vector<int>> c(51,vector<int>(51,0));
int w,h;

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

int main(){
	while (1) {
		cin>>w>>h;
		if (w == 0 && h == 0){
			break;
		}
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				cin >> c[i][j];
			}	
		}
		int ans = 0;

	// for(int i = 0; i < h; i++) {
	// 	for(int j = 0; j < w; j++) {
	// 		cout << c[i][j];
	// 	}
	// 	cout << endl;
	// }
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if (c[i][j] == 1){
					search(i,j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}