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 <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <sstream>
#include <numeric>
#include <set>
#include <map>

#define rep(i, j) for(int i = 0; i < j; i++)
#define all(i) i.begin(), i.end()
#define ll long long
#define bl bool
#define nn printf("\n");
#define mod 1000000007

using namespace std;

int w, h, cnt = 0;

void srch(int x, int y, vector<vector<int>> &inp, vector < vector <bool>> &hit)
{
	if (x < 0 || w <= x || y < 0 || h <= y || inp[y][x] == 0) { return; }
	if (hit[y][x]) return;
	hit[y][x] = true;
	cnt += 1;
	srch(x + 1, y, inp, hit);
	srch(x - 1, y, inp, hit);
	srch(x, y + 1, inp, hit);
	srch(x, y - 1, inp, hit);
	srch(x + 1, y + 1, inp, hit);
	srch(x + 1, y - 1, inp, hit);
	srch(x - 1, y + 1, inp, hit);
	srch(x - 1, y - 1, inp, hit);
}
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (cin >> w >> h && w != 0 && h != 0)
	{
		vector<vector<int>> inp(h, vector<int>(w, 0));
		vector<vector<bool>> hit(h, vector<bool>(w, false));
		int ans = 0;
		rep(y, h) rep(x, w){ cin >> inp[y][x]; }
		rep(y, h) rep(x, w)
		{
			srch(x, y, inp, hit);
			if (cnt > 0) { ans += 1; }
			cnt = 0;
		}
		printf("%d\n", ans);
	}

	return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

int w,h,res;
vector<vector<int> > map;
vector<vector<bool> > visited;
int dx[8] = {0,1,0,-1,1,1,-1,-1};
int dy[8] = {1,0,-1,0,1,-1,1,-1};

void bfs(int y,int x){
	if(y >= h || y < 0 || x >= w || x < 0)return;
	if(map[y][x] == 0)return;
	if(visited[y][x])return;
	visited[y][x] = true;
	for(int i=0;i<8;++i){
		bfs(y+dy[i],x+dx[i]);
	}
}

int main(void){
	while(1){
		res = 0;
		cin >> w >> h;
		if(w == 0 && h == 0)break;
		map.resize(h);
		visited.resize(h);
		for(int i=0;i<h;++i){
			map[i].resize(w);
			visited[i].resize(w);
			for(int j=0;j<w;++j){
				visited[i][j] = false;
				cin >> map[i][j];
			}
		}
		for(int i=0;i<h;++i){
			for(int j=0;j<w;++j){
				if(map[i][j] == 1&& visited[i][j] == false){
					bfs(i,j);
					res++;
				}
			}
		}
		cout << res << endl;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;

struct UnionFind {
        int n;
        vector<int> parent;
        vector<int> rank;
        vector<int> num;
        int find(int x) {
                if (parent[x] == x) return  x;
                return parent[x] = find(parent[x]);
        }
        UnionFind(int n_) {
                n = n_;
                parent.resize(n);
                for (int i = 0; i < n; i ++) parent[i] = i;
                rank.assign(n, 0);
                num.assign(n, 1);
        }
        void unite(int x, int y) {
                if ((x = find(x)) != (y = find(y))) {
                        if (rank[x] < rank[y]) {
                                parent[x] = y;
                                num[y] += num[x];
                        } else {
                                parent[y] = x;
                                if (rank[x] == rank[y]) rank[x] ++;
                                num[x] += num[y];
                        }
                        n --;
                }
        }
        bool same(int x, int y) { return find(x) == find(y); }
        int get() { return n; }
        int get(int x) { return num[find(x)]; }
};

int main() {
        int w, h;
        while (scanf("%d%d", &w, &h), w) {
                vector<vector<int>> s(h, vector<int> (w));
                int lands = 0;
                map<pair<int, int>, int> ptol;
                for (int i = 0; i < h; i ++) {
                        for (int j = 0; j < w; j ++){
                                scanf("%d", &s[i][j]);
                                if (s[i][j] == 1) {
                                        ptol[make_pair(i, j)] = lands;
                                        lands ++;
                                }
                        }
                }
                UnionFind uf(lands);
                //cerr << lands << endl;
                for (int i = 0; i < h - 1; i ++) {
                        for (int j = 0; j < w - 1; j ++) {
                                if (s[i][j] == 1) {
                                        if (s[i + 1][j] == 1) uf.unite(ptol[make_pair(i, j)], ptol[make_pair(i + 1, j)]);
                                        if (s[i + 1][j + 1] == 1) uf.unite(ptol[make_pair(i, j)], ptol[make_pair(i + 1, j + 1)]);
                                        if (s[i][j + 1] == 1) uf.unite(ptol[make_pair(i, j)], ptol[make_pair(i, j + 1)]);
                                }
                        }
                }
                for (int i = 0; i < h - 1; i ++) {
                        if (s[i][w - 1] == 1 && s[i + 1][w - 1] == 1) uf.unite(ptol[make_pair(i, w - 1)], ptol[make_pair(i + 1, w - 1)]);
                        if (w > 1 && s[i][w - 1] == 1 && s[i + 1][w - 2] == 1) uf.unite(ptol[make_pair(i, w - 1)], ptol[make_pair(i + 1, w - 2)]);
                }
                for (int j = 0; j < w - 1; j ++) {
                        if (s[h - 1][j] == 1 && s[h - 1][j + 1] == 1) uf.unite(ptol[make_pair(h - 1, j)], ptol[make_pair(h - 1, j + 1)]);
                        if (h > 1 && s[h - 1][j] == 1 && s[h - 2][j + 1] == 1) uf.unite(ptol[make_pair(h - 1, j)], ptol[make_pair(h - 2, j + 1)]);
                }
                printf("%d\n", uf.get());
        }
        return 0;
}