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 <cstdio>

using namespace std;

int W, H;

int field[52][52];
bool visited[52][52];
int tx[] = {1, 1, 1, 0, -1, -1, -1, 0};
int ty[] = {1, 0, -1, -1, -1, 0, 1, 1};

void island(int y, int x)
{
  visited[y][x] = true;
  for (int i = 0; i < 8; i++) {
    if (!visited[y + ty[i]][x + tx[i]] && field[y + ty[i]][x + tx[i]] == 1) {
      island(y + ty[i], x + tx[i]);
    }
  }
}

void solve()
{
  fill_n(&visited[0][0], 52 * 52, false);
  int c = 0;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (!visited[i][j] && field[i][j] == 1) {
	island(i, j);
	c++;
      }
    }
  }
  printf("%d\n", c);
}

int main()
{
  while (scanf("%d %d ", &W, &H) == 2) {
    if (W == 0 && H == 0) {
      return 0;
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
	scanf("%d ", &field[i][j]);
      }
    }
    solve();
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (ll)(1E9+7)
#define PI 3.1415926535897932384

template <class T>ostream &operator<<(std::ostream &o, const vector<T> &v)
{
	rep(i, v.size()) {
		o << (i > 0 ? " " : "") << v[i];
	}
	return o;
}



int main() {
	int w, h;
	int dx[8] = { -1, 0 , 1,-1,1,-1,0,1};
	int dy[8] = { -1,-1 ,-1, 0,0, 1,1,1};
	int input;
	while (1) {
		cin >> w >> h;
		if (w == 0) break;
		vector<vector<int>> field(h + 2, vector<int>(w + 2, 2));
		for (int i = 1; i < h + 1; i++) {
			for (int j = 1; j < w + 1; j++) {
				cin >> input;
				if (input == 1) {
					field[i][j] = 1;
				}
				else {
					field[i][j] = 0;
				}
			}
		}
		/*
		rep(i, h + 2) {
			rep(j, w + 2) {
				cout << field[i][j] << " " ;
			}
			cout << endl;
		}
		*/
		int ans = 0;
		for (int i = 1; i < h + 1; i++) {
			for (int j = 1; j < w + 1; j++) {
				if (field[i][j] == 1) {
					queue<pair<int, int>> que;
					que.emplace(i, j);
					ans++;
					while (!que.empty()) {
						pair<int, int> now = que.front();
						que.pop();
						if (field[now.first][now.second] == 0) continue;
						field[now.first][now.second] = 0;
						rep(k, 8) {
							if (field[now.first + dy[k]][now.second + dx[k]] == 1) {
								que.emplace(now.first + dy[k], now.second + dx[k]);
							}

						}
					}
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<int> dy = {1, 1, 0, -1, -1, -1, 0, 1};
vector<int> dx = {0, 1, 1, 1, 0, -1, -1, -1};
int main(){
  while (1){
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0){
      break;
    }
    vector<vector<int>> c(h + 2, vector<int>(w + 2, 0));
    for (int i = 1; i <= h; i++){
      for (int j = 1; j <= w; j++){
        cin >> c[i][j];
      }
    }
    int ans = 0;
    vector<vector<bool>> used(h + 2, vector<bool>(w + 2, false));
    for (int i = 1; i <= h; i++){
      for (int j = 1; j <= w; j++){
        if (!used[i][j] && c[i][j] == 1){
          used[i][j] = true;
          ans++;
          queue<pair<int, int>> Q;
          Q.push(make_pair(i, j));
          while (!Q.empty()){
            int y = Q.front().first;
            int x = Q.front().second;
            Q.pop();
            for (int k = 0; k < 8; k++){
              int y2 = y + dy[k];
              int x2 = x + dx[k];
              if (!used[y2][x2] && c[y2][x2] == 1){
                used[y2][x2] = true;
                Q.push(make_pair(y2, x2));
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

using ll = long long;

class UnionFind {
public:
    vector<int> data;
    UnionFind(int size) : data(size, -1) { }
    bool connect(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 same(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)];
    }
};
int dx[] = {1, 1, 1, 0, -1, -1, -1, 0};
int dy[] = {1, 0, -1, -1, -1, 0, 1, 1};

int main()
{
    int h,w;
    while(cin >> h >> w){
        if(h == 0)return 0;
        map<pair<int,int>, int> se;
        int cnt = 0;
        for(int x = 0;x < w;x++){
            for(int y = 0;y < h;y++){
                int c;cin >> c;
                if(c){
                    se[make_pair(x, y)] = cnt;
                    cnt++;
                }
            }
        }
        UnionFind Uni(cnt);
        for(auto a : se){
            int x = a.first.first;
            int y = a.first.second;
            for(int i = 0;i < 8;i++){
                if(se.find(make_pair(x + dx[i], y + dy[i])) != se.end()){
                    Uni.connect(a.second, se[make_pair(x + dx[i], y + dy[i])]);
                }
            }
        }
        set<int> s;
        for(int i = 0;i < cnt;i++){
            s.insert(Uni.root(i));
        }
        std::cout<<s.size()<<endl;
    }
}