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 <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <bitset>
#include <math.h>

using namespace std;
////using Graph = vector<vector<int>>;
//using Graph = vector<vector<int>>;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef long long LL;

typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<string, int> P;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define REP(i,n)  for(int i=0; i<(n); i++)

//constant
//--------------------------------------------
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const LL MOD = 1e9+7; 
//vector <vector<int>> x(20, vector<int> (20)), y(20,vector<int> (20));
/*int counter_1 (int x) {
    if(x == 0) return 0;
    return counter_1(x >> 1) + (x & 1);
}*/

//int Counter(int x)
/*int factorial(int n) {
    if (n > 0) {
        return n * factorial(n - 1);
    } else {
        return 1;
    }
}*/
int factorial(int n) {
    if (n > 0) {
        return n * factorial(n - 1);
    } else {
        return 1;
    }
}
LL sum(LL a, LL b){
    LL cnt=0;
    cnt=(b-a+1)*(a+b)/2;
    return cnt;
}
/*LL yaku(int x){
    vector<pair<LL, LL> a=[];
    LL xh;
    if(x==1){
        pair<LL, LL> tmp;
        tmp=MP(1,1);
        return tmp;
    }
    else{
        FOR(i, 1, x/2+1){
            if(x%i==0){
                a.append(MP(i,x/i));
            }
        }
    }
    return a;

}*/
LL cul(int a, int b){
    LL f = pow(a,4)+pow(a,3)*b+pow(a,2)*pow(b,2)+a*pow(b,3)+pow(b,4);
    return f;
} 
//using Graph = vector<vector<int>>;
int H, W;
VVI field;
bool seen[510][510];
const int dx[3] = {1, 0, -1};
const int dy[3] = {0, 1, -1};
void dfs(int h, int w) {
    seen[h][w] = true; // v を訪問済にする



    // v から行ける各頂点 next_v について
    for(int dh=0; dh<3; ++dh){
        REP(dw, 3){
        int nh = h + dy[dh];
        int nw = w + dx[dw];
        if(nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
        if(field[nh][nw]==0) continue;
        if(seen[nh][nw]) continue; // next_v が探索済だったらスルー
        dfs(nh, nw); // 再帰的に探索
        }
    }
}
int main(){
    VI ANS;
    while(true){
        cin >> W >> H;
        if(H==0&&W==0) break;
        field.resize(H);
        REP(i,H){
            field[i].resize(W);
        }
        REP(i,H){
            REP(j, W){
                cin >> field[i][j];
                //cout << field[i][j] << ' ';
            }
            //cout << endl;
        }
        /*int sh, sw, gh, gw;
        REP(i,H){
            REP(j,W){
                if(field[i][j]=='s'){
                    sh=i;
                    sw=j;
                    //cout << sh << ' ' << sw << endl;
                }
                if(field[i][j]=='g'){
                    gh=i;
                    gw=j;
                    //cout << gh << ' ' << gw << endl;
                }
            }
        }*/
        memset(seen, false, sizeof(seen));
        /*REP(i, 510){
            REP(j,510){
                cout << seen[i][j] << ' ';
            }
            cout << endl;
        }*/
        //bool flg=false;
        int ans = 0;
        while(true){
            bool f=false;
            REP(i,H){
                REP(j,W){
                    if(field[i][j]==1&&seen[i][j]==false){
                        dfs(i,j);
                        f=true;
                        break;
                    }
                }
                if(f) break;
            }
            if(f) ans++;
            else break;
        }
        ANS.PB(ans);
        //cout << seen[gh][gw] << endl;
        /*if(seen[gh][gw]) cout << "Yes" << endl;
        else cout << "No" << endl;*/
        /*REP(i,H){
            REP(j,W){
                cout << field[i][j] << ' ';
            }
            cout << endl;
        }*/
    }
    int n = ANS.size();
    REP(i,n){
        cout << ANS[i] << endl;
    }

}

C++: #include<bits/stdc++.h>
using namespace std;
const int dx[8]={0,1,0,-1,1,1,-1,-1};
const int dy[8]={1,0,-1,0,1,-1,-1,1};
vector<vector<bool>> seen;
int H,W;
char info[50][50];
void GridDFS(const int y,const int x){
    seen[y][x]=1;
    for(int i=0;i<8;i++){
        int ny=y+dy[i],nx=x+dx[i];
        if(ny<0 or H<=ny or nx<0 or W<=nx)continue;
        if(seen[ny][nx])continue;
        if(info[ny][nx]=='1'){
            GridDFS(ny,nx);
        }
    }
}
void solve(int H,int W){
    seen=vector<vector<bool>>(H,vector<bool>(W));
    for(int i=0;i<H;i++)for(int j=0;j<W;j++)cin>>info[i][j];
    int ans=0;
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            if(seen[i][j])continue;
            if(info[i][j]=='1'){
                ans++;
                GridDFS(i,j);
            }
        }
    }
    printf("%d\n",ans);
}
int main(){
    while(true){
        scanf("%d%d",&W,&H);
        if(W+H==0)break;
        else solve(H,W);
    }
    return 0;
}