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++: //#define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
constexpr int PI = (2*acos(0.0));
constexpr int EPS = (1e-9);
constexpr int MOD = (int)(1e9+7);
constexpr int INF = 100000000;
#pragma endregion

int w, h;
int a[51][51];
int dx[]{1,-1,0,0,1,-1,1,-1};
int dy[]{0,0,1,-1,1,-1,-1,1};

void dfs(int x, int y) {
    a[x][y] = 0;
    rep(i, 8) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(nx>=0&&nx<h&&ny>=0&&ny<w&&a[nx][ny]==1) {
            dfs(nx, ny);
        }
    }
}

int main()
{
    while(cin>>w>>h, w) {
        rep(i,h) rep(j,w) a[i][j] = 0;

        rep(i,h) {
            rep(j,w) {
                cin >> a[i][j];
            }
        }

        int ans = 0;
        rep(i,h) {
            rep(j, w) {
                if(a[i][j]==1) {
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

int res, land[1080][1080];
int xs[] = {1, 1, 0, -1, -1, -1, 0, 1};
int ys[] = {0, 1, 1, 1, 0, -1, -1, -1};
void dfs(int x, int y){
	land[x][y] = 0;
	for(int i = 0;i < 8;i++){
		int tx = x + xs[i];
		int ty = y + ys[i];
		if(land[tx][ty])dfs(tx, ty);
	}
}

int main(){
	int w, h;
	while(cin >> h >> w, w | h){
		res = 0;
		for(int i = 1;i <= w;i++){
			for(int j = 1;j <= h;j++){
				cin >> land[i][j];
			}
		}
		for(int i = 1;i <= w;i++){
			for(int j = 1;j <= h;j++){
				if(land[i][j]){
					res++;
					dfs(i, j);
				}
			}
		}
		cout << res<< endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int map[60][60];
int main(void) {
	vector<int> list;
	for (;;) {
		int w, h;
		cin >> w >> h;
		if (!(w&&h))return 0;
		int cnt = 1;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				int buf;
				cin >> buf;
				map[i][j] = (buf?cnt++:0);
			}
		}
		bool update = true;
		for (;update;){
			cnt = 0;
			update = false;
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					if (!map[i][j])continue;
					for (int n = max(0, i - 1); n <= min(h-1, i + 1); n++) {
						for (int m = max(0, j - 1); m <= min(w-1, j + 1); m++) {
							if (!map[n][m])continue;
							if (map[i][j]>map[n][m]) {
								map[i][j] = map[n][m];
								update = true;
							}
						}
					}
				}
			}
		}
		vector<int> num;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				num.push_back(map[i][j]);
			}
		}
		sort(num.begin(), num.end());
		num.erase(unique(num.begin(),num.end()),num.end());
		cout << num.size()-1<<endl;
	}
	return 0;
}

C++: #include<queue>
#include<cstdio>
#include<utility>
using namespace std;
typedef pair<int, int> P;
int main() {
	int W, H;
	int i, j, k;
	int map[50][50];
	int vector[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
	int count;
	queue<P> q;
	while (1) {
		scanf("%d %d", &W, &H);
		if (!(W||H)) break;
		count = 0;
		for (i = 0; i < H; i ++) for (j = 0; j < W; j ++) scanf("%d", &map[i][j]);
		for (i = 0; i < H; i ++) {
			for (j = 0; j < W; j ++) {
				if (map[i][j]) {
					map[i][j] = 0; count ++;
					q.push(P(i, j));
					while (!q.empty()) {
						P p = q.front(); q.pop();
						int x, y;
						for (k = 0; k < 8; k ++) {
							x = p.first + vector[k][0]; y = p.second + vector[k][1];
							if (-1 < x && x < H && -1 < y && y < W && map[x][y]) {
								q.push(P(x, y));
								map[x][y] = 0;
							}
						}
					}
				}
			}
		}
		printf("%d\n", count);
	}
	return 0;
}