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>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

const ll d[8][2] = {{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}};

void dfs(const vector<vector<ll>> &c, vector<vector<ll>> &g, ll x, ll y, ll root) {
    if (g[y][x] != -1) return;
    if (c[y][x] == 0) return;
    g[y][x] = root;
    rep(i, 8) {
        ll nx = x + d[i][0];
        ll ny = y + d[i][1];
        if ((nx < 0) || (nx >= sz(c[0])) || (ny < 0) || (ny >= sz(c))) continue;
        dfs(c, g, nx, ny, root);
    }
}

void solve(ll w, ll h) {
    vector<vector<ll>> c(h, vector<ll>(w)), g(h, vector<ll>(w, -1));
    rep(i, h) rep(j, w) cin >> c[i][j];
    set<ll> s;
    rep(i, h) {
        rep(j, w) {
            dfs(c, g, j, i, i * w + j);
            if (g[i][j] >= 0) s.insert(g[i][j]);
        }
    }
    cout << sz(s) << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    while(true) {
        ll w, h;
        cin >> w >> h;
        if ((w == 0) && (h == 0)) break;
        solve(w, h);
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <cstring>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for(int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int> >
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

#define MAX_V 1000000

int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

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

  vector<int> ans;
  while(true) {
      int w, h;
      cin >> w >> h;
      if(w == 0 && h == 0) break;

      int c[h][w];
      bool reached[h][w];
      rep(i, 0, h) rep(j, 0, w) {
          cin >> c[i][j];
          reached[i][j] = false;
      }

      int rsl = 0;
      rep(y, 0, h) {
          rep(x, 0, w) {
              if(c[y][x] == 0) continue;
              if(reached[y][x]) continue;

              rsl++;
              stack<pair<int, int> > s;
              s.push(make_pair(x, y));
              reached[y][x] = true;
              while(!s.empty()) {
                  pair<int, int> p = s.top();
                  s.pop();
                  rep(i, 0, 8) {
                      int nx = p.first + dx[i], ny = p.second + dy[i];
                      if(!(0 <= nx && nx < w && 0 <= ny && ny < h)) continue;
                      if(c[ny][nx] == 0) continue;
                      if(reached[ny][nx]) continue;

                      reached[ny][nx] = true;
                      s.push(make_pair(nx, ny));
                  }
              }
          }
      }
      ans.push_back(rsl);
  }
  rep(i, 0, ans.size()) cout << ans[i] << endl;

}

C++: #include <bits/stdc++.h>
using namespace std;

int x,y;
const int mx[8]={-1,-1,-1,0,0,1,1,1};
const int my[8]={-1,0,1,-1,1,-1,0,1};

void dfs(vector<vector<int>> &c,int H,int W,int& h,int& w)
{
c[H][W] = 0;
for(int i=0;i<8;i++){y=H+my[i],x=W+mx[i];if(y<0||y>h-1||x<0||x>w-1)continue;if(!c[y][x])continue;dfs(c,y,x,h,w);}
}

int main()
{
int w,h,ans;
vector<vector<int>> c(50,vector<int>(50));
scanf("%d%d", &w, &h);
while(w!=0&&h!=0)
{
ans=0;
for(int i=0;i<h;i++)for(int j=0;j<w;j++)scanf("%d", &c[i][j]);
for(int i=0;i<h;i++)for(int j=0;j<w;j++){if(!c[i][j])continue;dfs(c,i,j,h,w);ans++;}
printf("%d\n",ans);
scanf("%d%d", &w, &h);
}
return 0;
}

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

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) {
    v[r][c] = 0;
    for(int dr = -1; dr <= 1; ++dr) {
        for(int dc = -1; dc <= 1; ++dc) {
            int nr = r + dr;
            int nc = c + dc;
            if(inRange(0, nr, v.size()) && inRange(0, nc, v[0].size()) && v[nr][nc]) {
                fill(v, nr, nc);
            }
        }
    }
}

int main() {
    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;
}