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;

/*bool check(const int x, const int y, const int LandNum, const int aNodeCol[50][50], const int aXMax, const int aYMax){
	if ((x<0 || x >= aXMax) || (y<0 || y >= aYMax) || (aNodeCol[x][y] != 1)){
		return false;
	}
	return true;
}*/

bool LandConfirmedAndMawariCheck(const int x, const int y, const int LandNum, int aNodeCol[50][50], const int aXMax, const int aYMax){
	if (aNodeCol[x][y] == 1){
		aNodeCol[x][y] = LandNum;
		/*if (check(x - 1, y, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x - 1, y, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x - 1, y - 1, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x - 1, y - 1, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x - 1, y + 1, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x - 1, y + 1, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x + 1, y - 1, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x + 1, y - 1, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x + 1, y, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x + 1, y, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x + 1, y + 1, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x + 1, y + 1, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x, y - 1, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x, y - 1, LandNum, aNodeCol, aXMax, aYMax);
		if (check(x, y + 1, LandNum, aNodeCol, aXMax, aYMax))LandConfirmedAndMawariCheck(x, y + 1, LandNum, aNodeCol, aXMax, aYMax);*/
		if (!((x - 1<0 || x - 1 >= aXMax) || (y    <0 || y     >= aYMax) || (aNodeCol[x-1][y    ] != 1)))LandConfirmedAndMawariCheck(x - 1, y    , LandNum, aNodeCol, aXMax, aYMax);
		if (!((x - 1<0 || x - 1 >= aXMax) || (y - 1<0 || y - 1 >= aYMax) || (aNodeCol[x-1][y - 1] != 1)))LandConfirmedAndMawariCheck(x - 1, y - 1, LandNum, aNodeCol, aXMax, aYMax);
		if (!((x - 1 < 0 || x - 1 >= aXMax) || (y + 1 < 0 || y + 1 >= aYMax) || (aNodeCol[x - 1][y + 1] != 1))){
			LandConfirmedAndMawariCheck(x - 1, y + 1, LandNum, aNodeCol, aXMax, aYMax);
		}
		if (!((x + 1<0 || x + 1 >= aXMax) || (y - 1<0 || y - 1 >= aYMax) || (aNodeCol[x+1][y - 1] != 1)))LandConfirmedAndMawariCheck(x + 1, y - 1, LandNum, aNodeCol, aXMax, aYMax);
		if (!((x + 1<0 || x + 1 >= aXMax) || (y    <0 || y     >= aYMax) || (aNodeCol[x+1][y    ] != 1)))LandConfirmedAndMawariCheck(x + 1, y    , LandNum, aNodeCol, aXMax, aYMax);
		if (!((x + 1<0 || x + 1 >= aXMax) || (y + 1<0 || y + 1 >= aYMax) || (aNodeCol[x+1][y + 1] != 1)))LandConfirmedAndMawariCheck(x + 1, y + 1, LandNum, aNodeCol, aXMax, aYMax);
		if (!((x    <0 || x     >= aXMax) || (y - 1<0 || y - 1 >= aYMax) || (aNodeCol[x  ][y - 1] != 1)))LandConfirmedAndMawariCheck(x    , y - 1, LandNum, aNodeCol, aXMax, aYMax);
		if (!((x    <0 || x     >= aXMax) || (y + 1<0 || y + 1 >= aYMax) || (aNodeCol[x  ][y + 1] != 1)))LandConfirmedAndMawariCheck(x    , y + 1, LandNum, aNodeCol, aXMax, aYMax);
		return true;
	}
	return false;
}



int main(void){
	int NodeCol[50][50];
	int a, b, c;
	int XMax, YMax;
	while(1){
		cin >> a >> b;
		if (a == 0&&b == 0)break;
		XMax = a; YMax = b;

		for (int Lone = 0;  Lone < YMax; ++Lone){
			for (int Ltwo = 0; Ltwo < XMax; ++Ltwo){
				cin >> c;
				NodeCol[Ltwo][Lone] = c;
			}
		}
		int t = 2;
		for (int Lone = 0;  Lone < XMax; ++Lone){
			for (int Ltwo= 0; Ltwo < YMax; ++Ltwo){
				if (NodeCol[Lone][Ltwo]==1)
				{
					if (LandConfirmedAndMawariCheck(Lone, Ltwo, t, NodeCol, XMax, YMax))t++;
				}
			}
		}
		cout << t-2<<endl;
	}
	return 0;
}

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

