Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

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

using namespace std;

#define rep(i,a) for(int i=0;i<a;i++)

int di[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dj[] = {-1,  0,  1,-1, 1,-1, 0, 1};
int field[60][60];
int w, h;
bool inrange(int i, int j){
    return i >= 0 && j >= 0 && i < h && j < w;
}

void rec(int i, int j){
    field[i][j] = 0;
    rep(k, 8){
        int ni = i+di[k];
        int nj = j+dj[k];
        if(inrange(ni, nj) && field[ni][nj] == 1){
            rec(ni, nj);
        }
    }
}


int main(){
    int n,p;
    while(cin>>w>>h,w||h){
        rep(i, h){
            rep(j, w){
                cin >> field[i][j];
            }
        }
        int ans = 0;
        rep(i, h){
            rep(j, w){
                if(field[i][j] == 1) {
                    rec(i, j);
                    ans++;/*
                    rep(a, h) {
                        rep(b, w){
                            cout << field[a][b] << " ";
                        }
                        cout << endl;
                    }
                    cout << endl;*/
                }
            }
        }
        cout << ans << endl;
    }
}

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