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 <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;

#define REP(i, a, b) for(ll i=(a); i<(b); i++)
#define PER(i, a, b) for(ll i=(a); i>=(b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF=1e18+18;
const ll MAX=100005;
const ll MOD=1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a,b)
#define Each(a,b) for(auto &a :b)
#define REPM(i, mp) for (auto i = mp.begin(); i != mp.end(); ++i)
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) cerr << #mp << ":"<<endl; for (auto i = mp.begin(); i != mp.end(); ++i) { cerr << i->first <<":"<<i->second << endl;}
#define dbgarr(n,m,arr) rep(i,n){rep(j,m){cerr<<arr[i][j]<<" ";}cerr<<endl;}
#define dbgdp(n,arr) rep(i,n){cerr<<arr[i]<<" ";}cerr<<endl;
#define dbgmint(n,arr) rep(i,n){cerr<<arr[i].x<<" ";}cerr<<endl;
#define out(a) cout<<a<<endl
#define out2(a,b) cout<<a<<" "<<b<<endl
#define vout(v) rep(i,v.size()){cout<<v[i]<<" ";}cout<<endl
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }

template<typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s<<"("<<p.first<<", "<<p.second<<")"; }

template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,v.size())i>>v[j];return i;}

// vector
template<typename T>
ostream &operator<<(ostream &s, const vector<T> &v) {
    int len=v.size();
    for(int i=0; i<len; ++i) {
        s<<v[i];
        if(i<len-1) s<<"	";
    }
    return s;
}

// 2 dimentional vector
template<typename T>
ostream &operator<<(ostream &s, const vector<vector<T> > &vv) {
    int len=vv.size();
    for(int i=0; i<len; ++i) {
        s<<vv[i]<<endl;
    }
    return s;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<std::setprecision(10);

    while(true){
        ll ans=0;
        ll w,h;
        cin>>w>>h;
        if(w==0&&h==0)break;
        VV g(h+2,Vec(w+2));
        VV d(h+2,Vec(w+2,-1));
        rep(i,h){
            rep(j,w){
                cin>>g[i+1][j+1];
            }
        }
        Vec dx = {1,0,-1,0,1,1,-1,-1};
        Vec dy = {0,1,0,-1,1,-1,1,-1};
        rep(i,h){
            rep(j,w){
                if(g[i+1][j+1]==0)continue;
                if(d[i+1][j+1]==-1){
                    VV visited(h+2,Vec(w+2));
                    auto dfs = [&](auto dfs,ll y,ll x)->void{
                        visited[y][x] = 1;
                        rep(i,8){
                            ll nx = x + dx[i];
                            ll ny = y + dy[i];
                            if(g[ny][nx]==1 && visited[ny][nx]==0){
                                d[ny][nx] = d[y][x]+1;
                                dfs(dfs,ny,nx);
                            }
                        }
                        //visited[y][x] = 0;
                    };
                    ans++;
                    d[i+1][j+1] = 1;
                    dfs(dfs,i+1,j+1);
                }
            }
        }
        cout<<ans<<endl;
    }
    return  0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int inf = 1e9;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, b, a) for (int i = a - 1; i >= b; i--)
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dxx[8] = {1, 1, 1, 0, 0, -1, -1, -1}, dyy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

bool seen[55][55];
int ans = 0;
int c[55][55];

bool dfs (int y, int x, int w, int h) {
    if (seen[y][x] || c[y][x] == 0) return false;
    seen[y][x] = true;
    rep (i, 0, 8) {
        int ny = y + dyy[i], nx = x + dxx[i];
        if (ny >= 0 && ny < h && nx >= 0 && nx < w && !seen[ny][nx] &&
            c[ny][nx] == 1) {
            dfs (ny, nx, w, h);
        }
    }
    return true;
}

int main() {
    int w, h;
    while (1) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep (i, 0, h) rep (j, 0, w) cin >> c[i][j];
        rep (i, 0, h) rep (j, 0, w) seen[i][j] = 0;
        int ans = 0;
        rep (i, 0, h) {
            rep (j, 0, w) {
                if (dfs (i, j, w, h)) {
                    // cout << i << " " << j << "\n";
                    ans++;
                }
            }
        }
        cout << ans << "\n";
    }
}

C++: #include<iostream>
using namespace std;
int dx[]={1,0,-1,0,1,-1,-1,1};
int dy[]={0,1,0,-1,1,-1,1,-1};
int field[150][150];
void dfs(int x, int y);
int main(){
  int h,w;
  while(cin >>h>>w,h||w){
    for(int i=0; i<150; i++) for(int j=0; j<150; j++) field[i][j] = 0;
    for(int i=0; i<w; i++) for(int j=0; j<h; j++) cin >>field[i+50][j+50];
    int ans = 0;
    for(int i=0; i<w; i++){
      for(int j=0; j<h; j++){
	if(field[i+50][j+50] == 1){
	  dfs(i+50,j+50);
	  ans++;
	}
      }
    }
    cout <<ans<<endl;
  }
  return 0;
}
void dfs(int x, int y){
  field[x][y] = 0;
  for(int i=0; i<8; i++) if(field[x+dx[i]][y+dy[i]] == 1) dfs(x+dx[i],y+dy[i]);
}