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>
using namespace std;
int w,h;
int masu[1010][1010];
int dx[] = {-1,-1,-1,0,0,1,1,1};
int dy[] = {-1,0,1,-1,1,-1,0,1};
void dfs(int y,int x){
    masu[y][x] = 0;
    for(int i=0;i<8;i++){
        int ny = y+dy[i];
        int nx = x+dx[i];
        if(ny<0||ny>=h||nx<0||nx>=w||masu[ny][nx]==0) continue;
        dfs(ny,nx);
    }
}
int main(){
    int ans;
    while(cin >> w >> h,w!=0||h!=0){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++) cin >> masu[i][j];
        }
        ans = 0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(masu[i][j] == 1){
                    ans++;
                    dfs(i,j);
                }
            }
        }
        cout << ans << endl;
    }
    return(0);
}

C++: #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
ll seen[55][55];
ll W, H;
void dfs(int h, int w) {
    seen[h][w] = 0;
    
    for (ll dir = 0; dir < 8; dir++) {
        ll nh = h + dx[dir];
        ll nw = w + dy[dir];
        if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
        if (seen[nh][nw] == 0) continue;
        
        
        dfs(nh, nw);
            
    }
}


int main(){
    while(1 > 0){
        ll ans = 0;
        cin >> W >> H;
        if(W == 0 && H == 0) break;

        for (ll i = 0; i < H; i++) {
            for (ll j = 0; j < W; j++) {
                cin >> seen[i][j];
            }
        }

        for (ll i = 0; i < H; i++) {
            for (ll j = 0; j < W; j++) {
                if(seen[i][j]){
                    dfs(i,j);
                    ans++ ;
                }
            }
        }
        printf("%lld\n", ans);
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

int w, h;
vector<int> ans;

void dfs(int x, int y, vector<vector<int>> &m, vector<vector<bool>> &seen);

int main()
{
    while (true)
    {
        cin >> w >> h;
        // 終了条件
        if ((w == 0) && (h == 0))
        {
            break;
        }
        vector<vector<int>> m(52, vector<int>(52, 0));
        vector<vector<bool>> seen(52, vector<bool>(52, false));
        // データの受け取り
        rep(1, i, h + 1)
        {
            rep(1, j, w + 1)
            {
                cin >> m.at(i).at(j);
            }
        }
        int count = 0;
        //すべての点で探索
        rep(1, i, h + 1)
        {
            rep(1, j, w + 1)
            {
                // 探索済か海のマスならとばす
                if (seen.at(i).at(j) || (m.at(i).at(j) == 0))
                {
                    continue;
                }
                // dfsによる塗りつぶし
                dfs(i, j, m, seen);
                count++;
            }
        }
        ans.push_back(count);
    }
    for (auto a : ans)
    {
        cout << a << endl;
    }
}

void dfs(int x, int y, vector<vector<int>> &m, vector<vector<bool>> &seen)
{
    // 探索済か海のマスなら探索終了
    if (seen.at(x).at(y) || (m.at(x).at(y) == 0))
    {
        return;
    }
    // 探索済の印をつける
    seen.at(x).at(y) = true;
    rep(-1, dx, 2)
    {
        rep(-1, dy, 2)
        {
            dfs(x + dx, y + dy, m, seen);
        }
    }
    return;
}

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

using namespace std;

#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
#define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}


typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.end()
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1000000007;
//const ll MOD = 998244353;
const double pi = 3.14159265358979323846;
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { 1,0, -1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);
//#define mp make_pair

int w, h;

void dfs(vii &a, int ii, int jj, int d) {
  a[ii][jj] = d;
  rep (k, 8) {
    if (in(ii + dx2[k], 0, h) && in(jj + dy2[k], 0, w) && a[ii + dx2[k]][jj + dy2[k]] == -1) {
      dfs(a, ii + dx2[k], jj + dy2[k], d);
    }
  }
}

int main() {
  while (true) {
    cin >> w >> h;
    if (h == 0) return 0;
    vii a(h, vi(w));
    rep (i, h) {
      rep (j, w) {
        cin >> a[i][j];
        a[i][j] *= -1;
      }
    }

    int ans = 0;
    rep (i, h) {
      rep (j, w) {
        if (a[i][j] == -1) {
          ans++;
          dfs(a, i, j, ans);
        }
      }
    }

    cout << ans << endl;
    
  }
}