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 <iostream>
#include <vector>
using namespace std;

int w, h;
int C[100][100];
bool R[100][100];

void dfs_visit(int h, int w, int y, int x){
    if(y < 0 || h <= y || x < 0 || w <= x || C[y][x] == 0){
        return;
    }
    
    if(R[y][x]){
        return;
    }
    
    R[y][x] = true;
    
    dfs_visit(h, w, y-1, x-1);
    dfs_visit(h, w, y-1, x);
    dfs_visit(h, w, y-1, x+1);
    dfs_visit(h, w, y, x-1);
    dfs_visit(h, w, y, x+1);
    dfs_visit(h, w, y+1, x-1);
    dfs_visit(h, w, y+1, x);
    dfs_visit(h, w, y+1, x+1);
}

int dfs(int h, int w){
    int cnt = 0;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(C[i][j] == 1 && !R[i][j]){
                cnt++;
                dfs_visit(h, w, i, j);
            }
        }
    }
    
    return cnt;
}


int main(void){

    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0) break;

        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                C[i][j] = 0;
                R[i][j] = false;
                cin >> C[i][j];
            }
        }
        
        cout << dfs(h, w) << endl;
    }
    
    return 0;

    
}

C++: #include <iostream>
#include <vector>
#include <list>
#include <set>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>

using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
using pii=pair<int,int>;
using vs=vector<string>;
using vvi=vector<vi>;
using vpii=vector<pii>;
using si=set<int>;

#define REP(i,a) for(int i=0;i<a;i++)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define rREP(i,a) for(int i=(a)-1;i>=0;i--)
#define rFOR(i,a,b) for(int i=(a)-1;i>=(b);i--)
#define out(S) cout<<(S)<<endl;
#define beginend(v) (v).begin(),(v).end()
#define IfOut(condition,text) if(condition){out(text);return 0;}
#define IfOutElse(condition,if_text,else_text) if(condition){out(if_text);return 0;}else{out(else_text);return 0;}
#define mod(s) ((s)%(ll)(1e9+7))
ll modpow(ll i, ll j) { ll tmp = 1; while (j) { if (j % 2)tmp = mod(tmp*i); i = mod(i*i); j /= 2; }return tmp; }
#define divmod(a,b) (mod(a*modpow((ll)b,(ll)(1e9+5))))
#define pb(i) push_back(i)
#define fst first
#define snd second
#define Foreach(item,collection) for(auto item:collection)
#define INF (int)1e10
#define removeAll(vec,deleg) auto itr=remove_if(beginend(vec),deleg);vec.erase(itr,vec.end());
void removeAt(string& s, int index) { s.erase(index, 1); }
template<typename T> void removeAt(vector<T>& v, int index) { v.erase(v.begin() + index); }
ll manhattanDistance(ll x1, ll y1, ll x2, ll y2) { return (abs(x2 - x1) + abs(y2 - y1)); }
vector<ll> sieveOfEratosthenes(int max) {ll prime;double sqrtmax = sqrt(max);vector<ll> primeVec, searchVec;FOR(i, 2, max + 1) searchVec.push_back(i);do{prime = searchVec[0];primeVec.push_back(prime);auto itr = remove_if(beginend(searchVec), [=](ll x)->bool {return x%prime == 0; });searchVec.erase(itr, searchVec.end());} while (prime < sqrtmax);primeVec.reserve(primeVec.size() + searchVec.size());primeVec.insert(primeVec.end(), beginend(searchVec));return primeVec;}
ll sum(vi v) {ll sum=0;Foreach(i,v){sum+=i;}return sum;}
#define ShowAll(v) Foreach(i,v) out(i);
bool isInnerIndex(ll x,ll y,ll mx,ll my){ return (x>=0&&x<mx&&y>=0&&y<my); }

int x,y;
vvi box;
int dx[]{0,1,0,-1,1,1,-1,-1};
int dy[]{1,0,-1,0,-1,1,-1,1};
ll s=0;
void dfs(int px,int py){
    out("--------------");
    REP(i,8){
        int nx=px+dx[i];
        int ny=py+dy[i];
        if(isInnerIndex(nx,ny,x,y)&&box[ny][nx]==1){
            dfs(nx,ny);
        }
    }
    return;
}

int main(){
    vi ans;
    while (cin>>x>>y,(x||y))
    {
        box.clear();
        box.resize(y);
        REP(i,y) REP(n,x) {
            int c;
            cin>>c;
            box[i].pb(c);
        }
        ll s=0;
        REP(i,y)REP(n,x){
            if(box[i][n]==1) {dfs(n,i);s++;}
        }
        ans.pb(s);
    }
    
    ShowAll(ans);
    return 0;
}