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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
	if (n == 0)return 1;
	if (n == 1)return x%MOD;
	ll res = sq(mod_pow(x, n / 2));
	res %= MOD;
	if (n % 2 == 1) {
		res *= x;
		res %= MOD;
	}
	return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

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

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

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

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

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<cstdlib>

#define LOOP(n,cnt) for(int cnt=0;cnt<n;++cnt)

using namespace std;

template <typename TYPE>
void printArray(TYPE* array, int size){
  LOOP(size,i) cout << array[i] << "\t" << flush;
  cout << endl;
}

int StringToInt(string str){
  return atoi(str.c_str());
}

#define MAX_W 50
#define MAX_H 50
int w,h,c[MAX_W][MAX_H];
int count = 0;

void dfs(int sy, int sx){
  for(int i=-1;i<=1;i++){
    for(int j=-1;j<=1;j++){
      int y = sy+i;
      int x = sx+j;
      if(sy>=0 && sx>=0 && sy<h && sx<w){
	if(c[y][x] == 1){
	  c[y][x] = 0;
	  dfs(y,x);         
	}
      }
    }
  }
}

int main(){
  while(true){
    cin >> w;
    cin >> h;
    count = 0;
    if(w == 0 && h == 0) break;
    LOOP(h,y){
      LOOP(w,x){
	cin >> c[y][x];
      }    
    }
    
    LOOP(h,y){
      LOOP(w,x){
	if(c[y][x] == 1){
	  count++;
	  dfs(y,x);
	}
      }    
    }
    cout << count << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

const ll d[8][2] = {{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}};

void dfs(const vector<vector<ll>> &c, vector<vector<ll>> &g, ll x, ll y, ll root) {
    if (g[y][x] != -1) return;
    if (c[y][x] == 0) return;
    g[y][x] = root;
    rep(i, 8) {
        ll nx = x + d[i][0];
        ll ny = y + d[i][1];
        if ((nx < 0) || (nx >= sz(c[0])) || (ny < 0) || (ny >= sz(c))) continue;
        dfs(c, g, nx, ny, root);
    }
}

void solve(ll w, ll h) {
    vector<vector<ll>> c(h, vector<ll>(w)), g(h, vector<ll>(w, -1));
    rep(i, h) rep(j, w) cin >> c[i][j];
    set<ll> s;
    rep(i, h) {
        rep(j, w) {
            dfs(c, g, j, i, i * w + j);
            if (g[i][j] >= 0) s.insert(g[i][j]);
        }
    }
    cout << sz(s) << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    while(true) {
        ll w, h;
        cin >> w >> h;
        if ((w == 0) && (h == 0)) break;
        solve(w, h);
    }
    return 0;
}