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>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(a) (a).begin(),(a).end()
 
using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
 
const ll MOD  = 1e9+7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

int mv[3] = {1, 0, -1};

int h, w;
int atlas[50][50];

void dfs(int y, int x){

    rep(i, 3)rep(j, 3){
        int dx = x + mv[i];
        int dy = y + mv[j];

        if(dx < 0 || dx >= w || dy < 0 || dy >= h)continue;

        if(atlas[dy][dx] == 1){
            atlas[dy][dx] = 0;
            dfs(dy, dx);
        }
    }
}

int main(){
    vector<int> ans;
    while(true){
        cin >> w >> h;

        if(h + w == 0)break;

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

        int res = 0;
        rep(i, h)rep(j, w){
            if(atlas[i][j] == 1){
                atlas[i][j] = 0;
                res++;

                dfs(i, j);
            }
        }

        ans.push_back(res);
    }

    rep(i, ans.size()){
        cout << ans[i] << endl;
    }
}

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

using namespace std;

#define rep(i, n) REP((i), 0, (n))
#define REP(i, a, n) for(int i=(a); i<(n); i++)

int w, h;
int c[55][55];

void dfs(int i, int j) {
    if(c[i][j] == 0) return;
    
    c[i][j] = 0;
    dfs(i-1, j-1);
    dfs(i-1, j);
    dfs(i-1, j+1);
    dfs(i  , j-1);
    dfs(i  , j+1);
    dfs(i+1, j-1);
    dfs(i+1, j);
    dfs(i+1, j+1);
}

int main() {
	while(1) {
	    cin >> w >> h;
	    if(w == 0 && h == 0) break;
	    
	    rep(i, h) rep(j, w) cin >> c[i+1][j+1];
	    
	    int ans = 0;
	    rep(i, h) {
	        rep(j, w) {
	            if(c[i+1][j+1] == 0) continue;
	            dfs(i+1, j+1);
	            ans++;
	        }
	    }
	    cout << ans << endl;
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
const int dx[8] = {0,1,1,1,0,-1,-1,-1};
const int dy[8] = {1,1,0,-1,-1,-1,0,1};
int w,h,grid[51][51];
void s(int x, int y){
  grid[y][x] = 0;
  REP(r,8){
    if(x+dx[r]>=0&&x+dx[r]<w&&y+dy[r]>=0&&y+dy[r]<h&&grid[y+dy[r]][x+dx[r]]){
      s(x+dx[r],y+dy[r]);
    }
  }
} 


int main(void){
  while(cin>>w>>h){
    if(w==0) break;
    REP(y,h)
      REP(x,w)
        cin>>grid[y][x];
    int c = 0;
    REP(y,h)
      REP(x,w)
        if(grid[y][x]){
          c++;
          s(x,y);
        }
    cout<<c<<endl;
  }
  return 0;
}

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,P> PP;

//-----------------------------------------------------------------------------

int h,w;
int fld[50][50];
int dx[]={1,-1,0,0,1,1,-1,-1},dy[]={0,0,1,-1,-1,1,-1,1};

void bfs(int sy,int sx) {
	queue<P> q;
	fld[sy][sx]=0;
	q.push(P(sx,sy));
	while(!q.empty()) {
		P p=q.front();q.pop();
		REP(i,8) {
			int nx=p.fi+dx[i],ny=p.se+dy[i];
			if(nx<0||nx>=w||ny<0||ny>=h||fld[ny][nx]==0) continue;
			fld[ny][nx]=0;
			q.push(P(nx,ny));
		}
	}
}

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

	while(true) {
		cin>>w>>h;
		if(w==0) break;
		REP(i,h) REP(j,w) cin>>fld[i][j];
		int cnt=0;
		REP(i,h) {
			REP(j,w) {
				if(fld[i][j]==1) {
					cnt++;
					bfs(i,j);
				}
			}
		}
		cout<<cnt<<endl;
	}

	return 0;
}