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++: 
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define veci vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vec>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))


using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
//template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
//template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
    for(T& x:vec) is >> x;
    return is;
}
const ll INF=1e9+7;

ll c[100][100];
ll w,h;

void dfs(ll y,ll x){
    c[y][x]=0;
    for(ll p=-1;p<=1;p++){
        for(ll q=-1;q<=1;q++){
            if(p==0 && q==0) continue;
            ll ny=y+p;
            ll nx=x+q;
            if(0<=ny && ny<=h-1 && 0<=nx && nx<=w-1 && c[ny][nx]==1){
                dfs(ny,nx);
            }
        }
    }
}

int main()
{
    while(true){
        cin >> w >> h;
        if(w==0 && h==0){
            break;
        }
        rep(i,h){
            rep(j,w){
                cin >> c[i][j];
            }
        }
        ll cnt=0;
        rep(i,h){
            rep(j,w){
                if(c[i][j]==1){
                    dfs(i,j);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>

using namespace std;
typedef std::vector<std::vector<int> > Field;
typedef std::vector<std::vector<bool> > Seen;

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

// DFS
bool dfs(const Field& field, Seen& seen, const int ih, const int iw, const int H, const int W) {
    seen[ih][iw] = true;
    if (field[ih][iw] == 0) return false;
    // search oct
    for (int i = 0; i < 8; ++i) {
        const int curh = ih + dx[i];
        const int curw = iw + dy[i];

        // check the boundaries
        if (curh >= H || curh < 0) continue;
        if (curw >= W || curw < 0) continue;

        if (seen[curh][curw]) continue;

        dfs(field, seen, curh, curw, H, W);
    }
    return true;
}

int search(const Field& field, const int H, const int W) {
    Seen seen(H, std::vector<bool>(W, false));
    int cnt = 0;
    for (int ih = 0; ih < H; ++ih) {
        for (int iw = 0; iw < W; ++iw) {
            if (seen[ih][iw]) continue;
            if (field[ih][iw] == 0) continue;
            if (dfs(field, seen, ih, iw, H, W)) ++cnt;
        }
    }
    return cnt;
}

int main() {
    vector<int> res;
    for (;;) {
        int W, H; cin >> W >> H;
        if ( (W == 0 || H == 0) ) break;
        Field field(H, std::vector<int>(W, 0));
        for (int ih = 0; ih < H; ++ih) {
            for (int iw = 0; iw < W; ++iw) {
                cin >> field[ih][iw];
            }
        }
        res.push_back(search(field, H, W));
    }
    for (unsigned int i = 0; i < res.size(); ++i) {
        cout << res[i] << "\n";
    }
    return 0;
}

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

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

void dfs(int y, int x)
{
	fld[y][x] = 0;
	for(int i=0; i<8; i++)
	{
		int ny = y+dy[i];
		int nx = x+dx[i];

		if(fld[ny][nx])
		{
			dfs(ny,nx);
		}
	}
}


int main()
{
	int w,h,c;
	while(cin >> w >> h, w||h)
	{
		memset(fld,0,sizeof(fld));

		for(int y=1; y<=h; y++)
		for(int x=1; x<=w; x++)
		{
			cin >> fld[y][x];
		}

		c=0;
		for(int y=1; y<=h; y++)
		for(int x=1; x<=w; x++)
		{
			if(fld[y][x])
			{
				c++;
				dfs(y,x);
			}
		}

		cout << c << endl;
	}
}

C++: 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>

using namespace std;

void dfs(int x, int y, int h, int w, vector<vector<int> > &f){
	
	f[x][y] = 0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			int x2 = x+i;
			int y2 = y+j;
			if((0<=x2 && x2<=h-1) && (0<=y2 && y2<=w-1)){
				if(f[x2][y2]) dfs(x2, y2, h, w, f);
			}
		}
	}
	
	return;
}

bool solve(){
	int w, h;
	cin>> w>> h;
	if(w==0 && h==0) return false;
	
	vector<vector<int> > field(h, vector<int>(w,0));
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			int c;
			cin>> c;
			field[i][j] += c;
		}
	}
	
	int ans = 0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(field[i][j]){
				dfs(i, j, h, w, field);
				ans++;
			}
		}
	}
	
	cout<< ans<< endl;
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	while(solve());

	return 0;
}