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;
#define int long long
#define All(v) (v).begin(),(v).end()
int dy[8] = {-1,0,1,0,-1,1,1,-1};
int dx[8] = {0,1,0,-1,1,1,-1,-1};

int h,w,cnt=0;
int mas[55][55];
void dfs(int y,int x){
    mas[y][x]=0;
    for(int i=0;i<8;i++){
        int ny=y+dy[i];
        int nx=x+dx[i];
        if(ny<0||nx<0||ny>=h||nx>=w)continue;
        if(mas[ny][nx]==1){
            dfs(ny,nx);
        }
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(true){
        cin>>w>>h;
        if(h==0&&w==0)break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>mas[i][j];
            }
        }
        cnt=0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(mas[i][j]==1){
                    cnt++;
                    dfs(i,j);
                }
            }
        }
        cout<<cnt<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repr(i,a,n) for(int i=a;i>=n;i--)
#define INF 999999999
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

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

int w,h;

int dfs(int i, int j, int c[][51]) {
    if(c[i][j] == 1) {
        c[i][j] = 0;
        rep(di,i-1,i+2) {
            rep(dj,j-1,j+2) {
                if(di != i || dj != j) {
                    if(di<0 || di>=h || dj<0 || dj>=w) continue;
                    if(c[di][dj] == 1) dfs(di,dj,c);
                }
            }
        }
        return 1;
    }
    return 0;
}

int main() {
    int c[51][51];

    while(cin >> w >> h) {
        if(w == 0 && h == 0) break;

        rep(i,0,h) {
            rep(j,0,w) {
                cin >> c[i][j];
            }
        }

        int ret = 0;
        rep(i,0,h) {
            rep(j,0,w) {
                ret += dfs(i,j,c);
            }
        }

        cout << ret << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

using ll = long long;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

template<class T>
void print(vector<T> a) {
  cout << "[ ";
  REP(i, a.size()) cout << a[i] << " ";
  cout << "]" << endl;
}

void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

vector<pair<ll, ll>> d = {
  {-1, -1},
  {+1, -1},
  {-1, +1},
  {+1, +1},
  {+0, +1},
  {+0, -1},
  {+1, +0},
  {-1, +0},
};

void dfs(ll x, ll y, vector<vector<ll>> &graph) {
  graph[y][x] = 0;
  for(auto v: d) {
    ll nx = v.first + x;
    ll ny = v.second + y;
    if (0 <= ny && ny < graph.size() && 0 <= nx && nx < graph[y].size() && graph[ny][nx]) {
      dfs(nx, ny, graph);
    }
  }
}

bool solve() {
  ll input(w, h);
  if (w == 0 && h == 0) return false;

  vector<vector<ll>> graph(h);
  REP(i, h) {
    vector<ll> r(w);
    REP(j, w) cin >> r[j];
    graph[i] = r;
  }

  ll cnt = 0;
  REP(i, h) REP(j, w) {
    if (graph[i][j]) {
      dfs(j, i, graph);
      cnt++;
    }
  }
  cout << cnt << endl;
  return true;
}

int main() {
  while (solve());
}

C++: #include<bits/stdc++.h>
#define LL long long
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPA(i,n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define PII pair<int,int>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define PDD pair<double, double>
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9)
#define INFLL ((LL)1e18)
#define ALL(x) (x).begin(),(x).end()
#define ctoi(x) (x - 'a') 
#define CTOI(x) (x - 'A')
#define BIT(x) (1 << (x))
using namespace std;

LL modinv(LL a){
    LL b = MOD, u = 1, v = 0;
    while(b){
        LL t = a / b;
        a -= t * b;swap(a, b);
        u -= t * v;swap(u, v);
    }
    u%=MOD;
    if(u < 0) u += MOD;
    return u;

}

LL exp(LL a, LL b){
    LL sum = a;
    LL res = 1;
    while(b){
        if(b&1){
            res = (res * sum)%MOD;
        }
        sum = (sum * sum) % MOD;
        b>>=1;
    }
    return res;
}

int N, M;
int mx[]={1, 1, 1, 0, -1, -1, -1, 0}, my[]={1, 0, -1, -1, -1, 0, 1, 1};

int main(){
    while(cin>>M>>N&&N){
        vector<vector<int> > m(N+2, vector<int>(M+2, 0));
        REPA(i, N+1){
            REPA(j, M+1){
                cin >> m[i][j];
            }
        }
        int ans = 0;
        REPA(i, N+1){
            REPA(j, M+1){
                if(!m[i][j])continue;
                ++ans;
                queue<PII> que;
                que.push(PII(i, j));
                m[i][j]=0;
                while(!que.empty()){
                    PII d = que.front();
                    que.pop();
                    REP(i, 8){
                        int nx = d.first + mx[i];
                        int ny = d.second + my[i];
                        if(!m[nx][ny])continue;
                        m[nx][ny]=0;
                        que.push(PII(nx, ny));
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}