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

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

int A[51][51];
int member[51][51];
int count = 0;

void check_member(int i,int j,int h, int w){
    if(member[i][j] == 0 && A[i][j] == 1 && i <= h && j <= w){
        member[i][j] = count;
        check_member(  i,  j+1 , h , w);
        check_member(i+1 , j+1 , h , w);
        check_member(i+1 ,   j , h , w);
        check_member(i+1 , j-1 , h , w);
        check_member(  i , j-1 , h , w);
        check_member(i-1 , j-1 , h , w);
        check_member(i-1 ,   j , h , w);
        check_member(i-1 , j+1 , h , w);
    }else{
        return;
    }
}


int main(){
    vector<int> result;
    int h,w;
    while(cin >> w >> h){
        if(h == 0 && w == 0) break;
        count = 0;
        for(int i = 0; i <= h; i++){
            for(int j = 0; j <= w; j++){
                if(i > 0 && j > 0){
                    cin >> A[i][j];
                }else{
                    A[i][j] = 0;
                }
                member[i][j] = 0;
            }
        }
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                if(A[i][j]==1 && member[i][j] == 0){
                    count++;
                    check_member(i,j,h,w);
                }
            }
        }
        result.push_back(count);
    }
    for(int i = 0; i < result.size(); i++){
        cout << result[i] << endl;
    }
}