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>

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

using namespace std;

int main(){

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

  int c[h+2][w+2];memset(c,0,sizeof(c));

  for(int i=1;i<=h;i++){
    for(int j=1;j<=w;j++){
      cin>>c[i][j];
    }
  }

  queue<pair<int,int> > q;

  bool a[h+2][w+2];

  for(int i=0;i<=h+1;i++){
    for(int j=0;j<=w+1;j++){
      a[i][j]=false;
    }
  }

  int ans=0;

  for(int i=1;i<=h;i++){
    for(int j=1;j<=w;j++){
      if(c[i][j]==1&&a[i][j]==false){
	ans++;
	q.push(make_pair(i,j));
	while(!q.empty()){
	  pair<int,int> tmp=q.front();q.pop();
	  int x=tmp.first;
	  int y=tmp.second;

	  for(int i=0;i<8;i++){
	    int checkx=dx[i]+x;
	    int checky=dy[i]+y;
	    if(a[checkx][checky]==false&&c[checkx][checky]==1){
	      a[checkx][checky]=true;
	      q.push(make_pair(checkx,checky));
	    }
	  }
	  
	}
      }
    }
  }

  cout<<ans<<endl;

  }
  
  return 0;
}

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

using namespace std;
typedef long long ll;
typedef long int li;
typedef long double lb;
#define rep(i,j,n) for (ll i = j; i < (n); i++)
#define repr(i,j,n) for(ll i = j; i >= (n); i--)
#define all(x) (x).begin(),(x).end()
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<b)
#define out(ans) cout << ans << endl
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef pair<ll,ll>P;
const ll mod=1e9+7;
const ll INF = 1LL<<60;


ll w,h;
const ll dx[]={-1,0,1,-1,1,-1,0,1},dy[]={-1,-1,-1,0,0,1,1,1};

// void dfs(ll height,ll width,ll map[height][width],ll check[height][width]){

// }

int main(){

  while(cin>>w>>h){
    if(w==0&&h==0)break;

    ll map[h][w];
    ll check[h][w];
    rep(i,0,h){
      rep(j,0,w){
        cin>>map[i][j];
        check[i][j]=-1;
      }
    }
    ll num=0;
    rep(i,0,h){
      rep(j,0,w){
        if(map[i][j]==1&&check[i][j]==-1){
          num++;
          stack<P>s;
          s.push(P(i,j));

          while(!s.empty()){
            P p=s.top();s.pop();
            ll y=p.first,x=p.second;

            check[y][x]=num;
            rep(i,0,8){
              ll ny=y+dy[i],nx=x+dx[i];
              if(IN(0,h,ny)&&IN(0,w,nx)&&map[ny][nx]==1&&check[ny][nx]==-1){
                s.push(P(ny,nx));
              }
            }
          }
        }
      }
    }

    out(num);

  }


  return 0;
}

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

void add_label(map<int,int> &tbl, int a, int b){
	if( a == b ) return;
	if(a < b){
		int tmp = a;
		a = b;
		b = tmp;
	}
	if( tbl.find(a) == tbl.end() ) {
		tbl[a] = b;
	} else {
		int bb = tbl.find(a)->second;
		if( b == bb ) return;
		if( bb > b ) {
			tbl[a] = b;
			tbl[bb] = b;
		} else {
			tbl[b] = bb;
		}
	}
}

int main(void){
	int w, h;
	while( cin>>w>>h, w||h ) {
		vector<vector<int> > field( h );
		vector<vector<int> > labeled( h );
		for( int i=0; i<h; ++i ) {
			field[i] = vector<int>( w );
			labeled[i] = vector<int>( w );
			for( int j=0; j<w; ++j ) {
				cin>>field[i][j];
				labeled[i][j] = 0;
			}
		}
		int label = 0;
		map<int, int> tbl;
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				if( field[i][j] ){
					bool f = false;
					if( i ) {
						if( j && labeled[i-1][j-1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j-1];
							f = true;
						}
						if( labeled[i-1][j] ) {
							if(!f) labeled[i][j] = labeled[i-1][j];
							else add_label( tbl, labeled[i][j], labeled[i-1][j] );
							f = true;
						}
						if( j + 1 < w && labeled[i-1][j+1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j+1];
							else add_label( tbl, labeled[i][j], labeled[i-1][j+1] );
							f = true;
						}
						if( j && labeled[i][j-1] ) {
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j], labeled[i][j-1] );
							f = true;
						}
					} else {
						if( j && labeled[i][j-1] ){
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j], labeled[i][j-1] );
							f = true;
						}
					}
					if(!f) labeled[i][j] = ++label;
				}
			}
		}
		for( auto x : tbl ) {
			int a = x.first;
			int b = x.second;
			while( tbl.find(b) != tbl.end() ) {
				b = tbl.find(b)->second;
			}
			for( int i=0; i<h; ++i ) {
				for( int j=0; j<w; ++j ) {
					if( labeled[i][j] == a ) labeled[i][j] = b;
				}
			}
		}
		
		/*
		for( int i=0; i<h; ++i ) {
			cerr<<"!";
			for( int j=0; j<w; ++j ) {
				cerr<<labeled[i][j];
			}
			cerr<<endl;
		}*/
		
		set<int> nums;
		nums.insert(0);
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				nums.insert( labeled[i][j] );
			}
		}
		cout<<nums.size()-1<<endl;
	}
	return 0;
}