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>

using namespace std;

int w,h,res;
vector<vector<int> > map;
vector<vector<bool> > visited;
int dx[8] = {0,1,0,-1,1,1,-1,-1};
int dy[8] = {1,0,-1,0,1,-1,1,-1};

void bfs(int y,int x){
	if(y >= h || y < 0 || x >= w || x < 0)return;
	if(map[y][x] == 0)return;
	if(visited[y][x])return;
	visited[y][x] = true;
	for(int i=0;i<8;++i){
		bfs(y+dy[i],x+dx[i]);
	}
}

int main(void){
	while(1){
		res = 0;
		cin >> w >> h;
		if(w == 0 && h == 0)break;
		map.resize(h);
		visited.resize(h);
		for(int i=0;i<h;++i){
			map[i].resize(w);
			visited[i].resize(w);
			for(int j=0;j<w;++j){
				visited[i][j] = false;
				cin >> map[i][j];
			}
		}
		for(int i=0;i<h;++i){
			for(int j=0;j<w;++j){
				if(map[i][j] == 1&& visited[i][j] == false){
					bfs(i,j);
					res++;
				}
			}
		}
		cout << res << endl;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;

struct UnionFind {
        int n;
        vector<int> parent;
        vector<int> rank;
        vector<int> num;
        int find(int x) {
                if (parent[x] == x) return  x;
                return parent[x] = find(parent[x]);
        }
        UnionFind(int n_) {
                n = n_;
                parent.resize(n);
                for (int i = 0; i < n; i ++) parent[i] = i;
                rank.assign(n, 0);
                num.assign(n, 1);
        }
        void unite(int x, int y) {
                if ((x = find(x)) != (y = find(y))) {
                        if (rank[x] < rank[y]) {
                                parent[x] = y;
                                num[y] += num[x];
                        } else {
                                parent[y] = x;
                                if (rank[x] == rank[y]) rank[x] ++;
                                num[x] += num[y];
                        }
                        n --;
                }
        }
        bool same(int x, int y) { return find(x) == find(y); }
        int get() { return n; }
        int get(int x) { return num[find(x)]; }
};

int main() {
        int w, h;
        while (scanf("%d%d", &w, &h), w) {
                vector<vector<int>> s(h, vector<int> (w));
                int lands = 0;
                map<pair<int, int>, int> ptol;
                for (int i = 0; i < h; i ++) {
                        for (int j = 0; j < w; j ++){
                                scanf("%d", &s[i][j]);
                                if (s[i][j] == 1) {
                                        ptol[make_pair(i, j)] = lands;
                                        lands ++;
                                }
                        }
                }
                UnionFind uf(lands);
                //cerr << lands << endl;
                for (int i = 0; i < h - 1; i ++) {
                        for (int j = 0; j < w - 1; j ++) {
                                if (s[i][j] == 1) {
                                        if (s[i + 1][j] == 1) uf.unite(ptol[make_pair(i, j)], ptol[make_pair(i + 1, j)]);
                                        if (s[i + 1][j + 1] == 1) uf.unite(ptol[make_pair(i, j)], ptol[make_pair(i + 1, j + 1)]);
                                        if (s[i][j + 1] == 1) uf.unite(ptol[make_pair(i, j)], ptol[make_pair(i, j + 1)]);
                                }
                        }
                }
                for (int i = 0; i < h - 1; i ++) {
                        if (s[i][w - 1] == 1 && s[i + 1][w - 1] == 1) uf.unite(ptol[make_pair(i, w - 1)], ptol[make_pair(i + 1, w - 1)]);
                        if (w > 1 && s[i][w - 1] == 1 && s[i + 1][w - 2] == 1) uf.unite(ptol[make_pair(i, w - 1)], ptol[make_pair(i + 1, w - 2)]);
                }
                for (int j = 0; j < w - 1; j ++) {
                        if (s[h - 1][j] == 1 && s[h - 1][j + 1] == 1) uf.unite(ptol[make_pair(h - 1, j)], ptol[make_pair(h - 1, j + 1)]);
                        if (h > 1 && s[h - 1][j] == 1 && s[h - 2][j + 1] == 1) uf.unite(ptol[make_pair(h - 1, j)], ptol[make_pair(h - 2, j + 1)]);
                }
                printf("%d\n", uf.get());
        }
        return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<eps)
int w,h;
int f[55][55];
int dx[8]={0,0,1,1,1,-1,-1,-1};
int dy[8]={1,-1,1,0,-1,1,-1,0};
void dfs(int x,int y)
{
	f[x][y]=0;
	for(int i=0;i<8;i++)
	{
		int nx=x+dx[i],ny=y+dy[i];
		if(f[nx][ny]==1)dfs(nx,ny);
	}
}
void print()
{
	for(int i=1;i<=h;i++)
	{
		for(int j=1;j<=w;j++)
		{
			cout << f[i][j] << ' ';
		}
		cout << endl;
	}
}
int main()
{
	while(1)
	{
		int ans=0;
		memset(f,0,sizeof(f));
		cin >> w >> h;
		if(w==0&&h==0)break;
		for(int i=1;i<=h;i++)
		{
			for(int j=1;j<=w;j++)
			{
				cin >> f[i][j];
			}
		}
		for(int i=1;i<=h;i++)
		{
			for(int j=1;j<=w;j++)
			{
				if(f[i][j]==1)
				{
					ans++;
					dfs(i,j);
					//print();
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}