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 <stack>
#include <cstdio>

using namespace std;
stack<int> y;
stack<int> x;
bool c[52][52];

int check(int arg[][52], int i, int j);

int main(void){
	int w, h;
	int arg[52][52];
	int i, j, si, sj;
	int count;


	scanf("%d %d", &w, &h);
	while(w!=0 && h!=0){
		count=0;
		for(i=0; i<=h+1; i++){
			for(j=0; j<=w+1; j++){
				if(i==0 || i==(h+1) || j==0 || j==(w+1)){
					c[i][j]=false; 
					arg[i][j]=0;
				}
			}
		}

		for(i=1; i<=h; i++){
			for(j=1; j<=w; j++){ 
				scanf("%d",&arg[i][j]);
				if(arg[i][j])
					c[i][j]=true;
				else
					c[i][j]=false;
			}
		}

		for(i=1; i<=h; i++){
			for(j=1; j<=w; j++){
				if(arg[i][j]==1 && c[i][j]==true){
					y.push(i);
					x.push(j);
					while(!x.empty() && !y.empty()){
							si=y.top();sj=x.top();
							y.pop();x.pop();
							check(arg, si, sj);

					}
					count++;

				}
			}
		}


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

	return 0;
}

int check(int arg[][52], int i, int j){
	int flag = 0;

	c[i][j]=false;
	if(arg[i+1][j]==1 && c[i+1][j]==true){
		c[i+1][j]=false;
		y.push(i+1);
		x.push(j);
		flag = 1;
	}
	if(arg[i][j+1]==1 && c[i][j+1]==true){
		c[i][j+1]=false;
		y.push(i);
		x.push(j+1);
		flag = 1;
	}
	if(arg[i-1][j]==1 && c[i-1][j]==true){
		c[i-1][j]=false;
		y.push(i-1);
		x.push(j);
		flag = 1;
	}
	if(arg[i][j-1]==1 && c[i][j-1]==true){
		c[i][j-1]=false;
		y.push(i);
		x.push(j-1);
		flag = 1;
	}
	if(arg[i-1][j+1]==1 && c[i-1][j+1]==true){
		c[i-1][j+1]=false;
		y.push(i-1);
		x.push(j+1);
		flag = 1;
	}
	if(arg[i-1][j-1]==1 && c[i-1][j-1]==true){
		c[i-1][j-1]=false;
		y.push(i-1);
		x.push(j-1);
		flag = 1;
	}
	if(arg[i+1][j+1]==1 && c[i+1][j+1]==true){
		c[i+1][j+1]=false;
		y.push(i+1);
		x.push(j+1);
		flag = 1;
	}
	if(arg[i+1][j-1]==1 && c[i+1][j-1]==true){
		c[i+1][j-1]=false;
		y.push(i+1);
		x.push(j-1);
		flag = 1;
	}

	if(flag)
		return 1;
	else
		return 0;
}

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;

            // 場外アウトしたり、0 だったりはスルー
            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 <stdio.h>
#include <string.h>

void island_delete(int data[][60], int w, int h, int self_i, int self_j) {
  data[self_i][self_j] = 0;
  if (self_j-1 >= 0 && data[self_i][self_j-1] == 1) {
	island_delete(data,w,h,self_i,self_j-1);
  }
  if (self_i-1 >= 0 && data[self_i-1][self_j] == 1) {
	island_delete(data,w,h,self_i-1,self_j);
  }
  if (self_j+1 <= w && data[self_i][self_j+1] == 1) {
	island_delete(data,w,h,self_i,self_j+1);
  }
  if (self_i+1 <= h && data[self_i+1][self_j] == 1) {
	island_delete(data,w,h,self_i+1,self_j);
  }

  if (self_j-1 >= 0 && self_i-1 >= 0 && data[self_i-1][self_j-1] == 1) {
	island_delete(data,w,h,self_i-1,self_j-1);
  }
  if (self_j+1 <= w && self_i-1 >= 0 && data[self_i-1][self_j+1] == 1) {
	island_delete(data,w,h,self_i-1,self_j+1);
  }
  if (self_j-1 >= 0 && self_i+1 <= h && data[self_i+1][self_j-1] == 1) {
	island_delete(data,w,h,self_i+1,self_j-1);
  }
  if (self_j+1 <= w && self_i+1 <= h && data[self_i+1][self_j+1] == 1) {
	island_delete(data,w,h,self_i+1,self_j+1);
  }


  
}

int main(void) {
  while(1) {
	int w, h;
	int cnt = 0;
	int data[60][60] = {{0}};
	scanf("%d%d", &w, &h);
	if (w == h && w == 0) break;
	for (int i = 0; i < h; i++) {
	  for (int j = 0; j < w; j++) {
		scanf("%d", &data[i][j]);
	  }
	}
	int self_i, self_j;
	for (int i = 0; i < h; i++) {
	  for (int j = 0; j < w; j++) {
		if (data[i][j] == 1) {
		  self_i = i; self_j = j; cnt++;
		  island_delete(data,w,h,self_i,self_j);
		}
	  }
	}
	printf("%d\n",cnt);
  }
  return 0;
}

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

void dfs(vector< vector<int> > &island, int x, int y) {
    island[x][y] = 0;

    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            int nx = x + dx;
            int ny = y + dy;
            if (nx >= 0 && nx < island.size() && ny >= 0 && ny < island[0].size() && island[nx][ny] == 1) {
                dfs(island, nx, ny);
            }
        }
    }
}

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

        vector< vector<int> > island(h, vector<int>(w));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> island[i][j];
            }
        }

        int res = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (island[i][j] == 1) {
                    dfs(island, i, j);
                    res++;
                }
            }
        }

        cout << res << endl;
    }

    return 0;
}

C++: #include<stdio.h>
#include <stdlib.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<time.h>
using namespace std;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define vint vector<int> 
#define vvint vector<vint>
#define ct(a) cout<<a<<endl
typedef long long ll;

int w, h,cn[1000];
bool field[51][51];

void dfs(int i,int k) {
	field[i][k] = 0;
	FOR(x, -1, 2) {
		FOR(y, -1, 2) {
			int nx = k + x, ny = i + y;
			if (nx < w&&nx >= 0 && ny < h&&ny >= 0 && field[ny][nx] == 1)dfs(ny, nx);
		}
	}
	return;
}

int main(void)
{
	int c = 0;
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0)break;
		FOR(i, 0, h)FOR(k, 0, w)cin >> field[i][k];

		FOR(i, 0, h)FOR(k, 0, w) {
			if (field[i][k] == 1) {
				dfs(i, k);
				cn[c]++;
			}
		}
		ct(cn[c]);
		c++;
	}

	return 0;
}