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>
using namespace std;

int m[51][51];
bool v[51][51];
int h,w,c=0;
int dx[8]={-1,-1,0,1,1,1,0,-1};
int dy[8]={0,-1,-1,-1,0,1,1,1};
void dfs(int x,int y){
    if(v[y][x])return;
    for(int i=0;i<8;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx>=0&&nx<w&&ny>=0&&ny<h&&!v[y][x]&&m[ny][nx]){
            dfs(nx,ny);
        }
    }
}
int main(){
    cin>>h>>w;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>m[i][j];
            v[i][j]=0;
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(!v[i][j]&&m[i][j]){
                dfs(i,j);
                c++;
            }
        }
    }
    cout<<c<<endl;
}

C++: #include <iostream>
#include <string.h>

using namespace std;

int c[50][50];
bool visit[50][50];
const int dx[8] = {1,0,-1,0,1,1,-1,-1};
const int dy[8] = {0,1,0,-1,1,-1,1,-1};
int w,h;

void dfs(int x,int y)
{
	int tmp_x,tmp_y;
	for (int i=0;i<8;++i)
	{
		tmp_x = x + dx[i];
		tmp_y = y + dy[i];
		
		if (0<=tmp_x&&tmp_x<w&&0<=tmp_y&&tmp_y<h&&!visit[tmp_y][tmp_x]&&c[tmp_y][tmp_x] == 1)
		{
			visit[tmp_y][tmp_x] = true;
			dfs(tmp_x,tmp_y);
		}
	}
	return;
}

int main()
{

	int count;
	cin >> w >> h;
	
	while (w+h >0)
	{
		count = 0;
		memset(visit,0,sizeof(visit));
		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 (!visit[i][j]&&c[i][j]==1)
				{
					++count;
					dfs(j,i);
				}
			}
		}
		
		cout << count <<endl;
		
		cin >> w >> h;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> pl;
typedef vector<pl> vp;
#define fore(i,a,b) for(ll i=(a);i<=(b);++i)
#define rep(i,n) fore(i,0,(n)-1)
#define rfore(i,a,b) for(ll i=(b);i>=(a);--i)
#define rrep(i,n) rfore(i,0,(n)-1)
#define all(x) (x).begin(),(x).end()
const ll INF=1001001001;
const ll LINF=1001001001001001001;
const ll D4[]={0,1,0,-1,0};
const ll D8[]={0,1,1,0,-1,-1,1,-1,0};
template<class T>
bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>
bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
template<class T>
ll sum(const T& a){return accumulate(all(a),0LL);}

ll w,h;
ll c[55][55];
bool u[55][55];
void dfs(ll y,ll x){
    u[y][x]=1;
    rep(i,8){
        ll ny=y+D8[i];
        ll nx=x+D8[i+1];
        if(ny<0||h<=ny||nx<0||w<=nx||!c[ny][nx]||u[ny][nx])continue;
        dfs(ny,nx);
    }
}
void solve(){
    while(1){
        cin>>w>>h;
        if(w==0&&h==0)return;
        rep(i,h)rep(j,w){
            cin>>c[i][j];
            u[i][j]=0;
        }
        ll cnt=0;
        rep(i,h)rep(j,w){
            if(c[i][j]&&!u[i][j]){
                cnt++;
                dfs(i,j);
            }
        }
        cout<<cnt<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    solve();
}