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>

#define rep(i,a,b) for(int i=a;i<b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define llrep(i,a,b) for(ll i=a;i<b;i++)
#define llrepd(i,a,b) for(ll i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
using namespace std;
void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; const ll MOD = 1000000007;
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; }

//---------------------------------------------------------------------------------------------------
int dx[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[8] = { 1, 0, -1, 0 ,1, -1, 1, -1 };
ll H, W;
vector<vector<int>> area;

void dfs(int i, int j) {
    area[i][j] = 0;
    rep(n, 0, 8) {
        int nx = i + dx[n], ny = j + dy[n];
        if (nx >= H || ny >= W || nx < 0 || ny < 0) continue;
        if (area[nx][ny] == 1) dfs(nx, ny);
    }
}


void resolve(ll W, ll H) {
    area = vector<vector<int>>(H, vector<int>(W));
    rep(i, 0, H) rep(j, 0, W) cin >> area[i][j];
    ll count = 0;
    rep(i, 0, H) {
        rep(j, 0, W) {
            if (area[i][j] == 1) {
                count++;
                dfs(i, j);
            }
        }
    }
    cout << count << endl;
}

void _main() {
    while(true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        resolve(W,H);
    }
}

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

int dfs(int h, int w, vector<vector<int>> &field, vector<vector<int>> &flag) {
    flag[h][w] = 1;
    int dh[] = {-1, -1, 0, 1, 1, 1, 0, -1};
    int dw[] = {0, 1, 1, 1, 0, -1, -1, -1};
    
    for (int i = 0; i < 8; i++) {
        int nh = h + dh[i];
        int nw = w + dw[i];
        if (field[nh][nw] == 1 && flag[nh][nw] == 0) {
            dfs(nh, nw, field, flag);
        }
    }
    return 0;
}


int solve(int H, int W) {
    vector<vector<int>> field(H+2, vector<int>(W+2));
    for (int h = 1; h <= H; h++) {
        for (int w = 1; w <= W; w++) {
            cin >> field[h][w];
        }
    }
    
    int ret = 0;
    vector<vector<int>> flag(H+2, vector<int>(W+2));
    for (int h = 1; h <= H; h++) {
        for (int w = 1; w <= W; w++) {
            if (field[h][w] == 1 && flag[h][w] == 0) {
                dfs(h, w, field, flag);
                ret++;
            }
        }
    }
    return ret;
}

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

C++: #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
typedef long long ll;
typedef vector<vector<ll>> vvl;
typedef vector<ll> vl;
typedef vector<vector<char>> vvc;
typedef vector<char> vc;
typedef pair<ll,ll> pll;
ll max(ll x,ll y){
if(x>y)return x;
else return y;
}
ll min(ll x,ll y){
  return x+y-max(x,y);
}

const int dx[8]={0,0,1,1,1,-1,-1,-1};
const int dy[8]={1,-1,1,0,-1,1,0,-1};


void dfs(vector<vector<int>> &graph,int x,int y,vector<vector<bool>> &seen,int h,int w){
  seen[x][y]=true;
  rep(i,8){
    rep(j,8){
      int ax=x+dx[i];
      int ay=y+dy[i];
      if(ax<0||ay<0||ax>=h||ay>=w)continue;
      if(seen[ax][ay])continue;
      if(graph[ax][ay]==0)continue;
      dfs(graph,ax,ay,seen,h,w);
    }
  }
}

int main() {
rep(aaa,100000000){
  int h,w;cin >> w >> h;
  if(h==0&&w==0)return 0;
  vector<vector<bool>> seen(h,vector<bool>(w,false));
  vector<vector<int>> graph(h,vector<int>(w));
  rep(i,h)rep(j,w)cin >> graph[i][j];
  int ans=0;
  rep(v,h){
    rep(u,w){
      if(seen[v][u])continue;
      if(graph[v][u]==0)continue;
      dfs(graph,v,u,seen,h,w);
      ans++;
    }
  }
  cout << ans << endl;
}
}

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

typedef pair<int, int> P;

int dx[8] = { 1,0,-1,0,1,1,-1,-1 }, dy[8] = { 0,1,0,-1,1,-1,1,-1 };

int main() {
	int w, h;
	while (cin >> w >> h, w) {
		vector<vector<int>>ma(w, vector<int>(h));
		for (int i = 0; i < h;i++) {
			for (int j = 0; j < w;j++) {
				cin >> ma[j][i];
			}
		}
		int ans = 0;
		for (int i = 0; i < w;i++) {
			for (int j = 0; j < h;j++) {
				if (ma[i][j] == 1) {
					ans++;
					queue<P> q;
					q.push(P(i, j));
					ma[i][j] = 0;
					while (!q.empty()) {
						P p = q.front(); q.pop();
						for (int k = 0; k < 8; k++) {
							int nx = p.first + dx[k], ny = p.second + dy[k];
							if (nx < w && nx>=0 && ny < h && ny>=0 && ma[nx][ny]==1) {
								ma[nx][ny] = 0; q.push(P(nx, ny));
							}
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
}