Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

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

using namespace std;

int main(void) {
	int w,h;
	while(cin >> w >> h && w && h) {
		vector< vector<int> > data(w+2,vector<int>(h+2));
		vector< vector<bool> > check(w+2,vector<bool>(h+2,false));
		queue<int> sx,sy;
		for (int i = 0; i < h + 2; ++i) {
			for (int j = 0; j < w + 2; ++j) {
				if (i == 0 || i == h+1 || j == 0 || j == w+1) {
					data[i][j] = 0;
				}
				else {
					cin >> data[i][j];
					if (data[i][j] == 1) {
						sx.push(j);
						sy.push(i);
					}
				}
			}
		}

		int nx,ny;
		int count = 0;
		queue<int> fx,fy;
		while(!sx.empty()) {
			nx = sx.front();
			ny = sy.front();
			sx.pop();
			sy.pop();
			if (!check[ny][nx]) {
				check[ny][nx] = true;
				fx.push(nx);
				fy.push(ny);
				++count;
			}
			while (!fx.empty()) {
				nx = fx.front();
				ny = fy.front();
				fx.pop();
				fy.pop();
				//cout << ny << " " << nx << endl;
				if (!check[ny+1][nx] && data[ny+1][nx] == 1) {
					check[ny+1][nx] = true;
					fx.push(nx);
					fy.push(ny+1);
				}
				if (!check[ny-1][nx] && data[ny-1][nx] == 1) {
					check[ny-1][nx] = true;
					fx.push(nx);
					fy.push(ny-1);
				}
				if (!check[ny+1][nx+1] && data[ny+1][nx+1] == 1) {
					check[ny+1][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny+1);
				}
				if (!check[ny+1][nx-1] && data[ny+1][nx-1] == 1) {
					check[ny+1][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny+1);
				}
				if (!check[ny-1][nx-1] && data[ny-1][nx-1] == 1) {
					check[ny-1][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny-1);
				}
				if (!check[ny-1][nx+1] && data[ny-1][nx+1] == 1) {
					check[ny-1][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny-1);
				}
				if (!check[ny][nx+1] && data[ny][nx+1] == 1) {
					check[ny][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny);
				}
				if (!check[ny][nx-1] && data[ny][nx-1] == 1) {
					check[ny][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

int w, h;
char c[51][51];
bool f[51][51];
int ans = 0;


bool contain(int y, int x){
    return (0 <= y && y < h && 0 <= x && x < w);
}

void dfs(int y, int x){
    f[y][x] = true;
    if(c[y][x] == '0') return;
    rep(i, 0, 3){
        rep(j, 0, 3){
            int ny = y + i - 1;
            int nx = x + j - 1;
            if(contain(ny, nx) && f[ny][nx] == false){
                dfs(ny, nx);
            }
        }
    }
    return;
}

signed main(){
    while(cin >> w >> h){
        if(w + h == 0) break;
        rep(i, 0, h){
            rep(j, 0, w){
                cin >> c[i][j];
            }
        }
        rep(i, 0 ,51){
            rep(j, 0, 51){
                f[i][j] = false;
            }
        }
        // cout << -1 << endl;
        ans = 0;
        rep(i, 0, h){
            rep(j, 0 ,w){
                if(c[i][j] == '1' && f[i][j] == false){
                    // cout << i << ' ' << j << endl;
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }

}

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

using namespace std;
using ll = long long;

int dir[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, -1, -1, 0, 1, 1}};

void dfs(int i, int j, vector<vector<int>>& used, vector<vector<int>>& color){
  used[i][j] = 1;
  for(int d = 0; d < 8; d++){
    if(i + dir[0][d] >= 0 && i + dir[0][d] < used.size() && j + dir[1][d] >= 0 && j + dir[1][d] < used[0].size()){
      if(!used[i+dir[0][d]][j+dir[1][d]] && color[i+dir[0][d]][j+dir[1][d]]){
        dfs(i + dir[0][d], j + dir[1][d], used, color);
      }
    }
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  int w, h;
  while(cin >> w >> h && w){
    vector<vector<int> > used(h, vector<int>(w, 0));
    vector<vector<int> > color(h, vector<int>(w, 0));
    int ans = 0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        cin >> color[i][j];
      }
    }
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(!used[i][j] && color[i][j]) ans++, dfs(i, j, used, color);
      }
    }
    cout << ans << endl;
  }

  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}

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

int w, h, ans, tmp;
vector<vector<int>> c(60, vector<int>(60,0));

void ume(int x, int y){
	if(c[x][y]==0) return;
	c[x][y]=0;
	ume(x-1,y);
	ume(x+1,y);
	ume(x-1,y-1);
	ume(x,y-1);
	ume(x+1,y-1);
	ume(x-1,y+1);
	ume(x,y+1);
	ume(x+1,y+1);
	return;
}

bool hantei(int x, int y){
	if(c[x][y]==1){
		ume(x,y);
		return 1;
	}
	else return 0;
}

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

C++: #include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
int a[55][55]={};
int w,h;
int count=0;
void dfs(int y,int x){
  a[y][x]=0;
  int dy[8]={-1,-1,-1,0,0,1,1,1};
  int dx[8]={-1,0,1,-1,1,-1,0,1};
  for(int i=1;i<8;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if(nx<0||ny<0||nx>=w||ny>=h)continue;
    if(a[ny][nx]==0)continue;
    dfs(ny,nx);
  }
}
int main(){
  while(1){
    int ans=0;
    cin>>w>>h;
    if(w==0&&h==0)break;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>a[i][j];
      }
    }
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(a[i][j]==0)continue;
	ans++;
	dfs(i,j);
      }
    }
    cout<<ans<<endl;
  }
}