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,num) for(ll i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){T x; cin >> x; return (x);}
template<typename T=ll,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

class ConnectNodeInfo{
	vector<vector<pair<ll,ll>>> graph;
public:
	ConnectNodeInfo(int node_num){
		graph.resize(node_num);
	}
	void AddNonDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
		graph[v].emplace_back(u,w);
	}
	void AddDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
	}
	vector<pair<ll,ll>>& operator[](ll index){
		return graph[index];
	}
	size_t size(){return graph.size();}
};

vector<int> D,F;

int dfs(int p,int num,int H,int W,vector<vector<int>>& M,vector<vector<int>>& D){
	int u=p%W,v=p/W;
	D[v][u] = num;
	if(u>0 && M[v][u-1] && D[v][u-1]==-1){
		dfs(u-1+v*W,num,H,W,M,D);
	}
	if(u<W-1 && M[v][u+1] && D[v][u+1]==-1){
		dfs(u+1+v*W,num,H,W,M,D);
	}
	if(v>0 && M[v-1][u] && D[v-1][u]==-1){
		dfs(u+(v-1)*W,num,H,W,M,D);
	}
	if(v<H-1 && M[v+1][u] && D[v+1][u]==-1){
		dfs(u+(v+1)*W,num,H,W,M,D);
	}

	if(u>0 && v>0 && M[v-1][u-1] && D[v-1][u-1]==-1){
		dfs(u-1+(v-1)*W,num,H,W,M,D);
	}
	if(u<W-1 && v<H-1 && M[v+1][u+1] && D[v+1][u+1]==-1){
		dfs(u+1+(v+1)*W,num,H,W,M,D);
	}
	if(v>0 && u<W-1 && M[v-1][u+1] && D[v-1][u+1]==-1){
		dfs(u+1+(v-1)*W,num,H,W,M,D);
	}
	if(v<H-1 && u>0 && M[v+1][u-1] && D[v+1][u-1]==-1){
		dfs(u-1+(v+1)*W,num,H,W,M,D);
	}
	return 0;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	while(true){
		int W=in(),H=in();
		if(!W && !H) break;

		vector<vector<int>> M(H,vector<int>(W,0)),D(H,vector<int>(W,-1));
		REP(i,H){
			REP(j,W){
				M[i][j]=in();
			}
		}

		int num=0;
		REP(i,H){
			REP(j,W){
				if(M[i][j] && D[i][j]==-1){
					dfs(j+i*W,++num,H,W,M,D);
				}
			}
		}
		out(num);
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
enum class WalkingResult { undef, walked };
enum class LandS { sea, land };
using LMap = vector<vector<LandS>>;

void solve(int w, int h) {
    LMap lMap(h, vector<LandS>(w));
    vector<pair<int, int>> lands;
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            int t;
            cin >> t;
            lMap[i][j] = t == 1 ? LandS::land : LandS::sea;
            if (t == 1) lands.push_back(make_pair(i, j));
        }
    }

    vector<vector<WalkingResult>> wMap(h, vector<WalkingResult>(w, WalkingResult::undef));
    int count = 0;
    stack<pair<int, int>> s;
    for (int i = 0; i < lands.size(); ++i) {
        pair<int, int> x = lands[i];
        bool already = wMap[x.first][x.second] == WalkingResult::walked;
        if (already) continue;
        s.push(x);
        while(!s.empty()) {
            pair<int, int> pos = s.top();
            s.pop();
            bool out = pos.first < 0 || h <= pos.first || pos.second < 0 || w <= pos.second;
            if (out) continue;
            bool sea = lMap[pos.first][pos.second] == LandS::sea;
            bool walked = wMap[pos.first][pos.second] == WalkingResult::walked;
            if (sea || walked) continue;
            wMap[pos.first][pos.second] = WalkingResult::walked;
            s.push(make_pair(pos.first + 1, pos.second));
            s.push(make_pair(pos.first - 1, pos.second));
            s.push(make_pair(pos.first, pos.second + 1));
            s.push(make_pair(pos.first, pos.second - 1));
            s.push(make_pair(pos.first - 1, pos.second - 1));
            s.push(make_pair(pos.first + 1, pos.second - 1));
            s.push(make_pair(pos.first - 1, pos.second + 1));
            s.push(make_pair(pos.first + 1, pos.second + 1));
        }
        count++;
    }
    cout << count << endl;
}

int main() {

    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        solve(w, h);
    }
    return 0;
}

C++: #include <iostream>
using namespace std;

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

void dfs(int x,int y) {
    used[x][y]=1;
    for(int dx=-1; dx<=1; ++dx) {
        for(int dy=-1; dy<=1; ++dy) {
            int xx=x+dx;
            int yy=y+dy;
            if(x+dx < 0 || x+dx >=h) continue;
            if(y+dy < 0 || y+dy >=w) continue;
            if(used[xx][yy] || c[xx][yy]==0) continue;
            dfs(xx,yy);
        }
    }
}

int main() {
    while(true) {
        int ans=0;
        cin>>w>>h;
        if(!w) return 0;
        for(int i=0; i<h; ++i) {
            for(int j=0; j<w; ++j) {
                cin>>c[i][j];
                used[i][j]=0;
            }
        }
        for(int i=0; i<h; ++i) {
            for(int j=0; j<w; ++j) {
                if(used[i][j] || c[i][j]==0) continue;
                //cerr<<i<<" "<<j<<endl;
                dfs(i,j);
                ans++;
            }
        }
        cout<<ans<<endl;
    }
}