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 <vector>
using namespace std;
int ans;
int w, h;
//?§???????
const int mx[] = {-1, 0, 1, -1, 1, -1, 0, 1 };
const int my[] = { 1, 1, 1, 0, 0, -1, -1, -1};
vector<int> vx;
vector<int> vy;

vector<vector<int> > area;
int islands = 0;

//????±????????????¨????????¢?´¢
void dfs(int i,int j){
	for (int n = 0; n < vx.size(); n++){
		int x = j + vx[n];
		int y = i + vy[n];
		if (x >= 0 && x < w &&
			y >= 0 && y < h &&
			area[y][x] == 1){
			area[y][x] = 2;
			dfs(y,x);
		}
	}
}

int main(){
	for (int i = 0; i < 8; i++){
		vx.push_back(mx[i]);
		vy.push_back(my[i]);
	}
	while (1){
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		//?????????
		islands = 0;
		area.clear();
		area = vector<vector<int> >(h, vector<int>(w));
		//??\???
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				cin >> area[i][j];
			}
		}
		//??¨??¢?´¢
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (area[i][j] == 1){
					islands++;
					area[i][j] = 2;
					dfs(i, j);
				}
			}
		}
		cout << islands << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF (1<<29)

int c[52][52];
int w,h;
void del(int y,int x){
	c[y][x]=0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(c[y+i][x+j])del(y+i,x+j);
		}
	}
}
int main(){
	while(cin>>w>>h,w||h){
		memset(c,0,sizeof(c));
		for(int i=1;i<=h;i++)
			for(int j=1;j<=w;j++)cin>>c[i][j];
		int ans=0;
		for(int i=1;i<=h;i++)
			for(int j=1;j<=w;j++)
				if(c[i][j]){
					ans++;
					del(i,j);
				}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;

int w, h;
int data[50][50];	//data[row][col]

bool isRange(int r, int c) {
	return (r >= 0 && r < h && c >= 0 && c < w);
}

int main() {
	while (cin >> w >> h) {
		if (!w) break;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				cin >> data[i][j];
		
		int ans = 0;
		bool used[50][50] = {false};
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (!data[i][j] || used[i][j]) continue;
				ans++;
				
				//nurima-su (i, j) kara ikeru masu wo by bfs.
				queue<P> que;	//row, col
				que.push(P(i, j));
				while (!que.empty()) {
					int r = que.front().first;
					int c = que.front().second;
					que.pop();
					
					if (used[r][c]) continue;	//itidosika sirabenai (edakari)
					used[r][c] = true;	//visit
					
					for (int dr = -1; dr <= 1; dr++) {
						for (int dc = -1; dc <= 1; dc++) {
							if (dr == 0 && dc == 0) continue;
							if (!isRange(r + dr, c + dc) || !data[r + dr][c + dc]) continue;	//ikeruka hantei
							que.push(P(r + dr, c + dc));
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

#define rep(i,n) for(int i=0; i<n; i++)

int nx[8]={1,1,1,0,0,-1,-1,-1}, ny[8]={1,0,-1,1,-1,1,0,-1};
int w, h;
int mp[60][60];

void solve(int x, int y){
  if(x<0 || h<=x || y<0 || w<=y || mp[x][y]!=1)return;
  mp[x][y]=0;
  rep(i,8)solve(x+nx[i], y+ny[i]);
}

int main(){
  while(true){
    cin>>w>>h;
    if(w==0&&h==0)break;
    memset(mp, 0, sizeof(mp));
    rep(i,h)rep(j,w)cin>>mp[i][j];
    int cnt=0;
    rep(i,h)rep(j,w)if(mp[i][j]==1){solve(i, j); cnt++;}
    cout<<cnt<<endl;
  }
  return 0;
}

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

void add_label(multimap<int,int> &tbl, int a, int b){
	if( a == b ) return;
	if( a > b ) tbl.insert( make_pair(a, b) );
	else tbl.insert( make_pair(b, a) );
}

int main(void){
	int w, h;
	while( cin>>w>>h, w||h ) {
		vector<vector<int> > field( h );
		vector<vector<int> > labeled( h );
		for( int i=0; i<h; ++i ) {
			field[i] = vector<int>( w );
			labeled[i] = vector<int>( w );
			for( int j=0; j<w; ++j ) {
				cin>>field[i][j];
				labeled[i][j] = 0;
			}
		}
		int label = 0;
		multimap<int, int> tbl;
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				if( field[i][j] ){
					bool f = false;
					if( i ) {
						if( j && labeled[i-1][j-1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j-1];
							f = true;
						}
						if( labeled[i-1][j] ) {
							if(!f) labeled[i][j] = labeled[i-1][j];
							else add_label( tbl, labeled[i][j], labeled[i-1][j] );
							f = true;
						}
						if( j + 1 < w && labeled[i-1][j+1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j+1];
							else add_label( tbl, labeled[i][j], labeled[i-1][j+1] );
							f = true;
						}
						if( j && labeled[i][j-1] ) {
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j], labeled[i][j-1] );
							f = true;
						}
					} else {
						if( j && labeled[i][j-1] ){
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j], labeled[i][j-1] );
							f = true;
						}
					}
					if(!f) labeled[i][j] = ++label;
				}
			}
		}
		for( auto x : tbl ) {
			int a = x.first;
			int b = x.second;
			while( tbl.find(b) != tbl.end() ) {
				b = tbl.find(b)->second;
			}
			for( int i=0; i<h; ++i ) {
				for( int j=0; j<w; ++j ) {
					if( labeled[i][j] == a ) labeled[i][j] = b;
				}
			}
		}
		
		/*
		for( int i=0; i<h; ++i ) {
			cerr<<"!";
			for( int j=0; j<w; ++j ) {
				cerr<<labeled[i][j];
			}
			cerr<<endl;
		}*/
		
		set<int> nums;
		nums.insert(0);
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				nums.insert( labeled[i][j] );
			}
		}
		cout<<nums.size()-1<<endl;
	}
	return 0;
}