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 w, h, cnt;
int c[51][51];
void dfs(int i, int j){
    if(i-1 >= 0 && j-1 >= 0){
        if(c[i-1][j-1] == 1){
            c[i-1][j-1] = 0;
            dfs(i-1,j-1);
        }
    }
    if(i-1 >= 0){
        if(c[i-1][j] == 1){
            c[i-1][j] = 0;
            dfs(i-1,j);
        }
    }
    if(i-1 >= 0 && j+1 < w){
        if(c[i-1][j+1] == 1){
            c[i-1][j+1] = 0;
            dfs(i-1,j+1);
        }
    }
    if(j-1 >= 0){
        if(c[i][j-1] == 1){
            c[i][j-1] = 0;
            dfs(i,j-1);
        }
    }
    if(j+1 < w){
        if(c[i][j+1] == 1){
            c[i][j+1] = 0;
            dfs(i,j+1);
        }
    }
    if(i+1 < h && j-1 >= 0){
        if(c[i+1][j-1] == 1){
            c[i+1][j-1] = 0;
            dfs(i+1,j-1);
        }
    }
    if(i+1 < h){
        if(c[i+1][j] == 1){
            c[i+1][j] = 0;
            dfs(i+1,j);
        }
    }
    if(i+1 < h && j+1 < w){
        if(c[i+1][j+1] == 1){
            c[i+1][j+1] = 0;
            dfs(i+1,j+1);
        }
    }

}

int main(void){
    while(1){
        cin >> w >> h;
        if(w == 0 && h == 0)    break;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> c[i][j];
            }
        }

        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;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int w,h;
	cin >> w;
	cin >> h;

	while (w!=0||h!=0) {
		int count = 0;
		std::stack<int> st_x,st_y;
		std::stack<int> st_xn,st_yn;
		int xn,yn;
		int c;
		bool all = false;
		std::vector< std::vector<bool> > island(w+2, std::vector<bool>(h+2));
		for (int i = 0; i < h+2; i++) {
			for (int j = 0; j < w+2; j++) {
				if (i==0||i==h+1||j==0||j==w+1) {
					island[i][j] = false;
				} else {
					cin >> c;
					if (c==1) {
						island[i][j] = true;
						st_x.push(j);
						st_y.push(i);
					} else {
						island[i][j] = false;
					}
				}
			}
		}

		while (!st_x.empty()) {
			xn = st_x.top();
			yn = st_y.top();
			st_x.pop();
			st_y.pop();
			if (island[xn][yn]) {
				island[xn][yn] = false;
				count++;
				st_xn.push(xn);
				st_yn.push(yn);

				while (!st_xn.empty()) {
					xn = st_xn.top();
					yn = st_yn.top();
					st_xn.pop();
					st_yn.pop();
					if (island[xn+1][yn]) {
						st_xn.push(xn+1);
						st_yn.push(yn);
						island[xn+1][yn] = false;
					}
					if (island[xn-1][yn]) {
						st_xn.push(xn-1);
						st_yn.push(yn);
						island[xn-1][yn] = false;
					}
					if (island[xn+1][yn+1]) {
						st_xn.push(xn+1);
						st_yn.push(yn+1);
						island[xn+1][yn+1] = false;
					}
					if (island[xn+1][yn-1]) {
						st_xn.push(xn+1);
						st_yn.push(yn-1);
						island[xn+1][yn-1] = false;
					}
					if (island[xn-1][yn+1]) {
						st_xn.push(xn-1);
						st_yn.push(yn+1);
						island[xn-1][yn+1] = false;
					}
					if (island[xn-1][yn-1]) {
						st_xn.push(xn-1);
						st_yn.push(yn-1);
						island[xn-1][yn-1] = false;
					}
					if (island[xn][yn+1]) {
						st_xn.push(xn);
						st_yn.push(yn+1);
						island[xn][yn+1] = false;
					}
					if (island[xn][yn-1]) {
						st_xn.push(xn);
						st_yn.push(yn-1);
						island[xn][yn-1] = false;
					}
				}
			}
		}
		cout << count << endl;

		cin >> w;
		cin >> h;
	}
		return 0;
}