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<queue>
using namespace std;

struct mapst {
	int x, y;
};

#define func(x,y) (c[y][x]&&!f[y][x])

int main() {
	int w, h, ans=0;
	int c[52][52]={{0}}, f[52][52]={{1}};
	int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
	int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
	queue<mapst> q;
	while(1) {
		cin >> w >> h;
		if(!w&&!h) break;
		ans = 0;
		for(int i=0; i<52; ++i) for(int j=0; j<52; ++j) f[i][j] = 1;
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			cin >> c[i][j];
			f[i][j] = 0;
		}
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			if(func(j,i)) {
				mapst m = {j, i}, ms;
				q.push(m);
				ans++;
				do {
					m = q.front();
					q.pop();
					f[m.y][m.x] = 1;
					for(int k=0; k<8; k++) {
						if(func(m.x+dx[k],m.y+dy[k])) {
							ms.x = m.x+dx[k]; ms.y = m.y+dy[k];
							q.push(ms);
						}
					}
				}while(!q.empty());
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
#define INF 100000000
#define IINF 9999999999999;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

bool earth[51][51];
ofstream outputfile("output.txt");
int depth = 0;
int inputw, inputh;

void erase_island(int h, int w) {
    //++depth;
    //outputfile << depth << endl;
    earth[h][w] = false;
    if(h != 0 && w != 0) {
        if (earth[h - 1][w - 1]) erase_island(h - 1, w - 1);
    }
    if(h != 0 && w != inputw - 1) {
        if (earth[h - 1][w]) erase_island(h - 1, w);
    }
    if(h != 0) {
        if(earth[h - 1][w + 1]) erase_island(h - 1, w + 1);
    }
    if(w != 0 && h != inputh - 1) {
        if(earth[h + 1][w - 1]) erase_island(h + 1, w - 1);
    }
    if(w != 0) {
        if(earth[h][w - 1]) erase_island(h, w - 1);
    }
    if(h != inputh - 1 && w != inputw - 1) {
        if(earth[h + 1][w + 1]) erase_island(h + 1, w + 1);
    }
    if(h != inputh - 1) {
        if(earth[h + 1][w]) erase_island(h + 1, w);
    }
    if(w != inputw - 1) {
        if(earth[h][w + 1]) erase_island(h, w + 1);
    }
    //cout << "# " << h << " " << w << endl;
    depth = 0;
}

int main() {
    while(cin >> inputw >> inputh, inputw + inputh) {
        int temp;
        rep(i, inputh) {
            rep(j, inputw) {
                cin >> temp;
                if(temp == 0) earth[i][j] = false;
                else earth[i][j] = true;
            }
        }

        int ans = 0;
        rep(i, inputh) {
            rep(j, inputw) {
                if(earth[i][j]) {
                    erase_island(i, j);
                    ++ans;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

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

const int ALIVE = 1, DEAD = 2;
int h, w;
vector<vector<int>> field;
bool out_field(int i, int j) { return i < 0 || i >= h || j < 0 || j >= w; }

void dfs(int i, int j) {
    field[i][j] = DEAD;
    for (int di: {-1, 0, 1}) for (int dj: {-1, 0, 1}) {
        int ni = i + di, nj = j + dj;
        if (out_field(ni, nj) || field[ni][nj] != ALIVE) continue;
        dfs(ni, nj);
    }
}

int main() {
    while (cin >> w >> h, w) {
        field.assign(h, vector<int>(w));
        for (auto &row: field) for (auto &c: row) cin >> c;
        int cnt = 0;
        for (int si = 0; si < h; si++) for (int sj = 0; sj < w; sj++) {
            if (field[si][sj] != ALIVE) continue;
            cnt++;
            dfs(si, sj);
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <math.h>
#include <complex>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <functional>
#include <assert.h>
using ll=long long;
#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define SZ(x) ((int)x.size())
#define fi first
#define se second
const int INF = 1e15;
using namespace std;

template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;

using pi=pair<int,int>;
using vi=vc<int>;

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

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

int h,w,cnt;

void dfs(vc<vc<char>> &C,int hh,int ww){
    rep(k,8){
        int a=hh+dx[k],b=ww+dy[k];
        if(a<h && a>=0 && b<w && b>=0){
            if(C[a][b]=='1'){
                C[a][b]='0';
                dfs(C,a,b);
            }
        }
    }
}

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout<<fixed<<setprecision(20);

    while(true){
        cin >> w >> h;
        cnt=0;
        if(h==0 && w==0) break;
        vc<vc<char>> c(h,vc<char>(w));
        rep(i,h) rep(j,w) cin >> c[i][j];
        rep(i,h){
            rep(j,w){
                if(c[i][j]=='1'){
                    cnt++;
                    c[i][j]='0';
                    dfs(c,i,j);
                }
            }
        }
        cout << cnt << "\n";
    }
}