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>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define rep(i,n) REP(i, 0, n)
bool fld[60][60];
int W, H;
void dfs(int x, int y){
    fld[y][x] = false;
    REP(i, -1, 2) REP(j, -1, 2){
        int nx = x + i, ny = y + j;
        if(nx < 0 || ny < 0 || W <= nx || H <= ny) continue;
        if(!fld[ny][nx]) continue;
        dfs(nx, ny);
    }
}
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t;
    while(cin >> W >> H && W){
        memset(fld, 0, sizeof(fld));
        rep(i, H) rep(j, W) cin >> fld[i][j];
        int cnt = 0;
        rep(i, H) rep(j, W) if(fld[i][j]){
            dfs(j, i);
            cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}

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

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		if (w == 0&&h == 0) {
			break;
		}
		ans.push_back(res);


	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}

C++: #include <iostream>
using namespace std;
void kazoeruyo(int p,int P,int box[50][50]){
  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[50][50];
  while(1){
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	cin>>box[i][I];
      }
    }
    o=0;
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	if(box[i][I]==1) {
	  p=i;
	  P=I;
	  kazoeruyo(p,P,box);
	  box[p][P]=1;
	}
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	if(box[i][I]==1) o++;
      }
    }
    cout<<o<<endl;
    o=0;
  }
  return 0;
}