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;

//  四方向へのベクトル
const int dx[8] = {-1,0,1,0,1,1,-1,-1};
const int dy[8] = {0,-1,0,1,1,-1,1,-1};

int H,W;
//探索用
bool seen[501][5001];
int MAX = 0;
//using Graph = vector<vector<int>>;
bool roop;
int dfs(const vector<vector<int>> &G,int nh,int nw){
    seen[nh][nw] = true;
    
    for(int i=0;i<8;i++){
        int next_h = nh + dy[i];//8方向へ移動
        int next_w = nw + dx[i];

        if(next_h<0 || next_h >=H ||next_w<0||next_w >= W) continue;
        if(seen[next_h][next_w]) continue;
        if(G[next_h][next_w] == 0) continue;

        dfs(G,next_h,next_w);
    }

}
int main(){
    while(true){
        int cnt=0;
        cin >> W >> H;
        if(H+W == 0) break;

        vector<vector<int>> G(H);
        for(int i=0;i<H;i++){
            for(int k=0;k<W;k++){
                int a;
                cin >> a;
                G[i].push_back(a);
            }
        }
        memset(seen,0,sizeof(seen));

        for(int i=0;i<H;i++){
            for(int k=0;k<W;k++){
                if(G[i][k] == 1&& !seen[i][k]){
                    dfs(G,i,k);
                    cnt++;
                }
            }
        }

        cout <<cnt <<endl;
    }
    return 0;
    
}

C++: #include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;

bool c[51][51];
bool checked[51][51];
int w, h;

void dfs(int y, int x) {
    if (checked[y][x] || !c[y][x]) return;
    checked[y][x] = true;
    repd(i, -1, 2) {
        repd(j, -1, 2) {
            if (i == 0 && j == 0) continue;
            int yy = y + i, xx = x + j;
            if (yy < 0 || yy >= h || xx < 0 || xx >= w) continue;
            dfs(yy, xx);
        }
    }
}

int main()
{
    while (1) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(i, h) {
            rep(j, w) {
                int k;
                cin >> k;
                c[i][j] = k;
                checked[i][j] = false;
            }
        }

        int cnt = 0;
        rep(i, h) {
            rep(j, w) {
                if (!checked[i][j] && c[i][j]) {
                    dfs(i, j);
                    cnt++;
                }
            }
        }

        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string>
#include<limits.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define nrep(i,a,b) for(int i=a;i<b;i++)
#define mrep(i,a) for(int i=a;i<=0;i++)
#define INF LLONG_MAX
using namespace std;

ll w, h, ans;
vector<pair<ll, ll> > idou(8);

void dfs(vector<vector<ll> > &map, ll tate, ll yoko) {
	map[tate][yoko] = ans;
	rep(i, 8) {
		ll t = tate + idou[i].first;
		ll y = yoko + idou[i].second;
		if (0 <= t && t < h && 0 <= y && y < w && !map[t][y]) dfs(map, t, y);
	}
}

int main() {
	//???
	idou[0].first = -1;
	idou[0].second = 0;
	//??????
	idou[1].first = -1;
	idou[1].second = 1;
	//???
	idou[2].first = 0;
	idou[2].second = 1;
	//??????
	idou[3].first = 1;
	idou[3].second = 1;
	//???
	idou[4].first = 1;
	idou[4].second = 0;
	//??????
	idou[5].first = 1;
	idou[5].second = -1;
	//???
	idou[6].first = 0;
	idou[6].second = -1;
	//??????
	idou[7].first = -1;
	idou[7].second = -1;

	while (1) {
		cin >> w >> h;
		if (!w && !h) break;
		ans = 0;
		vector<vector<ll> > map(h, vector<ll>(w, -1));
		rep(i, h) rep(j, w) {
			ll temp; cin >> temp;
			if (temp) map[i][j] = 0;
		}rep(i, h) rep(j, w) {
			if (!map[i][j]) {
				ans++;
				dfs(map, i, j);
			}
		}cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

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

int w, h;
int map[50][50];
int ans;

bool iswalkable(int ty,int tx){
	if(ty >= 0 && ty < h && tx >= 0 && tx < w) return true;
	return false;
}

void dfs(int y, int x){
	if(map[y][x] == 0) return;
	map[y][x] = 0;
	
	for(int vy = -1; vy <= 1; vy++){
		for(int vx = -1; vx <= 1; vx++){
			
			if(vy == 0 && vx == 0) continue;
			int ty = y + vy;
			int tx = x + vx;
			if(iswalkable(ty, tx)){
				dfs(ty, tx);
			}
			
		}
	}
	return;
}
			
	
int main(int argc, char const* argv[])
{
	
	while(true){
		cin >> w >> h;
		if(w == 0 && h == 0) break;

		ans = 0;
		rep(i, h) rep(j, w) cin >> map[i][j];

		rep(i, h){
			rep(j, w){
				if(map[i][j]){
					ans++;
					dfs(i, j);
				}
			}
		}


		cout << ans << endl;
	}
	
	return 0;
}