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 <math.h>
#include <algorithm>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
				
				
void del_island(int i,int j,vector<vector<int>> &v){	
	v[i][j] = 0;
	if(v[i-1][j] == 1){
		del_island(i-1,j,v);
	}
	if(v[i+1][j] == 1){
		del_island(i+1,j,v);
	}
	if(v[i][j-1] == 1){
		del_island(i,j-1,v);
	}
	if(v[i][j+1] == 1){
		del_island(i,j+1,v);
	}
	if(v[i-1][j-1] == 1){
		del_island(i-1,j-1,v);
	}
	if(v[i-1][j+1] == 1){
		del_island(i-1,j+1,v);
	}
	if(v[i+1][j-1] == 1){
		del_island(i+1,j-1,v);
	}
	if(v[i+1][j+1] == 1){
		del_island(i+1,j+1,v);
	}

}


int count_island(int w, int h,vector<vector<int>> v){
	int count = 0;
	for(int i = 1 ; i <= h ;i++){
		for(int j = 1 ;j <= w ;j++){
			//cout << v[i][j] ;
			if(v[i][j] == 1){
				count++;
				//cout << "hello" <<endl;
				del_island(i,j,v);
			}
		}
	}
	return count;
}
	
int main(){ 
	int w,h;
	vector<vector<int>> v(60,vector<int>(60,0));
	int count;

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

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

		count = count_island(w,h,v);


		cout << count << endl;
	}
}

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

int N,M;
char filed[100][101];

void dfs(int x,int y){
  filed[x][y]='.';
  
  for(int dx=-1;dx<=1;dx++){
    for(int dy=-1;dy<=1;dy++){
      int nx=x+dx,ny=y+dy;
      if(0<=nx&&nx<N&&0<=ny&&ny<M&&filed[nx][ny]=='W')dfs(nx,ny);
    }
  }
}

void solve(){
  int res=0;
  for(int i=0;i<N;i++){
    for(int j=0;j<M;j++){
      if(filed[i][j]=='W'){
	dfs(i,j);
	res++;
      }
    }
  }
  cout<<res<<endl;
}
int main(){
  cin>>N>>M;
  for(int i=0;i<N;i++){
    for(int j=0;j<M;j++){
      cin>>filed[i][j];
    }
  }
  
  solve();
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
bool find1(vector< vector<int> >& C,vector< vector<int> >& check,int* findh,int* findw)
{
  for(int i = 0;i < C.size();++i)
    for(int j = 0;j < C[0].size();++j)
      if(C[i][j] == 1 && check[i][j] == 0){
	*findh = i;
	*findw = j;
	return true;
      }
  return false;
}
bool find2(vector< vector<int> >& C,vector< vector<int> >& check,int* findh,int* findw)
{
  if(C[*findh][*findw - 1] == 1 && check[*findh][*findw - 1] == 0){
    *findw -= 1;
    return true;
  }
  else if(C[*findh][*findw + 1] == 1 && check[*findh][*findw + 1] == 0){
    *findw += 1;
    return true;
  }
  else if(C[*findh + 1][*findw - 1] == 1 && check[*findh + 1][*findw - 1] == 0){
    *findh += 1;
    *findw -= 1;
    return true;
  }
  else if(C[*findh + 1][*findw] == 1 && check[*findh + 1][*findw] == 0){
    *findh += 1;
    return true;
  }
  else if(C[*findh + 1][*findw + 1] == 1 && check[*findh + 1][*findw + 1] == 0){
    *findh += 1;
    *findw += 1;
    return true;
  }
  else return false;
}
int main()
{
  int howmany = 0,w,h,findh,findw;
  stack< pair<int,int> > finish;
  queue<int> answer;
  while(cin >> h >> w && !(w == 0 && h == 0)){
    vector< vector<int> > C(h + 2,vector<int>(w + 2,0)),check(h + 2,vector<int>(w + 2,0)); 
    for(int i = 1;i <= h;++i)
      for(int j = 1;j <= w;++j)
	cin >> C[i][j];
    while(find1(C,check,&findh,&findw)){
      check[findh][findw] = 1;
      finish.push(make_pair(findh,findw));
      while(!finish.empty()){
	findh = finish.top().first;
	findw = finish.top().second;
	while(find2(C,check,&findh,&findw)){
	  finish.push(make_pair(findh,findw));
	  check[findh][findw] = 1;
	}
	finish.pop();
      }
      ++howmany;
    }
    answer.push(howmany);
    howmany = 0;
  }
  for(;!answer.empty();answer.pop())
    cout << answer.front() << endl;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

int H, W;
vector<vector<int>> field;

void dfs(int h, int w) {
	field[h][w] = 0;
	/* 8方向に探索 */
	for (int dh = -1; dh <= 1; dh++) {
		for (int dw = -1; dw <= 1; dw++) {
			int nh = h + dh;
			int nw = w + dw;

			if (nh < 0 || H <= nh || nw < 0 || W <= nw) continue;
			if (field[nh][nw] == 0) continue;
			
			dfs(nh, nw);
		}
	}
}

int main() {
	while (cin >> W >> H) {
		if (H == 0 || W == 0) break;
		field.assign(H, vector<int>(W, 0)); // 0で初期化
		for (int h = 0; h < H; h++) {
			for (int w = 0; w < W; w++) {
				cin >> field[h][w];
			}
		}
		 
		/* すべての地点に対して探索を行う */
		int count = 0;
		rep(h, H) {
			rep(w, W) {
				if (field[h][w] == 0) continue;
				dfs(h, w);
				count++;
			}
		}
		cout << count << endl;
	}
}

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


int foo(int c[2][60][60], int y, int x){
	if(c[1][y][x] == 1) return 0;
	c[1][y][x] = 1;
	if(c[0][y][x] == 0) return 0;
	
	for(int i = -1; i <= 1; i++)
		for(int j = -1; j <= 1; j++)
			foo(c, y+i, x+j);
	
	return 1;
}

int main(){
	int h, w;
	
	while(1){
		int  c[2][60][60] = {}, count = 0;
		
		cin>>w>>h;
		
		if(w == 0 && h == 0) break;
		
		for(int i = 1; i <= h; i++)
			for(int j = 1; j <= w; j++)
				cin>>c[0][i][j];
		
		for(int i = 1; i <= h; i++)
			for(int j = 1; j <= w; j++)
				if(foo(c, i, j) == 1) count++;
				
		cout<<count<<endl;
	}
	return 0;
}