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;

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(int i = 0; i < (n); i++)
#define Rep(i, n) for(int i = 1; i < n; i++)

const int mod = 1000000007;
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vii = vector<pair<int, int>>;

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; }

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return (a * b) / GCD(a, b); }
int round_int(int a, int b) { return (a + (b - 1)) / b; }

// 素因数分解; 返り値は(素数, 乗数)のvector配列
vector<pair<ll, int>> factorize(ll n) {
	vector<pair<ll, int>> res;
	for (ll i = 2; i * i <= n; ++i) {
		if (n % i) continue;
		res.emplace_back(i, 0);
		while (n % i == 0) {
			n /= i;
			res.back().second++;
		}
	}
	if (n != 1) res.emplace_back(n, 1);
	return res;
}

constexpr array<int, 9> dx = { 0, 1, 0, -1, -1, 1, 1, -1, 0 };
constexpr array<int, 9> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0 };
constexpr auto MAX = 100000;

// ──────────────────────────────────────────────────────────────────

// UnionFind 解説：https://youtu.be/TdR816rqc3s?t=8424

int w, h, t[50][50], cnt = 0;
bool check[50][50];

void dfs(ii pos) {
	int y = pos.first;
	int x = pos.second;

	rep(i, 8) {
		int nx = x + dx[i], ny = y + dy[i];

		if (0 <= nx && nx < w &&
			0 <= ny && ny < h &&
			check[ny][nx] == false &&
			t[ny][nx] == 1) {
			check[ny][nx] = true;
			dfs({ ny,nx });
		}
	}
}

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

		if (!w && !h) break;

		cnt = 0;
		rep(i, 50) rep(j, 50) check[i][j] = false;
		rep(i, h) rep(j, w) cin >> t[i][j];

		rep(i, h) {
			rep(j, w) {
				if (t[i][j] == 1 && check[i][j] == false) {
					check[i][j] = true;
					dfs({ i, j });
					cnt++;
				}
			}
		}
		ans.push_back(cnt);
	}
	rep(i, ans.size()) cout << ans[i] << endl;
}

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


int H,W;
vector<vector<int>> field;

void dfs(int h, int w){
	field[h][w] = 0;
	for(int dh=-1;dh<=1;dh++){
		for(int dw=-1;dw<=1;dw++){
			int nh=h+dh, nw=w+dw;
			if(nh<0||nw<0||H-1<nh||W-1<nw) continue;
			if(field[nh][nw] == 0) continue;
			
			dfs(nh, nw);
		}
	}	
}

int main()
{
	while(1){
		cin>>W>>H;
		if(W==0&&H==0) break;
		field.assign(H, vector<int>(W, 0));
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>field[i][j];
			}
		}
		
		int cnt = 0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(field[i][j] == 0) continue;
				dfs(i, j);
				cnt++;
			}
		}
		cout<<cnt<<endl;
	}
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define MAX 50

int c[MAX][MAX];

void dfs(int w, int h, int x, int y);
int main(int argc, char const *argv[]){
    int w, h, ans;

    while (cin>>w>>h, w || h){
        ans = 0;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                cin>>c[i][j];
            }
        }
        for (int i = 0; i < w; i++){
            for (int j = 0; j < h; j++){
                if (c[i][j] == 1){
                    dfs(w, h, i, j);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

void dfs(int w, int h, int x, int y){
    int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
    int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
    c[x][y] = 0;

    for (int i = 0; i < 8; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];

        if (0 <= nx && nx < w && 0 <= ny && ny < h && c[nx][ny] == 1){
            dfs(w, h, nx, ny);
        }
    }
    return;
}

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

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

int H, W;
vector<vector<int> > field;

void dfs(int h, int w) {
    field[h][w] = 0;
    for(int i = -1; i <= 1; i++) {
        for(int j = -1; j <= 1; j++) {
            int nh = h+i;
            int nw = w+j;
            
            if(nh < 0 || nw < 0 || nh >= H || nw >= W) continue;
            if(field[nh][nw] == 0) continue;
            dfs(nh, nw);
        }
    }
}

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

    while(cin >> W >> H) {
        if(H == 0 && W == 0) break;
        field.assign(H, vector<int> (W, 0));
        rep(i, H) rep(j, W) cin >> field[i][j];
        int ans = 0;
        rep(i, H) rep(j, W) {
            if(field[i][j] == 0) continue;
            dfs(i, j);
            ans++;
        }

        cout << ans << '\n';
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define loop(i, a, n) for(int i = (a); i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

constexpr int LEN = 55;
bool m[LEN][LEN];

int main(){
	while(true){
		int w, h; cin >> w >> h;
		if(!w && !h) break;
		rep(i, h)rep(j, w) cin >> m[i][j];

		int ans = 0;
		rep(i, h)rep(j, w){
			if(!m[i][j]) continue;
			ans++;
			queue<P> q;
			q.push(make_pair(i, j));
			while(q.size()){
				P now = q.front(); q.pop();
				loop(di, -1, 2)loop(dj, -1, 2){
					int ni = now.first + di, nj = now.second + dj;
					if(0 <= ni && ni < h && 0 <= nj && nj < w && m[ni][nj]){
						m[ni][nj] = false;
						q.push(make_pair(ni, nj));
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}