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 <cstdio>
#include <algorithm>
using namespace std;

int w, h, c[50][52];

int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

void dfs(int x, int y){
    c[x][y] = 0;
    for(int z = 0; z < 8; z++){
        int nx = x + dx[z], ny = y + dy[z];
        if( 0 <= nx && nx < h && 0 <= ny && ny < w &&
            c[nx][ny] ){
            dfs(nx, ny);
        }
    }
}

int main(){
    for(; scanf("%d%d", &w, &h), w||h;){
        for(int x = 0; x < h; x++){
            for(int y = 0; y < w; y++){
                scanf("%d", &c[x][y]);
            }
        }
        int ans = 0;
        for(int x = 0; x < h; x++){
            for(int y = 0; y < w; y++){
                if( c[x][y] ){
                    ++ans;
                    dfs(x, y);
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

C++: #include<bits/stdc++.h>

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


bool landfill(int x, int y, int w, int h, vector<vector<int> > &land) {
    if (x < 0 || y < 0 || x >= h || y >= w) return false;
    if (land.at(x).at(y) == 0) return false;
    land.at(x).at(y) = 0;
    return landfill(x, y + 1, w, h, land) || landfill(x, y - 1, w, h, land) ||
           landfill(x + 1, y, w, h, land) || landfill(x - 1, y, w, h, land) ||
           landfill(x - 1, y + 1, w, h, land) || landfill(x - 1, y - 1, w, h, land) ||
           landfill(x + 1, y + 1, w, h, land) || landfill(x + 1, y - 1, w, h, land);
}

int main() {
    vector<int> ans_union;
    do{
        int w, h;
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        vector<vector<int> > land(h, vector<int>(w));

        int ans = 0;
        rep(i, h) rep(j, w) {
                cin >> land.at(i).at(j);
            }

        rep(i, h) rep(j, w) {
                if (land.at(i).at(j) == 1) {
                    ans++;
                    landfill(i, j, w, h, land);
                }
            }
        ans_union.push_back(ans);
    }while(1);
    for(int i = 0; i < ans_union.size(); i++){
        cout << ans_union.at(i) << endl;
    }

    return 0;

}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <memory.h>
#include <functional>

using namespace std;

//FILE * in = freopen("B", "r", stdin);

typedef pair<int,int> P;

int n, m;
int cnt = 0;
int data[100][100];
int check[100][100];

int mvx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int mvy[] = {-1, -1, -1, 0, 0, 1, 1, 1};

void input() {
	for(int i = 0; i < m; i++){
		for(int j = 0; j < n; j++) {
			scanf("%d", &data[i][j]);
		}
	}
	cnt = 0;
	memset(check, 0, sizeof(check));
}

void bfs(int x, int y) {
	queue<P> que;
	que.push(P(x,y));
	check[x][y] = 1;
	while(!que.empty()){
		P cur = que.front(); que.pop();
		for(int i = 0; i < 8; i++) {
			if(cur.first + mvx[i] >= 0 && cur.first + mvx[i] < m && cur.second + mvy[i] >= 0 && cur.second + mvy[i] < n
				&& data[cur.first + mvx[i]][cur.second + mvy[i]] && !check[cur.first+mvx[i]][cur.second+mvy[i]]){
					check[cur.first +mvx[i]][cur.second+mvy[i]] = 1;
					que.push(P(cur.first+mvx[i], cur.second+mvy[i]));
			}
		}
	}
}

void process() {
	for(int i = 0; i < m; i++) {
		for(int j = 0; j < n; j++) {
			if(data[i][j] && !check[i][j]) {
				bfs(i,j);
				++cnt;
			}
		}
	}
	printf("%d\n", cnt);
}

int main() {
	while(true) {
		scanf("%d%d", &n, &m);
		if(n == 0) break;
		input();
		process();
	}
	return 0;
}