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<iostream>
#include<stack>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

class Node{
public:
	int x,y;
	int node;
	Node(int p,int q){
		x=p;
		y=q;
	}
};

int main(){
	int w,h;
	while(1){
		cin>>w>>h;
		if(w==0 && h==0)break;
		stack<Node> st;
		int dt[h][w];
		bool flg[h][w];
		rep(i,h){
			rep(j,w){
				cin>>dt[i][j];
				flg[i][j]=false;
			}
		}
		int ho[8]={1,1,0,-1,-1,-1,0,1};
		int ve[8]={0,1,1,1,0,-1,-1,-1};
		int ans=0;
		rep(i,h){
			rep(j,w){
				if(!flg[i][j] &&  dt[i][j]==1){
					st.push(Node(j,i));
					while(!st.empty()){
						Node now=st.top();
						flg[now.y][now.x]=true;
						st.pop();
						rep(k,8){
							int nx=now.x+ho[k];
							int ny=now.y+ve[k];
							if(nx>=0 && ny>=0 && nx<w && ny<h && flg[ny][nx]==false){
								if(dt[ny][nx]==dt[now.y][now.x]){
									Node next(nx,ny);
									st.push(next);
								}
							}
						}
					}
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;


int H, W;
int f[100][100];

void dfs(int y, int x){
    f[y][x] = 0;

    for(int i = -1; i <= 1; ++i){
        for(int j = -1; j <= 1; ++j){
            if(y+i<0 || x+j < 0 || y+i>=H || x+j>=W) continue;
            if(f[y+i][x+j]) dfs(y+i, x+j);
        }
    }
}

int main(){
    while(cin >> W >> H){
        if(W==0||H==0) break;
        rep(i,H) rep(j,W) cin >> f[i][j];
        int cnt = 0;
        rep(i,H)rep(j,W){
            if(f[i][j])cnt++, dfs(i,j);
        }
        cout << cnt << endl;
    }
    
}

C++: #include <iostream>
using namespace std;
int w, h;
int c[64][64];
int d[] = {1, -1, 0};

void rec(int x, int y) {
  if(x < 0 || x >= w || y < 0 || y >= h) return;
  if(!c[y][x]) return;

  c[y][x] = 0;
  for(int i = 0;i < 3; i++) {
    for(int j = 0;j < 3; j++) {
      rec(x+d[i], y+d[j]);
    }
  }
  return;
}

int main(void) {
  while(cin >> w >> h, w+h) {
    for(int i = 0;i < h; i++)
      for(int j = 0;j < w; j++)
        cin >> c[i][j];

    int ans = 0;
    for(int i = 0;i < h; i++) {
      for(int j = 0;j < w; j++) {
        if(c[i][j]) {
          ans++;
          rec(j, i);
        }
      }
    }

    cout << ans << endl;
  }
}

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

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
int field[51][51];
int h, w;

void sol(int x, int y)
{
  field[x][y] = 0;
  for (int i = 0; i < 8; i++){
    if ((0 <= x + dx[i] && x + dx[i] < h) || (0 <= y + dy[i] && y + dy[i] < w))
    if (field[x + dx[i]][y + dy[i]] == 1) sol(x + dx[i], y + dy[i]);
  }
}

int main()
{

  while (cin >> w >> h, h || w){
    int res = 0;
    for (int i = 0; i < h; i++){
      for (int j = 0; j < w; j++){
        cin >> field[i][j];
      }
    }

    for (int i = 0; i < h; i++){
      for (int j = 0; j < w; j++){
        if (field[i][j] == 1){
          res++;
          sol(i, j);
        }
      }
    }
    cout << res << endl;
  }
}