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++: //
// Created by elegant-belltree on 15/06/03.
//

#include <iostream>
#include <set>

using namespace std;

int main(){
    int w,h;
    int c[50][50];

    while (cin>>w>>h && (w>0 || h>0)) {
        int label=2;
        set<int> labels;

        for (int n = 0; n < 2; ++n) {
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    if(n==0) {
                        cin >> c[j][i];
                    }
                    if (0<c[j][i]) {
                        for (int k = -1; k < n+1 && i+k<h; ++k) {
                            for (int l = -1; (n!=0 || k!=0 || l!=0) && l < 3 && j + l < w; ++l) {
                                if (-1 < i + k && -1 < j + l && 0 < c[j + l][i + k] && (c[j][i] == 1 || c[j + l][i + k] < c[j][i])) {
                                    if(n==1){
                                        labels.erase(c[j][i]);
                                    }
                                    c[j][i] = c[j + l][i + k];
                                }
                            }
                        }
                        if (c[j][i] == 1) {
                            c[j][i] = label++;
                            labels.insert(c[j][i]);
                        }
                    }
                }
            }
        }
        /*for (int m = 0; m < h; ++m) {
            for (int i = 0; i < w; ++i) {
                cout<<c[i][m]<<" ";
            }
            cout<<endl;
        }*/
        cout<<labels.size()<<endl;
    }
    return 0;
}

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

void update(int x,int y,vector< vector<int> >& map){
	if(map[y][x]==1){
		map[y][x]=2;
	}else{
		return;
	}
	if(x!=0)update(x-1,y,map);
	if(y!=0)update(x,y-1,map);
	if(x!=0&&y!=0)update(x-1,y-1,map);
	if(y!=map.size()-1)update(x,y+1,map);
	if(x!=map[0].size()-1)update(x+1,y,map);
	if(y!=map.size()-1&&x!=0)update(x-1,y+1,map);
	if(y!=map.size()-1&&x!=map[0].size()-1)update(x+1,y+1,map);
	if(x!=map[0].size()-1&&y!=0)update(x+1,y-1,map);
}
int main(){
	while(true){
		vector< vector<int> > map;
		int w,h,tmp,sum=0;
		cin>>w>>h;
		if(w==0)break;
		map.resize(h);
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				map[y].resize(w);
				cin>>tmp;
				map[y][x]=tmp;
			}
		}
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				if(map[y][x]==1){
					sum++;
					update(x,y,map);
				}
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}

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

typedef pair<int,int> P;

const int dx[] = { 0, 1, 0,-1, 1, 1,-1,-1};
const int dy[] = { 1, 0,-1, 0, 1,-1, 1,-1};
int h, w;
vector<vector<int> > islands(50, vector<int>(50));

void bfs (int i, int j) {
    queue<P> q;
    q.push(P(i,j));
    while (q.size()) {
        const P p = q.front();
        q.pop();
        for (int i = 0; i < 8; i++) {
            int x = p.first + dx[i], y = p.second + dy[i];
            if (0 <= x && x < w && 0 <= y && y < h && islands[x][y]) {
                islands[x][y] = 0;
                q.push(P(x,y));
            }
        }
    }
}

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

        int count = 0;
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                if (islands[i][j] == 1) {
                    count++;
                    islands[i][j] = 0;
                    bfs(i, j);
                }
            }
        }
        cout << count << endl;
    }


    return 0;
}

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

struct pos {
    int x, y;
};

int main() {

    while (true) {
        int W, H;
        cin >> W >> H;
        if (W*H == 0) break;

        bool C[W][H] = {};
        rep(y, H) rep(x, W) cin >> C[x][y];

        int cnt = 0;
        stack<pos> s;
        rep(y, H) rep(x, W) {
            if (C[x][y]) {
                cnt++;
                s.push({x, y});
            }
            while (!s.empty()) {
                pos p = s.top();
                s.pop();
                C[p.x][p.y] = false;
                for (int dx=-1; dx<=1; dx++) {
                    for (int dy=-1; dy<=1; dy++) {
                        int nx = p.x+dx, ny = p.y+dy;
                        if ((nx<0) || (nx>=W) || (ny<0) || (ny>=H)) continue;
                        if (C[nx][ny]) {
                            s.push({nx, ny});
                            // cout << p.x << p.y << " " << nx << ny << endl;
                        }
                    }
                }
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(a) (a).begin(),(a).end()
 
using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
 
const ll MOD  = 1e9+7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

int mv[3] = {1, 0, -1};

int h, w;
int atlas[50][50];

void dfs(int y, int x){

    rep(i, 3)rep(j, 3){
        int dx = x + mv[i];
        int dy = y + mv[j];

        if(dx < 0 || dx >= w || dy < 0 || dy >= h)continue;

        if(atlas[dy][dx] == 1){
            atlas[dy][dx] = 0;
            dfs(dy, dx);
        }
    }
}

int main(){
    vector<int> ans;
    while(true){
        cin >> w >> h;

        if(h + w == 0)break;

        rep(i, h)rep(j, w){
            cin >> atlas[i][j];
        }

        int res = 0;
        rep(i, h)rep(j, w){
            if(atlas[i][j] == 1){
                atlas[i][j] = 0;
                res++;

                dfs(i, j);
            }
        }

        ans.push_back(res);
    }

    rep(i, ans.size()){
        cout << ans[i] << endl;
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

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

int w, h;
int c[55][55];

void dfs(int i, int j) {
    if(c[i][j] == 0) return;
    
    c[i][j] = 0;
    dfs(i-1, j-1);
    dfs(i-1, j);
    dfs(i-1, j+1);
    dfs(i  , j-1);
    dfs(i  , j+1);
    dfs(i+1, j-1);
    dfs(i+1, j);
    dfs(i+1, j+1);
}

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