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,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define all(a)  (a).begin(),(a).end()
#define IOS std::cin.tie(0),std::ios::sync_with_stdio(false)

namespace idaten {

	using namespace std;
	using ll = int_fast64_t;
	using ull = uint_fast64_t;
	using P = std::pair<int64_t, int64_t>;
	template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
	template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }
	template<class Type>struct is_vector : std::false_type {};
	template<class ValueType, class Alloc>struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
	template <typename T> inline std::ostream& operator << (std::ostream& out, const std::vector<T>& v) {
		if (v.empty())return out;
		constexpr bool is_vector_v = is_vector<T>::value;
		if (is_vector_v)for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
		else for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? " " : "");
		return out;
	}
	inline void put() {}
	template<class T> inline void put(const T& first) { std::cout << first << "\n"; }
	template<class T, class... N> inline void put(const T& first, const N& ... rest) { std::cout << first << " "; put(rest...); }
	inline void putn() {}
	template<class T, class... N> inline void putn(const T& first, const N& ... rest) { std::cout << first << "\n"; putn(rest...); }

	void dfs(int now, vector<vector<int>>& path, vector<int>& visit, int& time) {
		visit[now] = time;
		REP(i, path[now].size()) {
			int t = path[now][i];
			if (visit[t] < 0) {
				dfs(t, path, visit, time);
			}
		}
	}

	void solve() {
		IOS;
		int h, w;
		cin >> w >> h;
		while (h + w > 0) {
			vector<vector<int>> c(h, vector<int>(w));
			vector<vector<int>> path(h * w);
			REP(i, h) {
				REP(j, w) {
					cin >> c[i][j];
				}
			}
			int dy[] = { 0,1,1,1,0,-1,-1,-1 };
			int dx[] = { 1,1,0,-1,-1,-1,0,1 };
			REP(i, h) {
				REP(j, w) {
					if (c[i][j] == 1) {
						REP(k, 8) {
							int ny = i + dy[k];
							int nx = j + dx[k];
							if (0 <= ny && ny < h && 0 <= nx && nx < w) {
								if (c[ny][nx] == 1) {
									int next = ny * w + nx;
									int now = i * w + j;
									path[next].push_back(now);
									path[now].push_back(next);
								}
							}
						}
					}
				}
			}
			vector<int> visit(h * w, -1);
			int cnt = 0;
			REP(i, h) {
				REP(j, w) {
					int t = i * w + j;
					if (c[i][j]==1&&visit[t] < 0) {
						dfs(t, path, visit, cnt);
						cnt++;
					}
				}
			}
			put(cnt);
			cin >> w >> h;
		}
	}
} // namespace idaten

int main() {
	idaten::solve();
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int ans;
int w, h;
//?§???????
vector<int> vx = { -1, 0, 1, -1, 1, -1, 0, 1 };
vector<int> vy = { 1, 1, 1, 0, 0, -1, -1, -1 };
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(){
	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>
using namespace std;
#define rep(i, b) for (int i = 0; i < int(b); i++)

int w, h;
vector<vector<int>> g;

int const dx[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
int const dy[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
inline bool in(int x, int y){
    return 0 <= x && x < w && 0 <= y && y < h;
}

void fill(int x, int y){
    g[y][x] = 0;
    rep(i, 8){
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (in(xx, yy) && g[yy][xx]) fill(xx, yy);
    }
}

int main(){
    while (cin >> w >> h, w | h){
        g.assign(h, vector<int>(w));
        rep(i, h)rep(j, w) cin >> g[i][j];
        int c = 0;
        rep(i, h)rep(j, w){
            if (g[i][j]){
                c++;
                fill(j, i);
            }
        }
        cout << c << endl;
    }
}