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++: /* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.cpp                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: monoue <marvin@student.42.fr>              +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/04/22 12:30:15 by monoue            #+#    #+#             */
/*   Updated: 2020/05/05 08:55:10 by monoue           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <bits/stdc++.h>

/* #include <boost/multi_array.hpp> */
/* #include <boost/optional.hpp> */
/* #include <boost/range/irange.hpp> */
/* #include <boost/range/algorithm.hpp> */
/* #include <boost/range/adaptors.hpp> */
/* #include <boost/multiprecision/cpp_int.hpp> */
/* #include <boost/algorithm/string/classification.hpp> // is_any_of */
/* #include <boost/algorithm/string/split.hpp> */
/* #include <boost/algorithm/string/join.hpp> */
/* #include <boost/algorithm/string/replace.hpp> */
/* #include <boost/math/tools/minima.hpp> */
/* namespace adaptor = boost::adaptors; */
/* using boost::math::tools::brent_find_minima; */
/* using boost::irange; */
/* using boost::algorithm::join; */
/* using boost::algorithm::split; */
/* using boost::algorithm::replace_all; */
/* using boost::multiprecision::cpp_int; */

using namespace std;

#define	int	long long
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; --i)

typedef long long 			ll;
typedef vector<int>			VI;
typedef vector<vector<int>>	VVI;
typedef pair<int, int>		PII;

// TOP
int	h, w;

VVI	field;

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

void	dfs(int y, int x)
{
	field[y][x] = 0;
	int	ny, nx;
	rep(dir, 8)
	{
		ny = y + dy[dir];
		nx = x + dx[dir];
		if (ny < 0 || ny >= h || nx < 0 || nx >= w)
			continue;
		if (field[ny][nx] == 0)
			continue;
		dfs(ny, nx);
	}
}

void	Main()
{
	while (cin >> w >> h)
	{
		if (w == 0 && h == 0)
			break;
		field.assign(h, VI(w));
		rep(y, h)
		{
			rep(x, w)
					cin >> field[y][x];
		}
		int count;
		count = 0;
		rep(y, h)
		{
			rep(x, w)
			{
				if (field[y][x] == 0)
					continue;
				dfs(y, x);
				count++;
			}
		}
		cout << count << endl;
	}
}
// END

int32_t	main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	Main();
}

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

int w,h;
int islID;
int mp[52][52], island[52][52];
void dfs(int x, int y){
  if (mp[x][y]==0 || island[x][y]) return;
  island[x][y] = islID;
  for (int dx = -1; dx<=1; dx++){
    for (int dy= -1; dy<=1; dy++){
      if (dx==0 && dy==0) continue;
      dfs(x+dx, y+dy);
    }
  }
}

int main(void){  
  while (cin >> w >> h){
    if (w==0 && h==0) break;
    for (int i=0; i<h; i++){
      for (int j=0; j<w; j++){
        cin >> mp[j+1][i+1];
        island[j+1][i+1] = 0;
      }
    }
    islID = 0;
    for (int i=1; i<=h; i++){
      for (int j=1; j<=w; j++){
        if (mp[j][i] && !island[j][i]){
          islID++; 
          dfs(j, i);
        }
//        cout << island[j][i] << " ";
      }
//      cout << endl;
    }
    cout << islID << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
int dx[] = {1,1,0,-1,-1,-1,0,1};
int dy[] = {0,1,1,1,0,-1,-1,-1};
int c[51][51];
int main()
{

	int w, h;

	while (cin >> w && cin >> h && w != 0 && h != 0)
	{
		for (int i = 1; i < h + 1; i++)
		{
			for (int j = 1; j < w + 1; j++)
			{
				int tmp;
				cin >> tmp;
				if (tmp == 0)
				{
					c[i][j] = -1;
				}
				else
				{
					c[i][j] = 0;
				}
			}
		}

		int count = 0;
		queue<pair<int,int> > q;
		for (int i = 1; i < h + 1; i++)
		{
			for (int j = 1; j < w + 1; j++)
			{
				if (c[i][j] != 0)continue;
				q.push(make_pair(i,j));
				c[i][j] = 1;
				while (q.size() > 0)
				{
					pair<int, int> p = q.front();
					q.pop();
					int y = p.first;
					int x = p.second;

					for (int d = 0; d < 8; d++)
					{
						int nx = x + dx[d];
						int ny = y + dy[d];

						if (nx < 1 || ny < 1 || nx > w || ny > h)continue;
						if (c[ny][nx] != 0)continue;
						q.push(make_pair(ny, nx));
						c[ny][nx] = 1;
					}
				}
				count++;
			}
		}

		cout << count << endl;


	}

	return 0;
}

C++: #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
struct pos {
  int row;
  int column;
};
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
const int INF = 1 << 30;
const ll MOD = 1000000007;
vector<pair<int, int>> directions = {
    make_pair(0, 1),  make_pair(1, 0),   make_pair(1, 1),  make_pair(0, -1),
    make_pair(-1, 0), make_pair(-1, -1), make_pair(-1, 1), make_pair(1, -1),
};
void traverse(int grid[55][55], bool dp[55][55], int row, int column,
              int maxRow, int maxColumn) {
  dp[row][column] = true;

  for (auto direction : directions) {
    int newRow = row + direction.first;
    int newColumn = column + direction.second;

    if (newRow < 0 || newRow >= maxRow || newColumn < 0 ||
        newColumn >= maxColumn) {
      continue;
    }

    if (grid[newRow][newColumn] == 0) continue;
    if (dp[newRow][newColumn]) continue;

    traverse(grid, dp, newRow, newColumn, maxRow, maxColumn);
  }
}
int main(void) {
  vector<int> results;
  while (true) {
    bool dp[55][55] = {false};
    int grid[55][55] = {0};
    int ROW, COLUMN;
    cin >> COLUMN >> ROW;
    if (ROW == 0 && COLUMN == 0) break;
    rep(row, ROW) {
      rep(column, COLUMN) { cin >> grid[row][column]; }
    }

    int count = 0;
    rep(row, ROW) {
      rep(column, COLUMN) {
        if (grid[row][column] == 0) continue;
        if (dp[row][column]) continue;
        traverse(grid, dp, row, column, ROW, COLUMN);
        count++;
      }
    }

    results.push_back(count);
  }

  for (auto result : results) {
    cout << result << endl;
  }

  return 0;
}