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, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int w, h;
vector<vector<int>> g, d;

void dfs(int y, int x) {
	d[y][x] = 1;
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			int dy = y + i;
			int dx = x + j;
			if (dy < 0 || dy >= h || dx < 0 || dx >= w) continue;
			if (g[dy][dx] == 0 || d[dy][dx] != 0) continue;
			dfs(dy, dx);
		}
	}
}

int main() {
	while (cin >> w >> h) {
		if (w == 0 && h == 0) break;
		g.assign(h, vector<int>(w));
		rep(i, h) rep(j, w) cin >> g[i][j];
		d.assign(h, vector<int>(w));
		int ans = 0;
		rep(i, h) rep(j, w) {
			if (g[i][j] == 0 || d[i][j] != 0) continue;
			dfs(i, j);
			ans++;
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <utility>
#include <numeric>
#include <stack>
#include <queue>
using namespace std;
#define ll long long
#define INF 1000000007
int h,w;
int m[50][50];
void dfs(int k,int n){
    m[k][n]=0;
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            if(i==0&&j==0)continue;
            int ni=k+i;
            int nj=n+j;
            if(0<=ni&&ni<h&&0<=nj&&nj<w&&m[ni][nj]==1)dfs(ni,nj);
        }
    }
    return;
}
int count(){
    int re=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(m[i][j]==1){
                dfs(i,j);
                re++;
            }
        }
    }
    return re;
}
int main(){
    while(1){
        cin>>w>>h;
        if(h==0&&w==0)break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>m[i][j];
            }
        }
        cout<<count()<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int w,h,mp[50][50],
	dy[8]={-1,-1,0,1,1,1,0,-1},
	dx[8]={0,1,1,1,0,-1,-1,-1};
void func(int x,int y){
	mp[y][x]=0;
	for(int i=0;i<8;i++){
		int xx=x+dx[i],yy=y+dy[i];
		if(xx>=0&&xx<w&&yy>=0&&yy<h&&mp[yy][xx]) func(xx,yy);
	}
}
int main(void){
	while(1){
		cin>>w>>h;
		if(w==0&&h==0)break;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++) cin>>mp[i][j];
		}
		int cnt=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(mp[i][j]==1){
					cnt++;
					func(j,i);
				}
			}
		}
		cout<<cnt<<endl;
	}
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,s,e) for (int i=s;i<(e);i++)
#define All(v) (v).begin(),(v).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define int long long
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18 * 5;

struct UnionFind
{
    vector< int > data;
    
    UnionFind(int sz){
        data.assign(sz, -1);
    }
    
    bool unite(int x, int y){
        x = root(x), y = root(y);
        if(x == y) return false;
        if(data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return true;
    }
    
    int root(int k){
        if(data[k] < 0) return(k);
        return (data[k] = root(data[k]));
    }
    
    int size(int k){
        return -data[root(k)];
    }
};

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

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

    int w, h;
    cin >> w >> h;
    while(!(w==0 && h==0)){
        vector< vector<int> > c(h+2, vector<int>(w+2));
        REP(i, h) c[i][0] = 0;
        REP(i, h) c[i][w+1] = 0;
        REP(j, w) c[0][j] = 0;
        REP(j, w) c[h+1][j] = 0;
        FOR(i, 1, h+1)FOR(j, 1, w+1) cin >> c[i][j];

        int num = 10, unite = 0;
        UnionFind UF(500);
        FOR(i, 1, h+1)FOR(j, 1, w+1){
            int tmp = 0;
            if(c[i][j] == 0) continue;
            if(c[i][j] == 1) c[i][j] = ++num;
            for(auto y: dy)for(auto x: dx){
                if(x==1 && y==1) continue;
                if(c[i+y][j+x] == 0) continue;
                if(c[i+y][j+x] == 1){
                    c[i+y][j+x] = c[i][j];
                    tmp++;
                }else if(UF.root(c[i+y][j+x]) != UF.root(c[i][j])){
                    unite++;
                    UF.unite(c[i+y][j+x], c[i][j]);
                }
            }
        }

        //FOR(i, 1, h+1)FOR(j, 1, w+1) cout << c[i][j] << (j==w ? "\n":" ");

        cout << num - 10 - unite << "\n";
        cin >> w >> h;
    }
    return 0;
}

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

int wm,hm;
vector< vector<int> > wh(50, vector<int>(50,0));

int xp[]={-1,0,1,-1,1,-1,0,1};
int yp[]={-1,-1,-1,0,0,1,1,1};

void dfs(int x,int y){
  for(int i=0;i<8;i++){
    int nx=x+xp[i];
    int ny=y+yp[i];
    if(nx>-1&&nx<wm&&ny>-1&&ny<hm&&wh[nx][ny]==1){
      //cout<<nx<<" "<<ny<<" "<<wh[nx][ny]<<endl;
      wh[nx][ny]=0;
      dfs(nx,ny);
    }
  }
}

int main(){
  while(1){
    //int wm,hm;
    int aa,bb;
    cin>>aa>>bb;
    wm=bb;
    hm=aa;
    if(wm==0&&hm==0)break;
    //vector< vector<int> > wh(wm, vector<int>(hm));
    for(int i=0;i<wm;i++){
      for(int j=0;j<hm;j++){
        int a;
        cin>>a;
        wh[i][j]=a;
      }
    }
    int ans=0;
    for(int i=0;i<hm;i++){
      for(int j=0;j<wm;j++){
        if(wh[i][j]==1){
          //cout<<"*"<<i<<" "<<j<<endl;
          wh[i][j]=0;
          dfs(i,j);
          ans++;
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}