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>
#include<queue>
#include<cstring>
using namespace std;
int x[50][50], col[50][50], h, w, c, i, j, k, l, p, q;
queue<int>Q, R;
int main() {
	while (true) {
		c = 0;
		memset(col, 0, sizeof(col));
		cin >> h >> w;
		if (h == 0) { break; }
		for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> x[i][j]; } }
		for (i = 0; i < 50; i++) {
			for (j = 0; j < 50; j++) {
				if (x[i][j] == 1 && col[i][j] == 0) {
					c++;
					col[i][j] = c;
					Q.push(i * 100 + j);
					while (!Q.empty()) {
						while (!Q.empty()) {
							p = Q.front() / 100;
							q = Q.front() % 100;
							for (k = -1; k < 2; k++) {
								for (l = -1; l < 2; l++) {
									if (x[p + k][q + l] == 1 && col[p + k][q + l] == 0) {
										col[p + k][q + l] = c;
										R.push((p + k) * 100 + q + l);
									}
								}
							}
							Q.pop();
						}
						while (!R.empty()) {
							Q.push(R.front());
							R.pop();
						}
					}
				}
			}
		}
		cout << c << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

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

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

    for(;;){
        int R, C;
        cin >> R >> C;
        if(R == 0 && C == 0)    return 0;
        vector<vector<int>> c(C, vector<int>(R));
        for(int i = 0; i < C; ++i){
            for(int j = 0; j < R; ++j){
                cin >> c[i][j];
            }
        }
        vector<vector<bool>> used(C, vector<bool>(R, false));
        auto dfs = [&](auto&& self, int x, int y) -> void {
            used[x][y] = true;
            for(int i = 0; i < 8; ++i){
                int nx = x + dx[i], ny = y + dy[i];
                if(nx < 0 || nx >= C || ny < 0 || ny >= R)  continue;
                if(used[nx][ny] || c[nx][ny] == 0)  continue;
                self(self, nx, ny);
            }
            return;
        };
        int ans = 0;
        for(int i = 0; i < C; ++i){
            for(int j = 0; j < R; ++j){
                if(used[i][j] || c[i][j] == 0)  continue;
                ++ans;
                dfs(dfs, i, j);
            }
        }
        cout << ans << '\n';
    }
}

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

int W,H;
int c[51][51]={0};
int d[51][51]={0};
int island=0;

bool valid(pair<int,int> a){
    return (a.first>=1 && a.first<=W && a.second>=1 && a.second<=H);
}

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

void bfs(pair<int,int> s){
    queue<pair<int,int>> Q;

    if(c[s.first][s.second]==1 && d[s.first][s.second]==0 && valid(s)){
        Q.push(s);
        d[s.first][s.second]=1;
        ++island;

        while(!Q.empty()){
            pair<int,int> cur=Q.front();
            Q.pop();

            for(int i=-1;i<=1;++i){
                for(int j=-1;j<=1;++j){
                    pair<int,int> dst=make_pair(cur.first+i,cur.second+j);

                    if(d[dst.first][dst.second]==0 && c[dst.first][dst.second]==1 && valid(dst)){
                        Q.push(dst);
                        d[dst.first][dst.second]=1;
                    }
                }
            }
        }
    }
}

int main(){
    
    while(cin>>W>>H && W!=0){
        for(int i=1;i<=H;++i){
            for(int j=1;j<=W;++j){
                cin>>c[j][i];
            }
        }

        for(int i=1;i<=W;++i){
            for(int j=1;j<=H;++j){
                pair<int,int> start=make_pair(i,j);
                bfs(start);
            }
        }
        cout<<island<<"\n";
        island=0;
        Fill(c,0);
        Fill(d,0);
    }
}

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

template <class T>
void readVector(int n, vector<T> &v){
	for(int i=0; i<n; ++i){
		T t;
		cin >> t;
		v.push_back(t);
	}
}
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
typedef pair<long long, long long> P;

void solve(int w, int h){
	vector<int> v[h];
	for(int i=0; i<h; ++i)
		readVector(w, v[i]);
	int ans = 0;
	for(int i=0; i<h; ++i)
		for(int j=0; j<w; ++j){
			if(v[i][j] == 1){
				++ans;
				v[i][j] = 0;
				queue<P> q;
				q.push(P(i, j));
				while(!q.empty()){
					P p = q.front();
					q.pop();
					int nx = p.first;
					int ny = p.second;
					for(int k=0; k<8; ++k)
						if(0<=nx+dx[k] && nx+dx[k]<h && 0<=ny+dy[k] && ny+dy[k]<w && v[nx+dx[k]][ny+dy[k]] == 1){
							v[nx+dx[k]][ny+dy[k]] = 0;
							q.push(P(nx+dx[k], ny+dy[k]));
						}
				}
			}
		}
	cout << ans << "\n";
}

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	while(1){
		int w, h;
		cin >> w >> h;
		if(w == 0 && h == 0) return 0;
		solve(w, h);
	}
}