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 <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;

struct UnionFind {
        int n;
        vector<int> parent;
        vector<int> rank;
        vector<int> num;
        int find(int x) {
                if (parent[x] == x) return  x;
                return parent[x] = find(parent[x]);
        }
        UnionFind(int n_) {
                n = n_;
                parent.resize(n);
                for (int i = 0; i < n; i ++) parent[i] = i;
                rank.assign(n, 0);
                num.assign(n, 1);
        }
        void unite(int x, int y) {
                if ((x = find(x)) != (y = find(y))) {
                        if (rank[x] < rank[y]) {
                                parent[x] = y;
                                num[y] += num[x];
                        } else {
                                parent[y] = x;
                                if (rank[x] == rank[y]) rank[x] ++;
                                num[x] += num[y];
                        }
                        n --;
                }
        }
        bool same(int x, int y) { return find(x) == find(y); }
        int get() { return n; }
        int get(int x) { return num[find(x)]; }
};

int main() {
        int w, h;
        while (scanf("%d%d", &w, &h), w) {
                vector<vector<int>> s(h, vector<int> (w));
                int lands = 0;
                map<pair<int, int>, int> ptol;
                for (int i = 0; i < h; i ++) {
                        for (int j = 0; j < w; j ++){
                                scanf("%d", &s[i][j]);
                                if (s[i][j] == 1) {
                                        ptol[make_pair(i, j)] = lands;
                                        lands ++;
                                }
                        }
                }
                UnionFind uf(lands);
                //cerr << lands << endl;
                for (int i = 0; i < h - 1; i ++) {
                        for (int j = 0; j < w - 1; j ++) {
                                if (s[i][j] == 1) {
                                        if (s[i + 1][j] == 1) uf.unite(ptol[make_pair(i, j)], ptol[make_pair(i + 1, j)]);
                                        if (s[i + 1][j + 1] == 1) uf.unite(ptol[make_pair(i, j)], ptol[make_pair(i + 1, j + 1)]);
                                        if (s[i][j + 1] == 1) uf.unite(ptol[make_pair(i, j)], ptol[make_pair(i, j + 1)]);
                                }
                        }
                }
                for (int i = 0; i < h - 1; i ++) {
                        if (s[i][w - 1] == 1 && s[i + 1][w - 1] == 1) uf.unite(ptol[make_pair(i, w - 1)], ptol[make_pair(i + 1, w - 1)]);
                        if (w > 1 && s[i][w - 1] == 1 && s[i + 1][w - 2] == 1) uf.unite(ptol[make_pair(i, w - 1)], ptol[make_pair(i + 1, w - 2)]);
                }
                for (int j = 0; j < w - 1; j ++) {
                        if (s[h - 1][j] == 1 && s[h - 1][j + 1] == 1) uf.unite(ptol[make_pair(h - 1, j)], ptol[make_pair(h - 1, j + 1)]);
                        if (h > 1 && s[h - 1][j] == 1 && s[h - 2][j + 1] == 1) uf.unite(ptol[make_pair(h - 1, j)], ptol[make_pair(h - 2, j + 1)]);
                }
                printf("%d\n", uf.get());
        }
        return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<eps)
int w,h;
int f[55][55];
int dx[8]={0,0,1,1,1,-1,-1,-1};
int dy[8]={1,-1,1,0,-1,1,-1,0};
void dfs(int x,int y)
{
	f[x][y]=0;
	for(int i=0;i<8;i++)
	{
		int nx=x+dx[i],ny=y+dy[i];
		if(f[nx][ny]==1)dfs(nx,ny);
	}
}
void print()
{
	for(int i=1;i<=h;i++)
	{
		for(int j=1;j<=w;j++)
		{
			cout << f[i][j] << ' ';
		}
		cout << endl;
	}
}
int main()
{
	while(1)
	{
		int ans=0;
		memset(f,0,sizeof(f));
		cin >> w >> h;
		if(w==0&&h==0)break;
		for(int i=1;i<=h;i++)
		{
			for(int j=1;j<=w;j++)
			{
				cin >> f[i][j];
			}
		}
		for(int i=1;i<=h;i++)
		{
			for(int j=1;j<=w;j++)
			{
				if(f[i][j]==1)
				{
					ans++;
					dfs(i,j);
					//print();
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;


vector<int> dx{-1,0,1};
vector<int> dy{-1,0,1};

vector<vector<int>> G;
int w,h;

void dfs(vector<vector<int>> &G, int x, int y){
    if (G[y][x] == 1){
        G[y][x] = 0;

        REP(i,3){
            REP(j,3){
                int next_x = x+dx[i];
                int next_y = y+dy[j];
                if (i==1 && j==1)continue;
                if (0<=next_x && next_x<w && 0<=next_y && next_y<h){
                    dfs(G, next_x, next_y);
                }
            }
        }
    }
}

int main() {
    while(1){
        
        cin >> w >> h;
        if(w==0 && h==0) break;
        G.assign(h, vector<int>(w, 0));
        REP(i,h){
            REP(j,w){
                cin >> G[i][j];
            }
        }

        int count = 0;
        REP(i,h){
            REP(j,w){
                if (G[i][j] == 1){
                    count ++;
                    dfs(G, j, i);//G,w,h
                
                }
            }
        }
        cout << count << endl;
    }
    return 0;
}