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>
using namespace std;
void kazoeruyo(int p,int P,int box[10][10]){
  box[p][P]=0;
    if(box[p][P+1]==1) {
      box[p][P+1]=0;
      P=P+1; 
      kazoeruyo(p,P,box);
    }
    if(box[p][P-1]==1){
      box[p][P-1]=0;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P]==1){ 
      box[p+1][P]=0;
      p=p+1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P]==1){
      box[p-1][P]=0;
      p=p-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P-1]==1){
      box[p-1][P-1]=0;
      p=p-1;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P+1]==1){
      box[p-1][P+1]=0;
      p=p-1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P+1]==1){
      box[p+1][P+1]=0;
      p=p+1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P-1]==1){
      box[p+1][P-1]=0;
      p=p+1;
      P=P-1;
      kazoeruyo(p,P,box);
    }	
}
int main(){
  int w,h,i,I,p,P,o,t,T,y,Y;
  int box[10][10];
  while(1){
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	cin>>box[i][I];
      }
    }
    o=0;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) {
	  p=i;
	  P=I;
	  kazoeruyo(p,P,box);
	  box[p][P]=1;
	}
      }
    }
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) o++;
      }
    }
    cout<<o<<endl;
    o=0;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
void b(int ,int );
int a[100][100]={};
 
int main(){
  while(1){
    int count=0,w,h;
    cin>>w>>h;
    if(w+h==0)break;
     
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++)
    cin>>a[i][j];
 
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++)
    if(a[i][j]==1){
      b(j,i);
      count++;
    }
    cout<<count<<endl;  
  }
  return 0;
}
 
  void b(int x,int y){
    a[y][x]=0;
    int x1,y1;
    int x2[8]={-1,0,1,1,1,0,-1,-1};
    int y2[8]={1,1,1,0,-1,-1,-1,0};
    for(int k=0;k<8;k++){
    x1=x+x2[k];
    y1=y+y2[k];
    if(a[y1][x1]==1)
      b(x1,y1);
    }
  }

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

struct UnionFind {
    vector<int> data;
    int sz;
    UnionFind(int sz) : data(sz, -1), sz(sz) { }
    bool unionSet(int x, int y) {
        if ((x = root(x)) == (y = root(y))) return false;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y]; data[y] = x; sz--;
        return true;
    }
    bool findSet(int x, int y) { return root(x) == root(y); }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    int size() { return sz; }
};

int X[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int Y[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int main(){
    while(true){
    int H, W;
    cin >> W >> H;
    if(H == 0 && W == 0)break;
    vector<vector<bool>> F(H, vector<bool>(W));
    UnionFind uf(H * W);
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            int a;cin >> a;
            if(a == 0)F[i][j] = false;
            else F[i][j] = true;
        }
    }
    
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            if(F[i][j]){
                for(int X = -1; X <= 1; X++){
                    for(int Y = -1; Y <= 1; Y++){
                    int x = j + X;
                    int y = i + Y;
                    if(x < 0 || W <= x || y < 0 || H <= y)continue;
                    else if(F[y][x])uf.unionSet(i * W + j, y * W + x);
                    }
                }
            }
        }
    }
    /*
    set<int> R;
    for(int i = 0; i < H * W; i++)R.insert(uf.root(i));
    //cout << uf.root(2) << endl;
    int ans = R.size();
    //cout << ans << endl;
    */
    int ans = uf.size();
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            if(F[i][j])continue;
            else ans--;
        }
    }
    
    cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>

using namespace std;

typedef long long ll;
typedef vector<int> VI;

#define FOR(i,n) for(int (i)=0;(i)<(n);(i)++)
#define FOR1(i,n) for(int (i)=1;(i)<(n);(i)++)
#define eFOR(i,n) for(int (i)=0;(i)<=(n);(i)++)
#define eFOR1(i,n) for(int (i)=1;(i)<=(n);(i)++)
#define SORT(i) sort((i).begin(),(i).end())
constexpr auto INF = 1000000000;
constexpr auto LLINF = 9223372036854775807;
constexpr auto mod = 1000000007;

int w, h, c[50][50];
int dx[8] = { -1,0,1,-1,1,-1,0,1 }, dy[8] = { -1,-1,-1,0,0,1,1,1 };
void search(int x, int y) {
	if (x >= 0 && x < w && y >= 0 && y < h) {
		if (c[y][x] == 1) {
			c[y][x] = 0;
			FOR(k, 8)search(x + dx[k], y + dy[k]);
		}
	}
}

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

	return 0;
}