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;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;

bool c[51][51];
bool checked[51][51];
int w, h;

void dfs(int y, int x) {
    if (checked[y][x] || !c[y][x]) return;
    checked[y][x] = true;
    repd(i, -1, 2) {
        repd(j, -1, 2) {
            if (i == 0 && j == 0) continue;
            int yy = y + i, xx = x + j;
            if (yy < 0 || yy >= h || xx < 0 || xx >= w) continue;
            dfs(yy, xx);
        }
    }
}

int main()
{
    while (1) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(i, h) {
            rep(j, w) {
                int k;
                cin >> k;
                c[i][j] = k;
                checked[i][j] = false;
            }
        }

        int cnt = 0;
        rep(i, h) {
            rep(j, w) {
                if (!checked[i][j] && c[i][j]) {
                    dfs(i, j);
                    cnt++;
                }
            }
        }

        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string>
#include<limits.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define nrep(i,a,b) for(int i=a;i<b;i++)
#define mrep(i,a) for(int i=a;i<=0;i++)
#define INF LLONG_MAX
using namespace std;

ll w, h, ans;
vector<pair<ll, ll> > idou(8);

void dfs(vector<vector<ll> > &map, ll tate, ll yoko) {
	map[tate][yoko] = ans;
	rep(i, 8) {
		ll t = tate + idou[i].first;
		ll y = yoko + idou[i].second;
		if (0 <= t && t < h && 0 <= y && y < w && !map[t][y]) dfs(map, t, y);
	}
}

int main() {
	//???
	idou[0].first = -1;
	idou[0].second = 0;
	//??????
	idou[1].first = -1;
	idou[1].second = 1;
	//???
	idou[2].first = 0;
	idou[2].second = 1;
	//??????
	idou[3].first = 1;
	idou[3].second = 1;
	//???
	idou[4].first = 1;
	idou[4].second = 0;
	//??????
	idou[5].first = 1;
	idou[5].second = -1;
	//???
	idou[6].first = 0;
	idou[6].second = -1;
	//??????
	idou[7].first = -1;
	idou[7].second = -1;

	while (1) {
		cin >> w >> h;
		if (!w && !h) break;
		ans = 0;
		vector<vector<ll> > map(h, vector<ll>(w, -1));
		rep(i, h) rep(j, w) {
			ll temp; cin >> temp;
			if (temp) map[i][j] = 0;
		}rep(i, h) rep(j, w) {
			if (!map[i][j]) {
				ans++;
				dfs(map, i, j);
			}
		}cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

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

int w, h;
int map[50][50];
int ans;

bool iswalkable(int ty,int tx){
	if(ty >= 0 && ty < h && tx >= 0 && tx < w) return true;
	return false;
}

void dfs(int y, int x){
	if(map[y][x] == 0) return;
	map[y][x] = 0;
	
	for(int vy = -1; vy <= 1; vy++){
		for(int vx = -1; vx <= 1; vx++){
			
			if(vy == 0 && vx == 0) continue;
			int ty = y + vy;
			int tx = x + vx;
			if(iswalkable(ty, tx)){
				dfs(ty, tx);
			}
			
		}
	}
	return;
}
			
	
int main(int argc, char const* argv[])
{
	
	while(true){
		cin >> w >> h;
		if(w == 0 && h == 0) break;

		ans = 0;
		rep(i, h) rep(j, w) cin >> map[i][j];

		rep(i, h){
			rep(j, w){
				if(map[i][j]){
					ans++;
					dfs(i, j);
				}
			}
		}


		cout << ans << endl;
	}
	
	return 0;
}