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++: #ifndef _WIN32
#include<iostream>
#endif

using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>

#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

typedef vector<int> V;
typedef vector<V> VV;

VV wrap(VV vs,int c) {
	int R = vs.size();
	int C = vs[0].size();
	VV ret(R + 2, V(C + 2, c));
	REP(i, R)REP(j, C)ret[i + 1][j + 1] = vs[i][j];
	return ret;
}
const int dr[] = { 0,0,-1,1 ,1,1,-1,-1};
const int dc[] = { -1,1,0,0, -1,1,-1,1};
VV g;
void dfs(int r,int c) {
	if (g[r][c] == 0)return;
	//cout << "(" << r << "," << c << ")";
	g[r][c] = 0;
	REP(i, 8) {
		int nr = r + dr[i];
		int nc = c + dc[i];
		dfs(nr, nc);
	}
}
int main() {
	int R, C;
	while (cin >> C >> R, C + R) {
		VV in(R,V(C));
		REP(i, R)REP(j,C)cin >> in[i][j];
		g = wrap(in,0);
		int res = 0;
		REP(i, R)REP(j, C) {
			if (g[i + 1][j + 1]) {
				res++;
				dfs(i + 1, j + 1);// cout << endl;
			}
		}

		cout << res << endl;
	}


}

C++: #define rep(i, n) for(int i = 0; i < n; i++)
#define ck(a, b) (0<=a && a<b)
using namespace std;
int dx[]={-1,0,1,-1,1,-1,0,1}, dy[]={-1,-1,-1,0,0,1,1,1};
int w, h; int il[50][50];

int main(){
	while(cin>>w>>h, w||h){
		int c = 0;
		rep(y, h)rep(x, w)cin>>il[y][x];
		rep(y, h)rep(x, w){
			if(il[y][x]){
				int cked[50][50] = {0};
				cked[y][x] = 1;
				bool finish = 0;
				while(!finish){
					finish = 1;
					rep(i, h)rep(j, w){
						if(cked[i][j]){
							rep(d, 8){
								if(ck(j+dx[d],w) && ck(i+dy[d],h) &&
									il[i+dy[d]][j+dx[d]] &&
									!cked[i+dy[d]][j+dx[d]]
								){
									cked[i+dy[d]][j+dx[d]] = 1; finish = 0;
								}
							}
						}
					}
				}
				c++;
				rep(i, h)rep(j, w)if(cked[i][j])il[i][j]=0;
			}
		}
		cout<<c<<endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;

int map[50][50];
int w,h,label;

void check(int x,int y,int lnum){
  map[y][x]=lnum;
  if (x>0){
    if (map[y][x-1]==1) check(x-1,y,lnum);
    if (y>0){
      if (map[y-1][x-1]==1) check(x-1,y-1,lnum);
    }
    if (y<h-1){
      if (map[y+1][x-1]==1) check(x-1,y+1,lnum);
    }
  }
  if(x<w-1){
    if (map[y][x+1]==1) check(x+1,y,lnum);
    if (y>0){
      if (map[y-1][x+1]==1) check(x+1,y-1,lnum);
    }
    if (y<h-1){
      if (map[y+1][x+1]==1) check(x+1,y+1,lnum);
    }
  }
  if (y>0){
    if (map[y-1][x]==1) check(x,y-1,lnum);
  }
  if (y<h-1){
    if (map[y+1][x]==1) check(x,y+1,lnum);
  }
}

int main(){
  cin >> w >> h;
  while (w!=0||h!=0){
    label=2;
    for (int i=0;i<h;i++)
      for (int j=0;j<w;j++)
	cin >> map[i][j];

    for (int i=0;i<h;i++){
      for (int j=0;j<w;j++){
	if (map[i][j]==1){
	  //cout << "(" << map[i][j] << ")";
	  check(j,i,label++);
	}
      }
    }
    //cout << "(" << label << ")";

    cout << (label-2) << endl;

    cin >> w >> h;
 }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

#define REP(i,n) for(int i=0;i<(n);i++)

using namespace std;
int field[50][50];
int w, h;

void dfs(int i, int j){
    field[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(field[ni][nj]==1 && 0 <= ni && ni < h &&
                    0 <= nj && nj < w){
                dfs(ni,nj);
            }
        }
    }
    return;
}

int main(){
    while(cin >> w >> h){
        if(w==0 && h==0) break;
        REP(i,h){
            REP(j,w){
                cin >> field[i][j];
            }
        }

        int cnt = 0;
        REP(i,h){
            REP(j,w){
                if(field[i][j] == 1){
                    dfs(i,j);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

C++: # include <iostream>

const int MAX_W = 50;
const int MAX_H = 50;

int w, h;

int c[MAX_H][MAX_W];

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

void dfs(int x, int y) {
	c[y][x] = 0;
	for (int i = 0; i < 8; ++i) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx<0 || ny<0 || nx>w || ny>h)
			continue;
		if (c[ny][nx])
			dfs(nx, ny);
	}
}

int solve() {
	int sum = 0;
	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			if (c[i][j] == 1) {
				dfs(j, i);
				++sum;
			}
		}
	}
	return sum;
}

int main() {
	while (1) {
		std::cin >> w >> h;
		if ((w + h) == 0)
			break;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				std::cin >> c[i][j];
		}
		std::cout << solve() << std::endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int w,h;
int c[55][55];
int dx[8]={1,1,1,-1,-1,-1,0,0};
int dy[8]={1,-1,0,1,-1,0,1,-1};
void dfs(int i,int j){
  c[j][i]=0;
  for(int k=0;k<8;k++){
    int nx = i + dx[k], ny = j + dy[k];
    if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
    if(c[ny][nx]==1) dfs(nx,ny);
  }
}
int main(){
  while(1){
    cin>>w>>h;
    if(w==0 && h==0) break;
    int cnt=0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>c[i][j];
      }
    }
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(c[i][j]==1){
	  cnt++;
	  dfs(j,i);
	}
      }
    }

    cout<<cnt<<endl;
  }

  return 0;

    
      
}

