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;

constexpr int MAX_H = 55;
constexpr int MAX_W = 55;

int H, W;
int grid[MAX_H][MAX_W];
bool reached[MAX_H][MAX_W];

constexpr int dh[]{-1, -1, -1, 0, 0, +1, +1, +1};
constexpr int dw[]{-1, 0, +1, -1, +1, -1, 0, +1};

void dfs(int h, int w) {
  reached[h][w] = true;

  for (int i{}; i < 8; ++i) {
    int nh = h + dh[i];
    if (nh < 0 || nh >= H)
      continue;
    int nw = w + dw[i];
    if (nw < 0 || nw >= W)
      continue;
    if (grid[nh][nw] == 0 || reached[nh][nw])
      continue;
    dfs(nh, nw);
  }
}

int main() {
  while (true) {
    cin >> H >> W;
    if (H == 0 && W == 0)
      break;
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        reached[h][w] = false;
        cin >> grid[h][w];
      }
    }

    int island{};
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        if (grid[h][w] == 0 || reached[h][w])
          continue;
        dfs(h, w);
        ++island;
      }
    }

    cout << island << endl;
  }

  return 0;
}

C++: #include <cstdio>

using namespace std;

int parent[2500];

int Find(int x){
    if(parent[x] != x) parent[x] = Find(parent[x]);
    return parent[x];
}

void Union(int x, int y){
    x = Find(x); y = Find(y);
    parent[x] = y;
}

int main(){
    int n,m;
    int M[50][50];
    
    while(true){
        scanf("%d %d",&m,&n);
        
        if(n == 0) break;
        
        for(int i = 0;i < n;++i)
            for(int j = 0;j < m;++j)
                scanf("%d",&M[i][j]);
        
        for(int i = n*m - 1;i >= 0;--i)
            parent[i] = i;
        
        for(int i = 0;i < n;++i)
            for(int j = 0;j < m;++j)
                if(M[i][j] == 1)
                    for(int dr = -1;dr <= 1;++dr)
                        for(int dc = -1;dc <= 1;++dc)
                            if(i + dr >= 0 && i + dr < n && j + dc >= 0 && j + dc < m && M[i + dr][j + dc] == 1)
                                Union(m * i + j, m * (i + dr) + j + dc);
        
        int ans = 0;
        
        for(int i = 0;i < n;++i)
            for(int j = 0;j < m;++j)
                if(M[i][j] == 1 && Find(m * i + j) == m * i + j)
                    ++ans;
        
        printf("%d\n",ans);
    }
    
    return 0;
}

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

using namespace std;

int W, H;

int field[52][52];
bool visited[52][52];
int tx[] = {1, 1, 1, 0, -1, -1, -1, 0};
int ty[] = {1, 0, -1, -1, -1, 0, 1, 1};

void island(int y, int x)
{
  visited[y][x] = true;
  for (int i = 0; i < 8; i++) {
    if (!visited[y + ty[i]][x + tx[i]] && field[y + ty[i]][x + tx[i]] == 1) {
      island(y + ty[i], x + tx[i]);
    }
  }
}

void solve()
{
  fill_n(&visited[0][0], 52 * 52, false);
  int c = 0;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (!visited[i][j] && field[i][j] == 1) {
	island(i, j);
	c++;
      }
    }
  }
  printf("%d\n", c);
}

int main()
{
  while (scanf("%d %d ", &W, &H) == 2) {
    if (W == 0 && H == 0) {
      return 0;
    }
    fill_n(&field[0][0], 52 * 52, 0);
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
	scanf("%d ", &field[i][j]);
      }
    }
    solve();
  }
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
 
#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;
 
using namespace std;
 
 int dx[8] = {1,0,-1,0,1,1,-1,-1};
 int dy[8] = {0,1,0,-1,-1,1,-1,1};

void dfs(vector<vector<int> > &map,int x,int y,int h, int w){
  
    if(x < 0 || x >=w || y < 0 || y >=h){
      return ;
    }

    if(map[y][x]==0){
      return;
    }

    map[y][x] = 0;

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

    return;
}


int main()
{

  while(true){

    int w,h;
    cin >> w >> h;

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

    vector<vector<int> > map(h,vector<int>(w,0));    
    int count=0;

    for(int i=0;i<h;i++){
      for(int k=0;k<w;k++){
        cin >> map[i][k];
      }
    }


   for(int i=0;i<w;i++){
    for(int k=0;k<h;k++){
      if(map[k][i]==1){
        dfs(map,i,k,h,w);
        count++;
      }
    }
   }
 


    cout << count << endl;

  }

    return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<vector>
int w,h,fld[64][64];

void dfs(int y,int x){
    if(x<0||x>=w||y<0||y>=h)return;
    if(!fld[y][x])return;
    fld[y][x]=0;
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            if(i||j)dfs(y+i,x+j);
        }
    }
}
int main(){
    while(scanf("%d%d",&w,&h),w||h){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                scanf("%d",&fld[i][j]);
            }
        }

        int cnt=0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(fld[i][j]){
                    cnt++;
                    dfs(i,j);
                }
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int f(int a[][52],int b[][52],int x,int y){
  if(a[x][y]==0||b[x][y]==1)
    return 0;
  else{
    b[x][y]=1;
    f(a,b,x-1,y-1);
    f(a,b,x,y-1);
    f(a,b,x+1,y-1);
    f(a,b,x-1,y);
    f(a,b,x+1,y);
    f(a,b,x-1,y+1);
    f(a,b,x,y+1);
    f(a,b,x+1,y+1);
    return 1;
  }
}
int main(){
  int i,j;
  int a[52][52],b[52][52],w,h;
  int ct;
  while(cin>>w>>h&&w+h){
    memset(a,0,sizeof(a));
    for(i=1;i<h+1;i++)
      for(j=1;j<w+1;j++)
	cin>>a[j][i];
    memset(b,0,sizeof(b));
    ct=0;
    for(i=1;i<h+1;i++)
      for(j=1;j<w+1;j++)
	ct+=f(a,b,j,i);
    cout<<ct<<endl;
  }
  return 0;
}