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;

void ume(int y, int x, vector<vector<int>> &field) {
    int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
    int dy[] = {0, 1, -1, 1, -1, 0, 1, -1};

    for (int d = 0; d < 8; d++) {
        if (field[y + dy[d]][x + dx[d]] == 1) {
            field[y + dy[d]][x + dx[d]] = 0;
            ume(y + dy[d], x + dx[d], field);
        }
    }
}

int solve(int w, int h) {
    vector<vector<int>> field(h + 2, vector<int>(w + 2, 0));
    for (int i = 0; i < h + 2; i++) {
        for (int j = 0; j < w + 2; j++) {
            if (i == 0 || i == h + 1 || j == 0 || j == w + 1)
                field[i][j] = -1;
            else
                cin >> field[i][j];
        }
    }
    // for (int i = 0; i < h + 2; i++) {
    //     for (int j = 0; j < w + 2; j++) {
    //         // if (i == 0 || i == h + 1 || j == 0 || j == w + 1)
    //         //     field[i][j] = -1;
    //         // else
    //         //     cin >> field[i][j];
    //         cout << field[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    // queue<pair<int,int>> que;
    int island = 0;
    for (int i = 0; i < h + 2; i++) {
        for (int j = 0; j < w + 2; j++) {
            if (field[i][j] == 1) {
                field[i][j] = 0;
                ume(i, j, field);
                island++;
            }
        }
    }
    return island;
}

int main(void) {
    int w, h;
    vector<int> ans;
    while (1) {
        cin >> w >> h;
        if (!w && !h) break;
        ans.push_back(solve(w, h));
    }
    for (int a : ans) cout << a << endl;
}

C++: #include<bits/stdc++.h>
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//clock_t start=clock();clock_t end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int> pii;
typedef pair<pii,int> ppii;
typedef pair<int,pii> pipi;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppll;
typedef pair<ll,pll> plpl;
typedef tuple<ll,ll,ll> tl;
typedef pair<double,double> pdd;
ll mod=1000000007;
ll mod2=998244353;
ll mod3=1000003;
ll mod4=998244853;
ll mod5=1000000009;
ll inf=1LL<<60;
double pi=3.141592653589793238462643383279L;
double eps=1e-9;
#define rep(i,m,n) for(ll i=m;i<n;i++)
#define rrep(i,n,m) for(ll i=n;i>=m;i--)
#define srep(itr,st) for(auto itr=st.begin();itr!=st.end();itr++)
#define mrep(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++)
#define Max(a,b) a=max(a,b)
#define Min(a,b) a=min(a,b)
int dh[4]={1,-1,0,0};
int dw[4]={0,0,1,-1};
int ddh[8]={-1,-1,-1,0,0,1,1,1};
int ddw[8]={-1,0,1,-1,1,-1,0,1};
ll gcd(ll a,ll b){
    if(a<b)swap(a,b);
    if(b==0)return a;
    if(a%b==0)return b;
    return gcd(b,a%b);
}
ll Pow(ll n,ll k){
    ll ret=1;
    ll now=n;
    while(k>0){
        if(k&1)ret*=now;
        now*=now;
        k/=2;
    }
    return ret;
}
ll beki(ll n,ll k,ll md){
  ll ret=1;
  ll now=n;
  while(k>0){
    if(k%2==1){
      ret*=now;
      ret%=md;
    }
    now*=now;
    now%=md;
    k/=2;
  }
  return ret;
}
ll gyaku(ll n,ll md){
  return beki(n,md-2,md);
}
ll popcount(ll n){
    ll ret=0;
    ll u=n;
    while(u>0){
        ret+=u%2;
        u/=2;
        
    }
    return ret;
}

int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    for(;;){
        int h,w;cin>>w>>h;
        if(h==0&&w==0)return 0;
        int a[h][w];
        rep(i,0,h)rep(j,0,w)cin>>a[i][j];
        bool used[h][w];
        memset(used,false,sizeof used);
        int ans=0;
        rep(i,0,h){
            rep(j,0,w){
                //cout<<i<<" "<<j<<" "<<used[i][j]<<endl;
                if(used[i][j]==true)continue;
                if(a[i][j]==0)continue;
                queue<pii> q;
                ans++;
                q.push({i,j});
                used[i][j]=true;
                while(q.size()>0){
                    pii p=q.front();q.pop();
                    int nh=p.first,nw=p.second;
                    
                    rep(k,0,8){
                        int nnh=nh+ddh[k],nnw=nw+ddw[k];
                        //if(i==0&&j==0)cout<<nnh<<" "<<nnw<<endl;
                        if(nnh<0||nnh>=h||nnw<0||nnw>=w)continue;
                        if(a[nnh][nnw]==0)continue;
                        if(used[nnh][nnw])continue;
                        
                        q.push({nnh,nnw});
                        used[nnh][nnw]=true;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
}