Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

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;
  }
}