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 < (int)(n); i++)
using namespace std;

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

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

vector<vector<bool>> seen;
void dfs(int h, int w) {
  seen[h][w] = true;

  for (int dir = 0; dir < 8; dir++) {
    int nh = h + dx[dir];
    int nw = w + dy[dir];

    if (nh < 0 || nh >= H || nw < 0 || nw >= W) {
      continue;
    } else if (islands.at(nh).at(nw) == 0 || seen.at(nh).at(nw)) {
      continue;
    } else {
      dfs(nh, nw);
    }
  }
}

int main() {
  while (true) {
    cin >> W >> H;
    if (H == 0 && W == 0) break;
    islands.assign(H, vector<int>(W, 0));
    rep(h, H) {
      rep(w, W) { cin >> islands.at(h).at(w); }
    }

    int count = 0;
    seen.assign(H, vector<bool>(W, false));
    rep(h, H) {
      rep(w, W) {
        if (!seen.at(h).at(w) && islands.at(h).at(w) == 1) {
          dfs(h, w);
          count++;
        }
      }
    }

    cout << count << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
const int INF = 1e9;

int h, w;
vector<vector<int>> mas;
void dfs(int y, int x) {
    mas[y][x] = 0;

    for (int dy = -1; dy <= 1; ++dy) {
        for (int dx = -1; dx <= 1; ++dx) {
            int ny = y + dy;
            int nx = x + dx;

            if (ny < 0 || ny >= h || nx < 0 || nx >= w || mas[ny][nx] == 0)
                continue;

            dfs(ny, nx);
        }
    }
}

int main() {
    while (cin >> w >> h) {
        if (h == 0 || w == 0)
            break;
        mas.assign(h, vector<int>(w, 0));
        rep(i, h) rep(j, w) cin >> mas[i][j];

        int ans = 0;
        rep(i, h) {
            rep(j, w) {
                if (mas[i][j]) {
                    dfs(i, j);
                    ++ans;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#define f(i,n)for(int i=0;i<n;i++)
#define s (scanf("%d",&e),e)
int main(){
	int a=0,w,n,m,b,e,d[99];
	f(i,99){
		d[i]=0;
	}
	n=s;
	m=s;
	if(n==0)exit(0);
	f(i,m){
		w=0;
		b=0;
		f(j,n){
			s;
			if(e){
				f(k,3){
					e=d[j+k];
					if(e&&e-w){
						if(w){
							f(l,n+2){
								d[l]=d[l]==e?w:d[l];
							}
							a--;
						}
						else{
							w=e;
						}
					}
				}
				if(w==0){
					a++;
					w=i*n+j+1;
				}
			}
			else{
				w=0;
			}
			d[j]=b;
			b=w;
		}
		d[n]=b;
		w=0;
	}
	printf("%d\n",a);
	main();
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <utility>

using namespace std;

void dfs(int x,int y,vector<vector<bool> >&map){
    if(!map[x][y])return;
    map[x][y]=false;
    for(int i=-1;i<2;i++)
    for(int j=-1;j<2;j++){
        if(i==0 && j==0)continue;
        int nx=x+i;
        int ny=y+j;
        dfs(nx,ny,map);
    }
    return;
}

int main(){
    while(true){
        int w,h;
        cin >> w >> h;
        if(w==0)break;
        vector<vector<bool> > map(h+2,vector<bool>(w+2,false));
        for(int i=1;i<=h;i++)
        for(int j=1;j<=w;j++){
            int t;
            cin >> t;
            if(t==1)map[i][j]=true;
        }

        int ans=0;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                if(map[i][j]){
                    ans++;
/*                    for(int k=1;k<=w;k++){
                    for(int l=1;l<=h;l++){
                        if(map[l][k])cout<<'o';
                        else cout << '_';
                    }
                    cout << endl;
                    }
                    cout << endl;*/
                    dfs(i,j,map);
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
using namespace std;
int w,h;
vector<int> nx;
vector<int> ny;
bool check(int **field){
  nx.clear();
  ny.clear();
  for (int i=0;i<h;i++){
    for (int j=0;j<w;j++){
      if (field[i][j]==1){
	nx.push_back(i);
	ny.push_back(j);
	return true;
      }
    }
  }
  return false;
}
void find(int **field){
  int x[nx.size()];
  int y[ny.size()];
  int s=nx.size();
  for (int i=0;i<s;i++){
    field[nx[i]][ny[i]]=0;
    x[i]=nx[i];
    y[i]=ny[i];
  }
  nx.clear();
  ny.clear();
  for (int i=0;i<s;i++){
    if (field[x[i]+1][y[i]]==1&&x[i]+1<h){
      nx.push_back(x[i]+1);
      ny.push_back(y[i]);
    }
    if (field[x[i]-1][y[i]]==1&&x[i]-1>=0){
      nx.push_back(x[i]-1);
      ny.push_back(y[i]);
    }
    if (field[x[i]][y[i]+1]==1&&y[i]+1<w){
      nx.push_back(x[i]);
      ny.push_back(y[i]+1);
    }
    if (field[x[i]][y[i]-1]==1&&y[i]-1>=0){
      nx.push_back(x[i]);
      ny.push_back(y[i]-1);
    }
    if (field[x[i]+1][y[i]+1]==1&&x[i]+1<h&&y[i]+1<w){
      nx.push_back(x[i]+1);
      ny.push_back(y[i]+1);
    }
    if (field[x[i]+1][y[i]-1]==1&&x[i]+1<h&&y[i]-1>=0){
      nx.push_back(x[i]+1);
      ny.push_back(y[i]-1);
    }
    if (field[x[i]-1][y[i]+1]==1&&x[i]-1>=0&&y[i]+1<w){
      nx.push_back(x[i]-1);
      ny.push_back(y[i]+1);
    }
    if (field[x[i]-1][y[i]-1]==1&&x[i]-1>=0&&y[i]-1>=0){
      nx.push_back(x[i]-1);
      ny.push_back(y[i]-1);
    }
  }
  if (nx.empty()){
    return;
  }
  int *f[h];
  for (int i=0;i<h;i++)
    f[i]=field[i];
  return find(f);
}
int main(){
  while (cin>>w>>h,w|h!=0){
    int count=0;
    int field[h][w];
    for (int i=0;i<h;i++){
      for (int j=0;j<w;j++){
	cin>>field[i][j];
      }
    }
    int *f[h];
    for (int i=0;i<h;i++) 
      f[i]=field[i];
    while (check(f)){
      find(f);
      count++;
      }
    cout<<count<<endl;
  }
}