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 h , w , count ;
int dx[] = { -1,0,1,-1,1,-1,0,1 } ;
int dy[] = { -1,-1,-1,0,0,1,1,1 } ;
int map[51][51] = {} ;

void destroy( int ny , int nx ){
	map[ny][nx] = 0 ;
	
	for( int k=0 ; k<8 ; k++ ){
		if( ny+dy[k] >= 0 && ny+dy[k] < h &&
			nx+dx[k] >= 0 && nx+dx[k] < w &&
			map[ny+dy[k]][nx+dx[k]] == 1 ){
			destroy( ny+dy[k] , nx+dx[k] ) ;
		}
	}
}

int main(){
	
	while(1){
		cin >> w >> h ;
		if( h==0 && w==0 ) break ;
		
		count = 0 ;
		for( int i=0 ; i<h ; i++ ){
			for( int j=0 ; j<w ; j++ ){
				cin >> map[i][j] ;
			}
		}
		
		for( int i=0 ; i<h ; i++ ){
			for( int j=0 ; j<w ; j++ ){
				if( map[i][j] == 1 ){
					destroy(i,j) ;
					count++;
				}
			}
		}
		
		cout << count << endl ;
	}
	
return 0;
}

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

int state[100][100];
bool reached[100][100];
int w,h;

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

void dfs(int x, int y) {

	if(x==0 || w<x || y==0 || h<y || state[x][y] == 0)
		return;
	if(reached[x][y] == true)
		return;

	reached[x][y]=true;

	for(int i=0; i<8; i++) {
		dfs(x+dx[i],y+dy[i]);
	}
}

int main() {

	memset(state, 0, sizeof(state));
	memset(reached, false, sizeof(reached));

	cin >> w >> h;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; j++) {
			cin >> state[i][j];
		}
	}
	int count=0;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; j++) {
			if(state[i][j]==0)
				continue;
			else{
				dfs(i,j);
				count++;
			}
		}
	}
	cout << count << endl;
	return 0;
}

C++: #define	_USE_MATH_DEFINES
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
#include <list>
using namespace std;
int x,y;
int o[52][52];
queue<int> q;
void tansaku(int j,int i){
	if(j<1||j>x||i<1||i>y||o[i][j]==0||o[i][j]==2){
		return ;
	}
	if(o[i][j]==1){
		if(q.empty())
		q.push(1);
		o[i][j]=2;
	}
	
	tansaku(j+1,i);
	tansaku(j-1,i);
	tansaku(j,i+1);
	tansaku(j,i-1);
	tansaku(j+1,i+1);
	tansaku(j-1,i-1);
	tansaku(j+1,i-1);
	tansaku(j-1,i+1);
}
int main(){

	while(cin>>x>>y,x!=0||y!=0){
		int count=0;
		for(int i=1;i<=y;i++){
			for(int j=1;j<=x;j++){
				cin>>o[i][j];
			}
		}
		for(int i=1;i<=y;i++){
			for(int j=1;j<=x;j++){
				tansaku(j,i);
				if(!q.empty()){
				count+=q.front();
				
				q.pop();
				}
			}
		}
		
		cout<<count<<endl;
	}

}

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

vector<int> route;
#define P 50
#define Q 50
char a[P+2][Q+2];


void search(int place1,int place2){
    a[place1][place2] = '0';
    if(a[place1][place2-1] == '1'){//左
        route.push_back(1);
        search(place1,place2-1);
    }
    else if(a[place1+1][place2] == '1'){//下
        route.push_back(2);
        search(place1+1,place2);
    }
    else if(a[place1][place2+1] == '1'){//右
        route.push_back(3);
        search(place1,place2+1);
    }
    else if(a[place1-1][place2] == '1'){//上
        route.push_back(4);
        search(place1-1,place2);
    }
    else if(a[place1+1][place2-1] == '1'){//左下
        route.push_back(5);
        search(place1+1,place2-1);
    }
    else if(a[place1+1][place2+1] == '1'){//右下
        route.push_back(6);
        search(place1+1,place2+1);
    }
    else if(a[place1-1][place2+1] == '1'){//右上
        route.push_back(7);
        search(place1-1,place2+1);
    }
    else if(a[place1-1][place2-1] == '1'){//左上
        route.push_back(8);
        search(place1-1,place2-1);
    }
    else{
        int bk = route.back();
        route.pop_back();//戻る
        if(bk == 0) return;
        else if(bk == 1)search(place1,place2+1);
        else if(bk == 2)search(place1-1,place2);
        else if(bk == 3)search(place1,place2-1);
        else if(bk == 4)search(place1+1,place2);
        else if(bk == 5)search(place1-1,place2+1);
        else if(bk == 6)search(place1-1,place2-1);
        else if(bk == 7)search(place1+1,place2-1);
        else if(bk == 8)search(place1+1,place2+1);
    }
}

int main(void){
    int ct;
    int x,y;
    while(1){
        ct = 0;
        cin >> y >> x;
        if(x == 0 & y == 0) break;
        for(int i = 0;i < x+2;i++){
            for(int j = 0;j < y+2;j++){
                if(i == 0||j == 0||i == x+1||j == y+1)a[i][j] = '0';
                else cin >> a[i][j];
                
            }
            
        }
        for(int i = 1;i < x+1;i++){
            for(int j = 1;j < y+1;j++){
                if(a[i][j] == '1'){
                    route.push_back(0);
                    search(i,j);
                    ct++;
                }
            }
        }
        cout << ct << endl;
    }
    return 0;
}

C++: 
#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <math.h>

using namespace std;

void chkmap(int w, int h, int x, int y, int map[50][50]) {
	if(map[x][y] == 0) return;
	map[x][y] = 0;
	if(x + 1 < w) chkmap(w,h,x+1,y,map);
	if(x > 0)	  chkmap(w,h,x-1,y,map);
	if(y + 1 < h) chkmap(w,h,x,y+1,map);
	if(y > 0)	  chkmap(w,h,x,y-1,map);
	if(x + 1 < w && y + 1 < h) chkmap(w,h,x+1,y+1,map);
	if(x + 1 < w && y > 0)	   chkmap(w,h,x+1,y-1,map);
	if(x > 0 && y + 1 < h)     chkmap(w,h,x-1,y+1,map);
	if(x > 0 && y > 0)		   chkmap(w,h,x-1,y-1,map);
}

int main(void){
	//FILE* fp_in = freopen("data.txt", "r", stdin);

	while(1) {

		int w, h, ans = 0;
		int map[50][50];
		cin >> w >> h;

		if( w == 0 && h == 0 ) break;	//終了処理

		//マップ読み取り
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j ++) {
				int tmp;
				cin >> tmp;
				map[j][i] = tmp;
			}
		}


		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if(map[j][i] == 1) ans ++;
				chkmap(w,h,j,i,map);
			}
		}

		cout << ans << endl;
	}

	
	//while(1){}
	return 0;
}