Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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;
}