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 <cstdio>
#define f(i,n)for(int i=0;i<n;i++)
#define s (scanf("%d",&e),e)
int main(){int a=0,w,n,m,b,e,d[99];f(i,99)d[i]=0;n=s;m=s;if(n==0)return 0;f(i,m){w=b=0;f(j,n){s;if(e)f(k,3){e=d[j+k];if(e&&e-w){if(w){f(l,n+2)d[l]=d[l]==e?w:d[l];a--;}else w=e;}if(w==0)a++,w=i*n+j+1;}else w=0;d[j]=b;b=w;}d[n]=b;}printf("%d\n",a);main();}

C++: #include<iostream>
#include<vector>
#include <unordered_map>
#include <algorithm>
#include <functional>
#include <map>
#include <string>
#include <sstream>
#include <cmath>
#include <float.h>
using namespace std;



bool checked[50][50];
int island[50][50];
int count1 = 0;
int w, h;
vector<int> num;
unsigned long long int mod = 1000000007;

void checkIsland(int x, int y) {
    if (x < 0 || x >= h || y >= w || y < 0) { return; }
    if (!island[x][y] || checked[x][y]) {
        checked[x][y] = true;
        return;
    }
    checked[x][y] = true;
    checkIsland(x + 1, y);
    checkIsland(x + 1, y + 1);
    checkIsland(x + 1, y - 1);
    checkIsland(x, y + 1);
}

void runtime() {
    count1 = 0;
    for (int i = 0; i < w; ++i) {
        for (int j = 0; j < h; ++j) {
            checked[i][j] = false;
            cin >> island[i][j];
        }
    }
    for (int i = 0; i < w; ++i) {
        for (int j = 0; j < h; ++j) {
            if (checked[i][j]) continue;
            if (island[i][j]) {
                count1++;
                checkIsland(i, j);
            }
            else {
                checked[i][j] = true;
            }
        }
    }
    num.push_back(count1);
}


int main() {
    cin >> w >> h;
    while (!(w == 0 && h == 0)) {
        count1 = 0;
        runtime();
        cin >> w >> h;
    }
    for (auto i : num) {
        cout << i << endl;
    }
    return 0;
}

C++: #include<cstdio>
using namespace std;
int w,h,cnt,a[50][50];
void func(int,int);

int main(void)
{
	int i,j;
	while(1)	{
		scanf("%d %d",&w,&h);
		if(w==0 && h==0)	break;
		for(i=0;i<h;i++)	{
			for(j=0;j<w;j++)	{
				scanf("%d",&a[i][j]);
			}
		}
		cnt=0;
		for(i=0;i<h;i++)	{
			for(j=0;j<w;j++)	{
				if(a[i][j]==1)	{
					cnt++;
					a[i][j]=0;
					func(i,j);
				}
			}
		}
		printf("%d\n",cnt);
	}
	return 0;
}
void func(int y,int x)
{
	int i,j,xx,yy;
	for(i=-1;i<=1;i++)	{
		for(j=-1;j<=1;j++)	{
			if(y+i>=0 && y+i<h && x+j>=0 && x+j<w && a[y+i][x+j]==1)	{
				a[y+i][x+j]=0;
				func(y+i,x+j);
			}
		}
	}
}

C++: 

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;
 
#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
 
int w,h;
int island[200][200];
int c=0;
bool searched[200][200];

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

void search(int x,int y){ 
  searched[x][y] = true;
  //island[i][j]==1?????????
  REP(i,8){
	int nx=x+dx[i]; int ny=y+dy[i];
	if(nx<0||ny<0||nx>h-1||ny>w-1) continue;
	//cout << nx << " " << ny << endl;
	if(island[nx][ny]&&searched[nx][ny]==false){
	  search(nx,ny);
	}
  }
}

int main(){

  while(1){
	c = 0;
	cin >> w >> h;

	if(w==0&&h==0) break;
	REP(i,h){
	  REP(j,w){
		searched[i][j] = false;
		cin >> island[i][j];
	  }
	}

	REP(i,h){
	  REP(j,w){
		if(island[i][j]&&searched[i][j]==false){
		  search(i,j);
		  c++;
		}

	  }}

	cout << c <<endl;

  }


}

