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>
#include<queue>

using namespace std;

int ans = 0;
bool dfs(vector< vector<int> > &island,int x,int y){
	int dx[8] = {-1,-1,-1, 0, 0, 1, 1, 1};
	int dy[8] = {-1, 0, 1,-1, 1,-1, 0, 1};
	if (island[x][y] == 0) return false;
	island[x][y] = 0;
	for (int i = 0; i< 8; i++){
		if(0 <= x + dx[i] and x + dx[i] < island.size() and 0 <= y + dy[i] and y + dy[i] < island[0].size()){
			dfs(island,x + dx[i],y + dy[i]);
		}
	}
	return true;
}

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

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

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int w,h;
int nx,ny;
vector<vector<int>> G;
void dfs(int a,int b){
     if(0>a|| a>=h || 0>b|| b>=w) return ;
     if( G.at(a).at(b)==0) return ; 
     G.at(a).at(b)=0;
    for(int k=-1;k<=1;k++){
        for(int s=-1;s<=1;s++){
            nx=a+k;
            ny=b+s;
            dfs(nx,ny);
        }
    }
}
 
 
 
int main(){
    cin>>w>>h;
    G.resize(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                count++;
                dfs(i,j);
            }
        }
    }
cout<<count<<endl;
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };
const int dx2[] = { -1,-1, 0,1,1, 1, 0,-1 }; const int dy2[] = { 0,-1,-1,-1,0, 1, 1, 1 };
struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n)  { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x)         { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};
int h, w;
int cell[50][50];
int main() {
	while (cin>>w>>h,h) {

		REP(i, h)
			REP(j, w)
			cin >> cell[i][j];
		int ans = 0;
		REP(i, h)
			REP(j, w)
			if (cell[i][j] == 1) {
				ans++;
				queue<pii> Q;
				Q.push(pii(i, j));
				cell[i][j] = 0;
				while (!Q.empty()){
					pii q = Q.front();Q.pop();
					int y = q.first;
					int x = q.second;
					REP(k,8) {
						if (0 <= y + dy2[k] && y + dy2[k] < h && 0 <= x + dx2[k] && x + dx2[k] < w &&cell[y + dy2[k]][x + dx2[k]]) {
							cell[y + dy2[k]][x + dx2[k]] = 0;
							Q.push(pii( y + dy2[k], x + dx2[k]));
						}
					}
				}
			}
		cout << ans << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
#define INF 200000000000
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()

bool compare(Pi a, Pi b) {
    if(a.second != b.second){
        return a.second > b.second;
        
    }else{
        return a.first < b.first;
    }
}
bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0,1,1,-1,-1};
const vector<ll> dy{0,1,0,-1,1,-1,1,-1};


ll cnt=0;
vector<vector<bool>> seen(110,vector<bool>(110));
vector<vector<bool>> seen2(110,vector<bool>(110));
vector<vector<bool>> G;
ll W,H;
void dfs(ll h, ll w){
    seen[h][w] = 1;
    cnt++;
    rep(dir,8){
        ll nh = h + dy[dir];
        ll nw = w + dx[dir];

        if(!In_map(nh,nw,H,W)) continue;
        if(G[nh][nw] == 0) continue;
        if(seen[nh][nw]) continue;
        dfs(nh,nw);
    }



}


int main() {
    while(1){
    seen = seen2;
    G.clear();
    cin>>W>>H;
    if(W==0 && H==0) break;
    ll num=0;
    rep(i,H){
        G.emplace_back();
        rep(j,W){
            bool c;
            cin>>c;
            G[i].pb(c);
        }
    }
    
    ll cnt=0;
    rep(i,H){
        rep(j,W){
            if(G[i][j] == 0) continue;
            if(seen[i][j]) continue;
            dfs(i,j);
            cnt++;
            
        }
    }

    cout<<cnt<<endl;
    }
    return 0;
    

}