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 <deque>
#include <cstdio>
using namespace std;

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

int mesh[50][50];
int n, m;

struct Point
{
	int x;
	int y;
};

int main()
{
	deque<Point> stack;
	while (true)
	{
		scanf("%d %d", &n, &m);
		if (n == 0 && m == 0) break;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				int a;
				scanf("%d", &a);
				mesh[i][j] = a;
			}
		}
		int count = 0;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				if (mesh[i][j] == 1)
				{
					Point pt = {j, i};
					stack.push_front(pt);
					mesh[i][j] = 0;
					while (!stack.empty())
					{
						Point point = stack.front();
						stack.pop_front();
						for (int k = 0; k < 8; k++)
						{
							if (mesh[point.y+dy[k]][point.x+dx[k]] == 1)
							{
								Point pt = {j + dx[k], i + dy[k]};
								stack.push_front(pt);
								mesh[point.y+dy[k]][point.x+dx[k]] = 0;
							}
						}
					}
					count++;
				}
			}
		}
		printf("%d\n", count);
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define Rep(i, a, b) for(int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(),(v).end()
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;

ll H, W;
bool c[100][100];
bool seen[510][510];
const int dx[8] = {0, 0, -1, 1, -1, 1, 1, -1};
const int dy[8] = {1, -1, 0, 0, 1, 1, -1, -1}; 

void dfs(ll h, ll w){
    seen[h][w] = true;

    ll nh, nw;
    rep(i, 0, 8){
        nh = h + dy[i];
        nw = w + dx[i];

        if(nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
        if(seen[nh][nw]) continue;

        if(c[nh][nw]) dfs(nh, nw);
    }
}

void solve() {
    while(cin >> W >> H){
        if(W == 0 && H == 0) break;

        rep(h, 0, H) rep(w, 0, W) cin >> c[h][w];
        rep(h, 0, H) rep(w, 0, W) seen[h][w] = 0;

        ll ans = 0;
        rep(h, 0, H) rep(w, 0, W) { 
            if(c[h][w] && !(seen[h][w])){
                dfs(h, w);
                ans++;
            }
        }
        
        cout << ans << endl;
    }

    return;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    solve();
    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

void dfs(int cur_x, int cur_y, vector<vector<int> >& c, vector< vector<bool> >& visited, int H, int W) {
    visited[cur_y][cur_x] = true;
    for(int dx=-1; dx<=1; dx++)
        for(int dy=-1; dy<=1; dy++)
            if(
            (dx !=0 || dy != 0)
            && (cur_x+dx>=0)
            && (cur_x+dx<W)
            && (cur_y+dy>=0)
            && (cur_y+dy<H)
            && c[cur_y+dy][cur_x+dx] == 1
            && !visited[cur_y+dy][cur_x+dx]) {
                dfs(cur_x+dx, cur_y+dy, c, visited, H, W);
            }
}

int main() {
    while(1) {
        int W, H;
        cin >> W >> H;
        if(W==0 && H==0) break;
        vector<vector<int> > c(H, vector<int>(W));
        vector<vector<bool> > visited(H, vector<bool>(W, false));
        REP(i, H)
            REP(j, W) cin >> c[i][j];

        int ans = 0;
        REP(i, H)
            REP(j, W)
                if(!visited[i][j] && c[i][j] == 1) {
                    ans++;
                    dfs(j, i, c, visited, H, W);
                }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
 
#define rep(i, n) for(int i=0; i<(n); ++i)

int w, h;
int c[51][51];
int island[51][51];
int ans;
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};

void dfs(int y, int x, int id) {
    island[y][x] = id;
    rep(i, 8) {
        int nx = x+dx[i];
        int ny = y+dy[i];
        if(nx>=0 && nx<w && ny>=0 && ny<h &&
                c[ny][nx] == 1 && island[ny][nx] != id) {
            dfs(ny, nx, id);
        }
    }
}
    
int main(void){
    while(cin >> w >> h && w) {
        rep(y, h) rep(x, w) cin >> c[y][x];
        ans = 0;
        memset(island, -1, sizeof(island));
        rep(y, h) {
            rep(x, w) {
                if(c[y][x]==1 && island[y][x]==-1) {
                    dfs(y, x, ans);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}