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>
#include <queue>
#include <utility>

using namespace std;

inline bool inRange(int a, int x, int b) {
    return a <= x && x < b;
}

void fill(vector<vector<int> > &v, int r, int c) {
    queue<pair<int,int> > q;
    q.push(make_pair(r, c));
    v[r][c] = 0;
    while(!q.empty()) {
        const pair<int,int> cur = q.front();
        q.pop();
        for(int dr = -1; dr <= 1; ++dr) {
            for(int dc = -1; dc <= 1; ++dc) {
                int nr = cur.first + dr;
                int nc = cur.second + dc;
                if(inRange(0, nr, v.size()) && 
                   inRange(0, nc, v[0].size()) && v[nr][nc])
                {
                    v[nr][nc] = 0;
                    q.push_back(make_pair(nr, nc))
                }
            }
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    while(true) {
        int C, R;
        cin >> C >> R;
        if(!C && !R) break;

        vector<vector<int> > v(R, vector<int>(C));
        for(int i = 0; i < R; ++i) 
            for(int j = 0; j < C; ++j)
                cin >> v[i][j];

        int cnt = 0;
        for(int r = 0; r < R; ++r) {
            for(int c = 0; c < C; ++c) {
                if(v[r][c]) {
                    ++cnt;
                    fill(v, r, c);
                }
            }
        }

        cout << cnt << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;
 
int c[64][64];
 
void paint(int y, int x) {
  if (y < 0 || y >= 50 || x < 0 || x >= 50) return;
  if (c[y][x] == 0) return;
  c[y][x] = 0;
  for (int dy = -1; dy <= 1; ++dy) {
    for (int dx = -1; dx <= 1; ++dx) {
      paint(y + dy, x + dx);
    }
  }
}
 
int main() {
  int H, W;
  while (cin >> W >> H, H) {
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> c[i][j];
      }
    }
    int res = 0;
    for (int y = 0; y < H; ++y) {
      for (int x = 0; x < W; ++x) {
        if (c[y][x] == 1) {
          ++res;
          paint(y, x);
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define rep3(i, s, n) for (int i = s; i > (int)(n); i--)
#define all(obj) obj.begin(), obj.end()
#define db(x) cerr << #x << ":" << x << " "
#define dbl(x) cerr << #x << ":" << x << "\n"
#define dbv(vec) cerr << #vec << ":"; for (auto e : vec) cerr << e << " "; cout << "\n"
#define dbvv(vv) cerr << #vv << ":\n"; for (auto vec : vv) { for (auto e : vec) cerr << e << " "; cerr << "\n"; }
#define YN(f) cout << (f ? "YES" : "NO") << "\n"
#define Yn(f) cout << (f ? "Yes" : "No") << "\n"
#define yn(f) cout << (f ? "yes" : "no") << "\n"
using ll = long long;
using vi = vector<int>; using vl = vector<ll>; using vs = vector<string>;
using vvi = vector<vi>; using vvl = vector<vl>;
using pii = pair<int, int>; using pll = pair<ll, ll>;
using vpii = vector<pii>; using vpll = vector<pll>;
using ti3 = tuple<int, int, int>; using tl3 = tuple<ll, ll, ll>;

vector<vector<bool>> c;
vector<vector<bool>> r;

void dfs(int i, int j) {
	r[i][j] = true;	
	vi di = { 1, 1, 0, -1, -1, -1, 0, 1 };
	vi dj = { 0, 1, 1, 1, 0, -1, -1, -1 };
	rep(k, 8) {
		int in = i + di[k], jn = j + dj[k];
		if (c[in][jn] and !r[in][jn]) dfs(in, jn);
	}
}

int main() {
	while (true) {
		int w, h; cin >> w >> h;
		if (w == 0 and h == 0) break;
		c.assign(h + 2, {}); rep(i, h + 2) c[i].assign(w + 2, false);
		rep2(i, 1, h + 1) {
			rep2(j, 1, w + 1) {
				int cij; cin >> cij;
				c[i][j] = cij == 1;
			}
		}

		// dbvv(c);
		r.assign(h + 2, {}); rep(i, h + 2) r[i].assign(w + 2, false);
		int ans = 0;		
		rep2(i, 1, h + 1) rep2(j, 1, w + 1) {
			if (c[i][j] and !r[i][j]) {
				dfs(i, j);
				ans++;
			}
		}
		// dbl(ans);
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Pos { int y, x; };

int main(void)
{
    int w, h;
    int map[52][52] = {{0}};
    int nx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
    int ny[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
    queue<Pos> poss;
    Pos now;
    int ans;
    while (true)
    {
        cin >> w >> h;
        if (!w && !h) break;
        ans = 0;
        for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> map[i][j];
        for (int i = 1; i <= h; i++)
        {
            for (int j = 1; j <= w; j++)
            {
                if (map[i][j])
                {
                    poss.push({i, j});
                    while (!poss.empty())
                    {
                        now = poss.front();
                        poss.pop();
                        if (!map[now.y][now.x]) continue;
                        else map[now.y][now.x] = 0;
                        for (int k = 0; k < 8; k++) if (map[now.y + ny[k]][now.x + nx[k]]) poss.push({now.y + ny[k], now.x + nx[k]});
                    }
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}