C++: #include <iostream>

using namespace std;

#define MAX_H 55
#define MAX_W 55

int W, H;

int C[MAX_H][MAX_W];

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

void dfs(int x, int y)
{
	if (C[y][x] == 1)
	{
		C[y][x] = 0;

		for (int i = 0; i < 8; i++)
		{
			if (0 <= x + dx[i] && x + dx[i] <= W - 1 && 0 <= y + dy[i] && y + dy[i] <= H - 1)
			{
				if (C[y + dy[i]][x + dx[i]] == 1)
				{
					dfs(x + dx[i], y + dy[i]);
				}
			}
		}
	}
}

int main()
{
	while (true)
	{
		cin >> W >> H;

		if (W == 0 && H == 0) { break; }

		int count = 0;

		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				if (C[i][j] == 1)
				{
					dfs(j, i);

					count++;
				}
			}
		}

		cout << count << endl;
	}

	return 0;
}

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

void dfs(std::vector<std::vector<int> >& map, std::vector<std::vector<int> >& visited, int& island, int x, int y, int w, int h)
{
	visited.at(x).at(y) = island;
	int dx[8] = {1, 1, 0, -1, -1, 0, 1, -1};
	int dy[8] = {1, 0, 1, -1, 0, -1, -1, 1};
	for (int i = 0; i < 8; i++)
	{
		int nex = dx[i] + x;
		int ney = dy[i] + y;
		if (nex < 0 || nex >= w || ney < 0 || ney >= h)
			continue;
		if (map.at(nex).at(ney) == 1 && visited.at(nex).at(ney) != island)
		{	
			dfs(map, visited, island, nex, ney, w, h);
		}
	}
}


int main()
{
	std::vector<int> I;
	while (true)
	{
		int w, h;
		std::cin >> w >> h;
		if (w == 0 && h == 0)
			break;
		std::vector<std::vector<int> > map(h, std::vector<int> (w));
		std::vector<std::vector<int> > visited(h, std::vector<int> (w, 0));
		for (int i = 0; i < h; i++)
		{
			for (int j = 0; j < w; j++)
			{
				std::cin >> map.at(i).at(j);
			}
		}
		int island = 1;
		for (int i = 0; i < h; i++)
		{
			for (int j = 0; j < w; j++)
			{
				if (map.at(i).at(j) == 1 && visited.at(i).at(j) == 0)
				{
					dfs(map, visited, island, i, j, h, w);
					++island;	
				}
			}
		}
		I.push_back(island - 1);
	}
	for (std::vector<int>::iterator i = I.begin(); i != I.end(); i++)
		std::cout << *i << '\n';

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int size = 12;
//const int size = 14;
char a[size][size];
int cnt = 0;
void dfs(int x, int y){
    //現在地を０にする
    a[x][y] = 0;
    //4近傍を探索
    //x+1, y
    int nx = x+1, ny=y;
    if(0 <= nx and nx < 12 and 0 <= ny and ny < 12 and a[nx][ny] == '1') dfs(nx, ny);
    //x-1, y
    nx = x-1, ny = y;
    if(0 <= nx and nx < 12 and 0 <= ny and ny < 12 and a[nx][ny] == '1') dfs(nx, ny);
    //x, y+1
    nx = x, ny = y+1;
    if(0 <= nx and nx < 12 and 0 <= ny and ny < 12 and a[nx][ny] == '1') dfs(nx, ny);
    //x, y-1
    nx = x, ny = y - 1;
    if(0 <= nx and nx < 12 and 0 <= ny and ny < 12 and a[nx][ny] == '1') dfs(nx, ny);
    return;
}

int main(){
    while (1) {
    cnt = 0;
    //マップを入力
    for(int i=0; i < 12; i++){
        for(int j=0; j < 12; j++) if(!(cin >> a[i][j])) return 0;
    }


    //サーチ
    for(int i=0; i < 12; i++){
        for(int j=0; j < 12; j++){
            if(a[i][j] == '1'){
                cnt += 1;
                dfs(i, j);
            }
        }
    }

    cout << cnt << endl;
}
    return 0;
}