using namespace std;

int w, h;
vector< vector<int> > c, island;

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

void dfs(int x, int y, int id) {

    island[y][x] = id;

    for(int i = 0; i < 8; i++) {

        int nx = x + dx[i];
        int ny = y + dy[i];

        if(nx < 0 || nx >= w || ny < 0 || ny >= h)
            continue;

        if(c[ny][nx] == 1 && island[ny][nx] != id)
            dfs(nx, ny, id);

    }

}

int main(){

    cin.tie(0);
    ios::sync_with_stdio(false);

    int ans;

    while(cin >> w >> h, w) {

        c.assign(h, vector<int>(w));
        island.assign(h, vector<int>(w, -1));
        ans = 0;

        for(int y = 0; y < h; y++)
            for(int x = 0; x < w; x++)
                cin >> c[y][x];

        for(int x = 0; x < w; x++)
            for(int y = 0; y < h; y++)
                if(c[y][x] == 1 && island[y][x] == -1)
                    dfs(x, y, ans++);

        cout << ans << endl;

    }

    return 0;

}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<stack>
#include<queue>
#include<string>
#include<set>
#include<map>
using namespace std;
#define rep(i,n) for(int i=0;(i)<(n);(i)++)
#define FOR(i,m,n) for(int i=(m);(i)<(n);(i)++)
typedef long long ll;
const int dx[8] = {0,1,1,1,0,-1,-1,-1};
const int dy[8] = {1,1,0,-1,-1,-1,0,1};
int w,h,res;

void dfs(int x,int y,int f,vector<vector<int> >& mp){
    if(mp[y][x]==1){
        if(f==0){
            f=1;
            res++;
        }
        mp[y][x]=0;
        rep(i,8){
            int nx=x+dx[i],ny=y+dy[i];
            if(nx>=0&&nx<w&&ny>=0&&ny<h){
                dfs(nx,ny,f,mp);
            }
        }
    }

}

int main(){
    while(cin >> w >> h,w|h){
        vector<vector<int> > c(h,vector<int>(w));
        
        rep(i,h)rep(j,w)cin >> c[i][j];
        res=0;
        rep(i,h)rep(j,w)dfs(j,i,0,c);
        cout << res << endl;
    }   
    return 0;
}

C++: #include<stdio.h>
void printmap();
void draw(int x,int y);
int w,h,c[50][50];

int main(){
	int i,j;
	int count;


	scanf("%d %d",&w,&h);


	while((w!=0)&&(h!=0)){
		count=0;
	
		for(j=0;j<h;j++){
			for(i=0;i<w;i++){
				scanf("%d",&c[j][i]);
			}
		}
		//printf("\n");
		
		for(i=0;i<w;i++){
			c[h][i]=0;
		}
		for(i=0;i<h;i++){
			c[i][w]=0;
		}

		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				if(c[j][i]==1){
					count++;
					//printf("count up count=%d\n",count);
					draw(j,i);
					//printmap();
				}
			}
		}

		printf("%d\n",count);
		scanf("%d %d",&w,&h);
	}
	return 0;
}

void draw(int x,int y){
	//printf("delete %d %d\n",x,y);
	int i,j;
	c[x][y]=0;
	for(i=0;i<3;i++){
		for(j=0;j<3;j++){
			if(c[x-1+i][y-1+j]==1){
				draw(x-1+i,y-1+j);
			}
		}
	}


}

void printmap(){
	int i,j;
	for(j=0;j<h;j++){
		for(i=0;i<w;i++){
			printf("%d ",c[j][i]);
		}
		printf("\n");
	}
}