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++: #include <bits/stdc++.h>
       
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
       

template <typename T>
class Grid {
  struct Pos {
    int i, j;
    Pos(int i, int j) : i(i), j(j) {}
  };

  int H, W;
  vector<vector<T>> G;

  vector<int> di8 = {1, 1, 0, -1, -1, -1, 0, 1},
              dj8 = {0, 1, 1, 1, 0, -1, -1, -1};
  vector<int> di4 = {1, 0, -1, 0}, dj4 = {0, 1, 0, -1};

  vector<Pos> neighbor(int i, int j, const vector<int>& dis,
                       const vector<int>& djs) const {
    vector<Pos> res;
    for (int k = 0; k < dis.size(); ++k) {
      if (in(i + dis[k], j + djs[k])) res.emplace_back(i + dis[k], j + djs[k]);
    }
    return res;
  }

 public:
  static bool in(int H, int W, int i, int j) {
    return 0 <= i && i < H && 0 <= j && j < W;
  }

  Grid(int H, int W) : H(H), W(W) { G.resize(H, vector<T>(W)); }

  vector<T>& operator[](int i) { return G[i]; }

  bool in(int i, int j) const { return (0 <= i && i < H && 0 <= j && j < W); }

  vector<Pos> neighbor8(int i, int j) const { return neighbor(i, j, di8, dj8); }

  vector<Pos> neighbor4(int i, int j) const { return neighbor(i, j, di4, dj4); }

  void scanFrom(istream& strm) { for (int i = 0; i < (int)(H); ++i) for (int j = 0; j < (int)(W); ++j) strm >> G[i][j]; }

  void outputTo(ostream& strm) {
    for (int i = 0; i < (int)(H); ++i) {
      for (int j = 0; j < (int)(W); ++j) cout << G[i][j];
      strm << endl;
    }
  }
};

template <typename T>
inline istream& operator>>(istream& strm, Grid<T>& g) {
  g.scanFrom(strm);
  return strm;
}

template <typename T>
inline ostream& operator<<(ostream& strm, Grid<T>& g) {
  g.outputTo(strm);
  return strm;
}

int main() {
  int w, h;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;
    Grid<int> grid(h, w);
    cin >> grid;

    function<void(int, int)> dfs = [&](int i, int j) {
      grid[i][j] = 0;
      for (auto& p : grid.neighbor8(i, j)) {
        if (grid[p.i][p.j]) dfs(p.i, p.j);
      };
    };

    int ans = 0;
    for (int i = 0; i < (int)(h); ++i) for (int j = 0; j < (int)(w); ++j) {
      if (grid[i][j]) {
        dfs(i, j);
        ++ans;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

C++: #include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <utility>
#include <set>

class union_find {
  mutable std::vector<intmax_t> c;

public:
  union_find(size_t n): c(n, -1) {}

  size_t find(size_t v) const {
    if (c[v] < 0) return v;
    return (c[v] = find(c[v]));
  }

  bool unite(size_t u, size_t v) {
    u = find(u);
    v = find(v);
    if (u == v) return false;
    if (-c[u] > -c[v]) std::swap(u, v);
    c[v] += c[u];
    c[u] = v;
    return true;
  }

  bool connected(size_t u, size_t v) const { return (find(u) == find(v)); }

  size_t size() const { return c.size(); }
  size_t size(size_t v) const {
    return -c[find(v)];
  }
};

constexpr size_t m1 = -1;
constexpr size_t di[] = {m1, m1, m1, 0, 0, 1, 1, 1};
constexpr size_t dj[] = {m1, 0, 1, m1, 1, m1, 0, 1};

int testcase_ends() {
  size_t h, w;
  scanf("%zu %zu", &w, &h);
  if (h == 0 && w == 0) return 1;

  std::vector<std::vector<int>> c(h, std::vector<int>(w));
  for (auto& ci: c)
    for (auto& cij: ci) scanf("%d", &cij);

  union_find uf(h*w);
  for (size_t i = 0; i < h; ++i)
    for (size_t j = 0; j < w; ++j) {
      if (c[i][j] == 0) continue;
      for (size_t k = 0; k < 8; ++k) {
        size_t ni = i + di[k];
        size_t nj = j + dj[k];
        if (!(ni < h && nj < w)) continue;
        if (c[ni][nj] == 0) continue;
        uf.unite(i*w+j, ni*w+nj);
      }
    }

  std::set<size_t> r;
  for (size_t i = 0; i < h; ++i)
    for (size_t j = 0; j < w; ++j)
      if (c[i][j] == 1)
        r.insert(uf.find(i*w+j));

  printf("%zu\n", r.size());
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>

    using namespace std;

int map[50][50], width, height;
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}, dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

bool rangeCheck(int row, int col)
{
    if (row >= 0 && row <= height - 1 && col >= 0 && col <= width - 1)
        return true;
    else
    {
        return false;
    }
}

void erase(int row, int col)
{
    map[row][col] = 0;

    for (int i = 0; i < 8; i++)
    {
        if (rangeCheck(row + dy[i], col + dx[i]) == true && map[row + dy[i]][col + dx[i]] == 1)
            erase(row + dy[i], col + dx[i]);
    }
}

int main()
{

    int count;

    while (true)
    {
        scanf("%d %d", &width, &height);
        if (width == 0 && height == 0)
            break;

        for (int i = 0; i < height; i++)
        {
            for (int k = 0; k < width; k++)
            {
                scanf("%d", &map[i][k]);
            }
        }

        count = 0;

        for (int row = 0; row < height; row++)
        {
            for (int col = 0; col < width; col++)
            {
                if (map[row][col] == 1)
                {
                    count++;
                    erase(row, col);
                }
            }
        }

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

    return 0;
}