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<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

bool used[55][55];
int h,w;
int a[55][55];
void dfs(int x,int y){
    REP(i,-1,2)REP(j,-1,2){
        int nx=x+i,ny=y+j;
        if(nx<0||nx>=h||ny<0||ny>=w)continue;
        if(used[nx][ny]||a[nx][ny]==0)continue;
        used[nx][ny]=1;
        dfs(nx,ny);
    }
}

void solve(){
    rep(i,h)rep(j,w)cin>>a[i][j];
    rep(i,h)rep(j,w)used[i][j]=false;
    int ans=0;
    rep(i,h)rep(j,w){
        if(used[i][j]||a[i][j]==0)continue;
        ++ans;
        used[i][j]=1;
        dfs(i,j);
    }
    cout<<ans<<endl;
}
int main(){
    while(cin>>w>>h,w!=0)solve();
    return 0;
}

C++: #include <stdio.h>

	int sima[51][51];
	int cnt=0;
	int cflag=0;

int count_sima(int w,int h){
	if(w<0)return 0;
	if(h<0)return 0;
	if(sima[w][h]==3)return 0;
	if(sima[w][h]==0)return 0;

		if(cflag==0){
			cnt++;
			cflag=1;
		}
		

		sima[w][h]=0;

		count_sima(w,h+1);
		count_sima(w+1,h+1);
		count_sima(w+1,h);
		count_sima(w+1,h-1);
		count_sima(w,h-1);
		count_sima(w-1,h-1);
		count_sima(w-1,h);
		count_sima(w-1,h+1);


	return 0;
}


int main(void){
	int w2,h2; //島の大きさを格納する
	int p;

	scanf("%d %d",&w2,&h2);
	while(w2!=0 && h2!=0){

		//島配列をすべてを3にする
		for(int i=0; i<51; i++){
			for(int j=0; j<51; j++){
				sima[i][j]=3;
			}
		}

		//入力された地図のデータを設定
		for(int i=0; i<h2; i++){
			for(int j=0; j<w2; j++){
				scanf("%d",&p);
				sima[i][j]=p;
			}
		}

		//ここまで入力された地図のデータを設定

		for(int i=0; i<h2; i++){
			for(int j=0; j<w2; j++){
				count_sima(i,j);
				cflag=0;
			}
		}

		printf("%d\n",cnt);
		cnt=0;

		scanf("%d %d",&w2,&h2);
	}

	return 0;
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){f(8)k(x+u[i],y+u[i+8]);d[x][y]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[0][i]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	printf("%d\n",a);
	main();
}

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

// 入力
int H, W;
vector<vector<int>> field;

// 探索
void dfs(int h, int w) {
    field[h][w] = 0;
    for (int dh = -1; dh <= 1; ++dh) {
        for (int dw = -1; dw <= 1; ++dw) {
            int nh = h + dh, nw = w + dw;
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if (field[nh][nw] == 0) continue;
            dfs(nh, nw);
        }
    }
}

int main() {
    while (cin >> W >> H) {
        if (H == 0 || W == 0) break;
        field.assign(H, vector<int>(W, 0));
        for (int h = 0; h < H; ++h) for (int w = 0; w < W; ++w) cin >> field[h][w];
        
        int count = 0;
        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                if (field[h][w] == 0) continue; 
                dfs(h, w);
                ++count;
            }
        }
        cout << count << endl;
    }
}

C++: #include <iostream>
using namespace std;
 
int main(void){
    while (1) {
        int Y, X;
        int c[55][55];
        cin >> X >> Y;
         
        //終了
        if (Y == 0 && X == 0) break;
         
        for (int i = 1; i <= Y; i++) {
            for (int j = 1; j <= X; j++) cin >> c[i][j];
        }
         
        //深さ別にリストを作成
        int vy[2555][155], vx[2555][155], pl[2555];
        for (int i = 0; i < 2000; i++) {
            pl[i] = 0;
            vy[i][0] = vx[i][0] = -1;
            for (int j = 1; j <= 553; j++) {
                vy[i][j] = vx[i][j] = 0;
            }
        }  
 
        //移動方向の指定
        int dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
        int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
     
        //通過済みかどうか
        bool psd[55][55];
        
        for (int i = 0; i <= 53; i++) {
            for (int j = 0; j <= 53; j++) psd[i][j] = false;
        }
        
        for (int i = 1; i <= Y; i++) {
            for (int j = 1; j <= X; j++) {
                psd[i][j] = true;
                if (c[i][j] == 0) psd[i][j] = false;
            }
        }
         
        for (int i = 0; i <= Y + 1; i++) psd[i][0] = psd[i][X + 1] = false;
        for (int i = 0; i <= X + 1; i++) psd[0][i] = psd[Y + 1][i] = false;
         
        bool fns = false;
        int isl = 0;
        while (1) {
            for (int inf = 0; true; inf++) {
                bool spot = false;
                if (inf == 0) {
                    vy[0][0] = -1;
                    for (int i = 1; i <= Y; i++) {
                        for (int j = 1; j <= X; j++) {
                            if (psd[i][j] == true) {
                                vy[0][0] = i;
                                vx[0][0] = j;
                                isl++;
                                pl[0] = 1;
                                psd[i][j] = false;
                                spot = true;
                                break;
                            }
                        }
                        if (spot) break;
                    }
                    if (vy[0][0] == -1) {
                        fns = true;
                        break; //4, 全部の島の確認終了
                    }
                }
                else {
                    int pt = 0;
                    for (int i = 0; i < pl[inf - 1]; i++) {
                        for (int dir = 0; dir < 8; dir++) {
                            
                            if (vy[inf - 1][i] + dy[dir] != -1 && vy[inf - 1][i] + dy[dir] != Y + 2 && vx[inf - 1][i] + dx[dir] != -1 && vx[inf - 1][i] + dx[dir] != X + 2) {
                                if (psd[vy[inf - 1][i] + dy[dir]][vx[inf - 1][i] + dx[dir]] == true) {
                                    vy[inf][pt] = vy[inf - 1][i] + dy[dir];
                                    vx[inf][pt] = vx[inf - 1][i] + dx[dir];
                                    pl[inf]++;
                                    psd[vy[inf][pt]][vx[inf][pt]] = false;
                                    pt++;
                                }
                            }
                        }
                    }
                    if (pt == 0) break; //4, 1つの島の確認終了
                }
//                cout << "*" << inf << endl;
//                for (int i = 0; i < pl[inf]; i++) cout << vy[inf][i] << ", " << vx[inf][i] << endl;
            }
            if (fns) {
                cout << isl << endl;
                break; // 2, query終了
            }
        }
    }
}