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 <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <array>
#include <map>
#include <set>
using namespace std;

#define IS_POWER_TWO(x) (!(x & (x-1))) 
struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) {
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {
		return -data[root(x)];
	}
};

typedef pair<int, int> pii;
const pii Dir_eight[8]{
	{0,-1},{-1,0},{1,0},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}
};

int main()
{
	int w, h;

	while (scanf("%d %d", &w, &h)) {
		if (w == 0)
			return 0;
		array<array<int, 52>, 52> map = {};
		UnionFind uf(2501);

		for (int i = 0; h > i; i++) {
			for (int j = 0; w > j; j++) {
				int a;
				scanf("%d", &a);

				map[j + 1][i + 1] = a;
				if (!a)uf.unionSet(i*w + j, h*w);
			}
		}

		for (int i = 1; h >= i; i++) {
			for (int j = 1; w >= j; j++) {
				if (map[j][i]) {
					for (int k = 0; 8 > k; k++) {
						if (map[j + Dir_eight[k].first][i + Dir_eight[k].second]) {
							uf.unionSet(j + Dir_eight[k].first + (i + Dir_eight[k].second-1)*w-1, (i-1)*w + j-1);
						}
					}
				}
			}
		}

		set<int> ver;

		for (int i = 0; h*w+1 > i; i++) {
			ver.insert(uf.root(i));
		}
		
		printf("%d\n", ver.size() - 1);
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <utility>
#include <memory>
#include <functional>
#include <deque>
#include <cctype>
#include <numeric>
#include <ctime>
#include <bitset>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>

using namespace std;

//define
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define dump(x)  cerr << #x << " = " << (x) << endl;

#define INF (INT_MAX/3)
#define PI (2*acos(0.0))
#define EPS (1e-8)
#define MOD (1000000007LL)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<ll> vll;
typedef vector<vector<ll> > vvll;

#define vv(t) vector< vector< T > >
#define pb push_back
#define pu push
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define EQ(a,b) (abs((a)-(b))<EPS)

int w, h;
char field[50][50];
typedef pair<int, int> P;

void bfs(int sh, int sw){
  queue<P> que;
  que.push(P(sh, sw));
  while(!que.empty()){
    P p = que.front();
    que.pop();
    int nh = p.first, nw = p.second;
    field[nh][nw] = '0';
    for(int i = -1; i <= 1; i++){
      for(int j = -1; j <= 1; j++){
        if(i == 0 && j == 0) continue;
        int th = nh + i, tw = nw + j;
        if(th < 0 || th >= h || tw < 0 || tw >= w || field[th][tw] != '1') continue;
        que.push(P(th, tw));
      }
    }
  }
}

int main(void){
  ios_base::sync_with_stdio(0);
  while(cin >> w >> h, w || h){
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++) cin >> field[i][j];
    }
    int ans = 0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(field[i][j] == '0') continue;
        bfs(i, j);
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0; 
}

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

struct mapst {
	int x, y;
};

#define func(x,y) (c[y][x]&&!f[y][x])

int main() {
	int w, h, ans=0;
	int c[50][50], f[50][50];
	queue<mapst> q;
	while(1) {
		cin >> w >> h;
		if(!w&&!h) break;
		ans = 0;
		for(int i=0; i<h; ++i) for(int j=0; j<w; ++j) {
			cin >> c[i][j];
			f[i][j] = 0;
		}
		for(int i=0; i<h; ++i) for(int j=0; j<w; ++j) {
			mapst m = {j, i}, ms;
			if(func(j,i)) {
				q.push(m);
				ans++;
				do {
					m = q.front();
					q.pop();
					
					f[m.y][m.x] = 1;
					if(m.x>0&&m.y>0) if(func(m.x-1,m.y-1)) {
						ms.x = m.x-1; ms.y = m.y-1;
						q.push(ms);
					}
					if(m.y>0) if(func(m.x,m.y-1)) {
						ms.x = m.x; ms.y = m.y-1;
						q.push(ms);
					}
					if(m.x<w-1&&m.y>0) if(func(m.x+1,m.y-1)) {
						ms.x = m.x+1; ms.y = m.y-1;
						q.push(ms);
					}
					if(m.x<w-1) if(func(m.x+1,m.y)) {
						ms.x = m.x+1; ms.y = m.y;
						q.push(ms);
					}
					if(m.x<w-1&&m.y<h-1) if(func(m.x+1,m.y+1)) {
						ms.x = m.x+1; ms.y = m.y+1;
						q.push(ms);
					}
					if(m.y<h-1) if(func(m.x,m.y+1)) {
						ms.x = m.x; ms.y = m.y+1;
						q.push(ms);
					}
					if(m.x>0&&m.y<h-1) if(func(m.x-1,m.y+1)) {
						ms.x = m.x-1; ms.y = m.y+1;
						q.push(ms);
					}
					if(m.x>0) if(func(m.x-1,m.y)) {
						ms.x = m.x-1; ms.y = m.y;
						q.push(ms);
					}
				}while(!q.empty());
			}
		}
		cout << ans << endl;
	}
}