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>
using namespace std;
int w,h;
int masu[1010][1010];
int dx[] = {-1,-1,-1,0,0,1,1,1};
int dy[] = {-1,0,1,-1,1,-1,0,1};
void dfs(int y,int x){
    masu[y][x] = 0;
    for(int i=0;i<8;i++){
        int ny = y+dy[i];
        int nx = x+dx[i];
        if(ny<0||ny>=h||nx<0||nx>=w||masu[ny][nx]==0) continue;
        dfs(ny,nx);
    }
}
int main(){
    int ans;
    while(cin >> w >> h,w!=0||h!=0){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++) cin >> masu[i][j];
        }
        ans = 0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(masu[i][j] == 1){
                    ans++;
                    dfs(i,j);
                }
            }
        }
        cout << ans << endl;
    }
    return(0);
}

C++: #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
ll seen[55][55];
ll W, H;
void dfs(int h, int w) {
    seen[h][w] = 0;
    
    for (ll dir = 0; dir < 8; dir++) {
        ll nh = h + dx[dir];
        ll nw = w + dy[dir];
        if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
        if (seen[nh][nw] == 0) continue;
        
        
        dfs(nh, nw);
            
    }
}


int main(){
    while(1 > 0){
        ll ans = 0;
        cin >> W >> H;
        if(W == 0 && H == 0) break;

        for (ll i = 0; i < H; i++) {
            for (ll j = 0; j < W; j++) {
                cin >> seen[i][j];
            }
        }

        for (ll i = 0; i < H; i++) {
            for (ll j = 0; j < W; j++) {
                if(seen[i][j]){
                    dfs(i,j);
                    ans++ ;
                }
            }
        }
        printf("%lld\n", ans);
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

int w, h;
vector<int> ans;

void dfs(int x, int y, vector<vector<int>> &m, vector<vector<bool>> &seen);

int main()
{
    while (true)
    {
        cin >> w >> h;
        // 終了条件
        if ((w == 0) && (h == 0))
        {
            break;
        }
        vector<vector<int>> m(52, vector<int>(52, 0));
        vector<vector<bool>> seen(52, vector<bool>(52, false));
        // データの受け取り
        rep(1, i, h + 1)
        {
            rep(1, j, w + 1)
            {
                cin >> m.at(i).at(j);
            }
        }
        int count = 0;
        //すべての点で探索
        rep(1, i, h + 1)
        {
            rep(1, j, w + 1)
            {
                // 探索済か海のマスならとばす
                if (seen.at(i).at(j) || (m.at(i).at(j) == 0))
                {
                    continue;
                }
                // dfsによる塗りつぶし
                dfs(i, j, m, seen);
                count++;
            }
        }
        ans.push_back(count);
    }
    for (auto a : ans)
    {
        cout << a << endl;
    }
}

void dfs(int x, int y, vector<vector<int>> &m, vector<vector<bool>> &seen)
{
    // 探索済か海のマスなら探索終了
    if (seen.at(x).at(y) || (m.at(x).at(y) == 0))
    {
        return;
    }
    // 探索済の印をつける
    seen.at(x).at(y) = true;
    rep(-1, dx, 2)
    {
        rep(-1, dy, 2)
        {
            dfs(x + dx, y + dy, m, seen);
        }
    }
    return;
}