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;
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;
	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){
					count = count + check(arg, i, j);
				}
			}
		}

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

	return 0;
}

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

	if(arg[i+1][j]==1 && c[i+1][j]==true){
		c[i+1][j]=false;
		check(arg, i+1, j);
	}
	if(arg[i][j+1]==1 && c[i][j+1]==true){
		c[i][j+1]=false;
		check(arg, i, j+1);
	}
	if(arg[i-1][j]==1 && c[i-1][j]==true){
		c[i-1][j]=false;
		check(arg, i-1, j);
	}
	if(arg[i][j-1]==1 && c[i][j-1]==true){
		c[i][j-1]=false;
		check(arg, i, j-1);
	}
	if(arg[i-1][j+1]==1 && c[i-1][j+1]==true){
		c[i-1][j+1]=false;
		check(arg, i-1, j+1);
	}
	if(arg[i-1][j-1]==1 && c[i-1][j-1]==true){
		c[i-1][j-1]=false;
		check(arg, i-1, j-1);
	}
	if(arg[i+1][j+1]==1 && c[i+1][j+1]==true){
		c[i+1][j+1]=false;
		check(arg, i+1, j+1);
	}
	if(arg[i+1][j-1]==1 && c[i+1][j-1]==true){
		c[i+1][j-1]=false;
		check(arg, i+1, j-1);
	}

	return 1;

}

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

void search(vector<vector<int>>& map, int x, int y)
{
	if (map[x][y] < 0)
		return;

	if (map[x][y] == 1) {
		map[x][y] = 0;
		search(map, x - 1, y);
		search(map, x + 1, y);
		search(map, x, y - 1);
		search(map, x, y + 1);
		search(map, x - 1, y - 1);
		search(map, x + 1, y + 1); 
		search(map, x + 1, y - 1);
		search(map, x - 1, y + 1);
	}
}
int main()
{
	int w, h;
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0)
			break;

		vector<vector<int>> map;
		map = vector<vector<int>>(h+2, vector<int>(w+2, -1));

		for (int i = 1; i < h+1; i++) {
			for (int j = 1; j < w+1; j++)
				cin >> map[i][j];
		}
		int island_n = 0;
		for (int i = 1; i < h + 1; i++) {
			for (int j = 1; j < w + 1; j++) {
				if (map[i][j] == 1) {
					island_n++;
					search(map,i, j);
				}
			}
		}
		cout << island_n << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

int w,h;
int dim[52][52];
int mark[52][52];
int dirx[8] = {-1,-1,-1,0,0,1,1,1};
int diry[8] = {-1,0,1,-1,1,-1,0,1};
queue<pair<int,int> > q;
int check(int x,int y){
	if (x <0 || x >= h || y<0 || y>=w)return 0;
	return 1;
}
int main(){
	while(scanf("%d %d", &w, &h)!= EOF){
		if (w==0 && h==0)return 0;
		memset(mark, -1, sizeof(mark));
		for (int i=0;i<h;i++){
			for (int j=0;j<w;j++){
				scanf("%d", &dim[i][j]);
			}	
		}
		int island = 0;
		for (int i=0;i<h;i++){
			for (int j=0;j<w;j++){
				if (dim[i][j] == 1 && mark[i][j] == -1){
					island++;
					q.push(make_pair(i, j));
					mark[i][j] = 1;
					while(!q.empty()){
						pair<int, int> temp= q.front();
						q.pop();
						for (int k=0;k<8;k++){
							int tx = temp.first + dirx[k];
							int ty = temp.second + diry[k];
							if (check(tx,ty) == 1){
								if (dim[tx][ty] && mark[tx][ty] == -1){
									mark[tx][ty] = 1;
									q.push(make_pair(tx,ty));
								}
							}
						}
					}
				}
			}	
		}
		printf("%d\n", island);
	}
	return 0;
}

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

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, vector<vector<bool> >& map){
    map[y][x] = false;
    for(int i=0; i<8; i++){
        if(map[y+dy[i]][x+dx[i]]){
            dfs(x+dx[i], y+dy[i], map);
        }
    }
}

int main(){
    while(1){
        int W,H;
        cin >> W >> H;
        if(W==0) break;

        vector<vector<bool> > map(H+2, vector<bool>(W+2,false));
        for(int y=1; y<=H; y++){
            for(int x=1; x<=W; x++){
                char tmp;
                cin >> tmp;
                if(tmp == '0'){
                    map[y][x]=false;
                }else{
                    map[y][x]=true;
                }
            }
        }

        int num=0;
        for(int y=1; y<=H; y++){
            for(int x=1; x<=W; x++){
                if(map[y][x]){
                    num++;
                    dfs(x,y,map);
                }
            }
        }
        cout << num << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int w,h;
int nx,ny;
void dfs(vector<vector<int>> &G,int a,int b){
    G.at(a).at(b)=0;
    
    for(int k=-1;k<=1;k++){
        for(int l=-1;l<=1;l++){
            nx=a+k;
            ny=b+l;
        if(0<=nx&& nx<h && 0<=ny&& ny<w && G.at(nx).at(ny)==1) dfs(G,nx,ny);
        }
    }
    return ;
}



int main(){
    int w,h;
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                dfs(G,i,j);
                count++;
            }
        }
    }
cout<<count<<endl;
}

C++: #include<stdio.h>
#include<iostream>
#include <utility>
#include<stack>
#include<cstdio>
using namespace std;

int field[51][51];
int w,h;

void land(int x,int y){
	stack<pair<int,int> > field2;
	int dx[]={1,1,1,-1,-1,-1,0,0},dy[]={1,0,-1,1,0,-1,1,-1};
	int xx,yy,flag,stka=1;

	field2.push(pair<int,int>(x,y));
	field[x][y]=0;

	while(1){
		flag=0;
		for(int i=0;i<9;i++){
			if(field2.top().first+dx[i]>=0&&field2.top().first+dx[i]<=w&&field2.top().second+dy[i]>=0&&field2.top().second+dy[i]<=h){
				if(field[field2.top().first+dx[i]][field2.top().second+dy[i]]==1){
					xx=field2.top().first+dx[i],yy=field2.top().second+dy[i];
					field[field2.top().first+dx[i]][field2.top().second+dy[i]]=0;
					field2.push(pair<int,int>(xx,yy));
					stka+=1;
					flag=1;
					break;
				}
			}
		}
		if(flag==0){
			field2.pop();
			stka--;
			if(stka==0){
				break;
			}
		}
	}
	return;
}

int main(void){

	int kazu=0;

	while(1){

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

	if(w==0&&h==0) break;

	kazu=0;

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			scanf("%d",&field[j][i]);
		}
	}

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[j][i]==1){
				land(j,i);
				kazu++;
			}

		}
	}

	printf("%d\n",kazu);
	}

	return 0;
}