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>

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

void dfs(std::vector<std::vector<bool>> &seen, std::vector<std::vector<int>> &map, int x, int y){
//    std::cout << x << ", " << y << std::endl;
    seen.at(y).at(x) = true;
    for(int i = 0; i < 8; ++i){
        int nextX = x + dx.at(i), nextY = y + dy.at(i);
        if(!seen.at(nextY).at(nextX) && 1 == map.at(nextY).at(nextX)){
            dfs(seen, map, nextX, nextY);
        }
    }
}

int main(){
    int w, h;
    while(std::cin >> w >> h){
        if(0 == w && 0 == h){
            break;
        }

        std::vector<std::vector<int>> map(h + 2, std::vector<int>(w + 2, 0));
        for(int i = 1; i <= h; ++i){
            for(int j = 1; j <= w; ++j){
                std::cin >> map.at(i).at(j); 
            }
        }

        std::vector<std::vector<bool>> seen(h + 2, std::vector<bool>(w + 2, false));
        int count = 0;
        for(int i = 1; i <= h; ++i){
            for(int j = 1; j <= w; ++j){
                if(!seen.at(i).at(j) && 1 == map.at(i).at(j)){
                    dfs(seen, map, j, i);
                    ++count;
                } 
            }
        }
    
        std::cout << count << std::endl;
    }

    return 0;
}

C++: 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#define INF 100000
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;
typedef long long int ll;

#define MAX 5000

int G[51][51];
int h,w;


void dfs(int x, int y){
    if(x<0||x>w||y<0||y>h) return;
    for (int i=-1; i<=1; i++) {
        for (int j=-1; j<=1; j++) {
            if (G[x+i][y+j]==1) {
                G[x+i][y+j]=0;
                dfs(x+i,y+j);
            }
        }
    }
}


int main(){
    for(;;){
        cin >> w >> h;
        if(w==0 and h == 0) break;
        int ans=0;
        for (int i=0; i<h; i++) {
            for (int j=0; j<w; j++) {
                G[i][j]=0;
            }
        }
        for (int i = 0; i<h;i++) {
            for (int j=0; j < w; j++) {
                cin >> G[i][j];
            }
        }
        for (int i=0; i<h; i++) {
            for (int j=0; j<w; j++) {
                if (G[i][j]==1) {
                    G[i][j]=0;
                    ans++;
                    dfs(i,j);
                }
            }
        }
        cout << ans <<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <climits>
//#include "h.h"
using namespace std;

#define ll long long
#define pint pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define pm(first, second) pb(mp(first, second))
#define SPACE " "
#define fpf first.first
#define fps first.second
#define spf second.first
#define sps second.second

#define all(X) (X).begin(), (X).end()

//parent[i]:ノード番号iの親のノード番号
//親が存在しないときは-(属するグループのサイズ)を格納
//parent = vector<int>(N, -1);で初期化
vector<int> parent;
//ノードXの根を返す
int root(int X){
	if(parent[X] < 0)return X;
	return parent[X] = root(parent[X]);
}
//ノードXが属するグループのサイズ(属するノード数)を返す
int size(int X){
	return -parent[root(X)];
}
//Xの属するグループとYの属するグループを繋ぐ
//それぞれの根どうしを繋ぐ
bool connect(int X, int Y){
	//根同士をつなぐため指すノードを根にする
	X = root(X);
	Y = root(Y);
	//すでに繋がっているときは繋がずにfalseを返す
	if(X == Y){
		return false;
	}
	//大きいほうに小さいほうを繋ぎたいので逆ならスワップ
	if(size(X) < size(Y))swap(X, Y);
	//繋がれる方のサイズを更新
	parent[X] += parent[Y];
	//繋ぐ方の親を設定
	parent[Y] = X;
	return true;
}
int main(){

	while(true){
		int W, H;
		cin >> W >> H;
		if(W == 0 && H == 0)break;
		vector<vector<int>> C(H, vector<int>(W));
		int landNum = 0;
		for(int i = 0; i < H; i ++){
			for(int j = 0; j < W; j ++){
				cin >> C[i][j];
				if(C[i][j] == 1)landNum ++;
			}
		}
		parent = vector<int>(H * W, -1);
		for(int i = 0; i < H; i ++){
			for(int j = 0; j < W; j ++){
				if(C[i][j] == 1){
					if(i + 1 < H && C[i + 1][j] == 1)connect((i + 1) * W + j, i * W + j);
					if(i - 1 >= 0 && C[i - 1][j] == 1)connect((i - 1) * W + j, i * W + j);
					if(j + 1 < W && C[i][j + 1] == 1)connect(i * W + (j + 1), i * W + j);
					if(j - 1 >= 0 && C[i][j - 1] == 1)connect(i * W + (j - 1), i * W + j);
					if(i + 1 < H && j + 1 < W && C[i + 1][j + 1] == 1)connect((i + 1) * W + (j + 1), i * W + j);
					if(i + 1 < H && j - 1 >= 0 && C[i + 1][j - 1] == 1)connect((i + 1) * W + (j - 1), i * W + j);
					if(i - 1 >= 0 && j + 1 < W && C[i - 1][j + 1] == 1)connect((i - 1) * W + (j + 1), i * W + j);
					if(i - 1 >= 0 && j - 1 >= 0 && C[i - 1][j - 1] == 1)connect((i - 1) * W + (j - 1), i * W + j);
				}
			}
		}
		map<int, int> m;
		for(int i = 0; i < H; i ++){
			for(int j = 0; j < W; j ++){
				if(C[i][j] == 1)m[root(i * W + j)] ++;
			}
		}
		int ans = 0;
		for(auto itr = m.begin(); itr != m.end(); itr ++){
			if(itr -> second > 0)ans ++;
		}
		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstdio>
#include<queue>
#include<deque>
#include<map>
#include<stack>
#include<set>

using namespace std;

int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
const int MAXN=100000;

int w,h;
vector<vector<int> > c(50,vector<int>(50,0));

void dfs(int y,int x){
    c[y][x]=0;
    for(int dy=-1;dy<=1;dy++){
        for(int dx=-1;dx<=1;dx++){
            int ny=y+dy,nx=x+dx;
            if(ny>=0&&ny<h&&nx>=0&&nx<w&&c[ny][nx]==1){
                dfs(ny,nx);
            }
        }
    }
    return ;
}

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