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 rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for(int i = (s); i < (int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i = (int)(s - 1); i >= (int)(e); i--)
#define pb push_back
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-8;

template <typename T> T chmax(T &a, const T &b) { return a = (a > b ? a : b); }
template <typename T> T chmin(T &a, const T &b) { return a = (a < b ? a : b); }

struct UF { // O(loga(n))
    int n;
    int m;
    vi d, r;
    UF(int n) : n(n), m(n), d(n, -1), r(n, 0){};
    int root(int i) {
        if(d[i] < 0) return i;
        return d[i] = root(d[i]);
    }
    bool same(int x, int y) { return root(x) == root(y); }
    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        if(x == y) return false;

        if(r[x] < r[y])
            swap(x, y);
        else if(r[x] == r[y])
            r[x]++;
        d[x] += d[y];
        d[y] = x;
        --m;
        return true;
    }
    int size(int i) { return -d[root(i)]; }
    int size() { return m; }
};

int main() {
    int w, h;
    while(cin >> w >> h) {
        if(w == 0 && h == 0) break;
        vector<vi> d(h, vi(w));
        rep(i, h) rep(j, w) cin >> d[i][j];
        UF uf(h * w);
        rep(i, h) rep(j, w) {
            if(d[i][j]) REP(dy, -1, 2) REP(dx, -1, 2) {
                int x = j + dx, y = i + dy;
                if(min(x, y) < 0 || x >= w || y >= h || d[y][x] == 0)
                    continue;
                uf.unite(i * w + j, y * w + x);
            }
        }
        int ans = 0;
        rep(i, h) rep(j, w) if(d[i][j] && uf.root(i * w + j) == i * w + j) {
            ++ans;
            // cout << " " << i << " " << j << endl;
        }
        cout << ans << endl;
    }
    return 0;
}

C++: # include <iostream>

const int MAX_W = 50;
const int MAX_H = 50;

int w, h;

int c[MAX_H][MAX_W];

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

void dfs(int x, int y) {
	c[y][x] = 0;
	for (int i = 0; i < 8; ++i) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx<0 || ny<0 || nx>w || ny>h)
			continue;
		if (c[ny][nx])
			dfs(nx, ny);
	}
}

int solve() {
	int sum = 0;
	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			if (c[i][j] == 1) {
				dfs(j, i);
				++sum;
			}
		}
	}
	return sum;
}

int main() {
	while (1) {
		
		std::cin >> w >> h;
		if ((w + h) == 0)
			break;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				c[i][j] = 0;
		}
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				std::cin >> c[i][j];
		}
		std::cout << solve() << std::endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(),(v).end()

using namespace std;

using ll=long long;
using pii=pair<int, int>;
using vi=vector<int>;

const ll INF=1LL<<60;
const int MOD=1e9+7;

vector<vector<char>> ch;
vector<vector<bool>> visit;
int dx[8]={1, 0, -1, 0, 1, 1, -1, -1};
int dy[8]={0, 1, 0, -1, 1, -1, 1, -1};
int w, h;

void dfs(int x, int y){
    visit[x][y]=true;
    for(int i=0; i<8; i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<0 || nx>=h || ny<0 || ny>=w) continue;
        if(visit[nx][ny] != 0 || ch[nx][ny] == '0') continue;
        dfs(nx, ny);
    }
}

int main(){
    while(cin >> w >> h){
        if(w==0 && h==0) break;
        ch.assign(h, vector<char>(w));
        visit.assign(h, vector<bool>(w,0));
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cin >> ch[i][j];
            }
        }
        int res=0;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(ch[i][j] == '1' && visit[i][j] == 0){
                    dfs(i, j);
                    res++;
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}

C++: #include <iostream>
using namespace std;
int mas[50][50];
int dx[8] = {0,1,0,-1,1,-1,1,-1};
int dy[8] = {1,0,-1,0,-1,1,1,-1};
int w,h;
void dfs(int,int);

int main(){
    int cnt;
    while(1){
        cnt = 0;
        cin >> w >> h;
        if(w == 0 && h == 0)break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> mas[i][j];
            }
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(mas[i][j] == 1){
                    cnt++;
                    dfs(j,i);
                }
            }
        }
        cout << cnt << endl;
    }
}

void dfs(int x,int y){
    mas[y][x] = 0;
    for(int i=0;i<8;i++){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(nx >= 0 && nx < w && ny >= 0 && ny < h && mas[ny][nx] == 1){
            dfs(nx,ny);
        }
    }
}

C++: #include<cstring>
#define foreach(t,p) for(t::iterator it=p.begin();it!=p.end();p++)
#define REP(i,n) for(int i=0;i<n;i++)
using namespace std;
#define MAX_W 50

bool island[MAX_W+3][MAX_W+3];

int solve(int x,int y)
{
	if(!island[x][y]) {return 0;}

	island[x][y]= false;
	solve(x-1,y-1);
	solve(x-1,y);
	solve(x-1,y+1);
	solve(x,y-1);
	solve(x,y+1);
	solve(x+1,y+1);
	solve(x+1,y);
	solve(x+1,y-1);
//	cout << x << " " << y << endl;
	return 1;
}


int main()
{
	int w,h;
	while(cin >> w >> h &&w && h)
	{
		REP(i,MAX_W+2)REP(j,MAX_W+2) island[i][j]=false;
		REP(i,h)REP(j,w){cin >> island[i+1][j+1];}
		int kosu = 0;
		REP(i,h)REP(j,w){kosu += solve(i+1,j+1);}
		cout << kosu << endl;
		
	}
	return 0;
}