C++: #include <cstdio>
#include <queue>
#include <map>
using namespace std;
typedef pair<int,int> P;

int w, h;
int c[50][50];
queue<P> que;

void dfs(){
	P p = que.front(); que.pop();
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			if(c[p.first+i-1][p.second+j-1]){
				c[p.first+i-1][p.second+j-1] = false;
				que.push(P(p.first+i-1,p.second+j-1));
				dfs();
			}
		}
	}
}

int main(){
	int w, h;
	while(scanf("%d%d", &w, &h),w||h){
		int ans = 0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				scanf("%d", &c[j][i]);
			}
		}
		for(int i = 0; i < w; i++){
			for(int j = 0; j < h; j++){
				if(c[i][j]){
					c[i][j] = false;
					ans++;
					que.push(P(i,j));
					dfs();
				}
			}
		}
		printf("%d\n", ans);
	}
}

C++: #include <iostream>
using namespace std;

int W,H;
int map[51][51];

void fillmap(int i,int j,int s){
     if(map[i][j]!=0)return;
     map[i][j]=s;
     for(int x=i-1;x<i+2;x++){
	  for(int y=j-1;y<j+2;y++){
	       if(!(y>=W||y<0||x>=H||x<0)) fillmap(x,y,s);
	  }
     }
}
    
int main(){
     while(cin>>W>>H,W||H){
	  for(int i=0;i<H;i++){
	       for(int j=0;j<W;j++){
		    int temp;
		    cin>>temp;
		    if(temp)map[i][j]=0;
		    else map[i][j]=-1;
	       }
	  }
	  int s=0;
	  for(int i=0;i<H;i++){
	       for(int j=0;j<W;j++){
		    if(map[i][j]==0){
			 fillmap(i,j,++s);
		    }
	       }
	  }
	  cout<<s<<endl;
     }
}