Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: //#define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
constexpr int PI = (2*acos(0.0));
constexpr int EPS = (1e-9);
constexpr int MOD = (int)(1e9+7);
constexpr int INF = 100000000;
#pragma endregion

int w, h;
int a[51][51];
int dx[]{1,-1,0,0,1,-1,1,-1};
int dy[]{0,0,1,-1,1,-1,-1,1};

void dfs(int x, int y) {
    a[x][y] = 0;
    rep(i, 8) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(nx>=0&&nx<h&&ny>=0&&ny<w&&a[nx][ny]==1) {
            dfs(nx, ny);
        }
    }
}

int main()
{
    while(cin>>w>>h, w) {
        rep(i,h) rep(j,w) a[i][j] = 0;

        rep(i,h) {
            rep(j,w) {
                cin >> a[i][j];
            }
        }

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

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

using namespace std;

int res, land[1080][1080];
int xs[] = {1, 1, 0, -1, -1, -1, 0, 1};
int ys[] = {0, 1, 1, 1, 0, -1, -1, -1};
void dfs(int x, int y){
	land[x][y] = 0;
	for(int i = 0;i < 8;i++){
		int tx = x + xs[i];
		int ty = y + ys[i];
		if(land[tx][ty])dfs(tx, ty);
	}
}

int main(){
	int w, h;
	while(cin >> h >> w, w | h){
		res = 0;
		for(int i = 1;i <= w;i++){
			for(int j = 1;j <= h;j++){
				cin >> land[i][j];
			}
		}
		for(int i = 1;i <= w;i++){
			for(int j = 1;j <= h;j++){
				if(land[i][j]){
					res++;
					dfs(i, j);
				}
			}
		}
		cout << res<< endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int map[60][60];
int main(void) {
	vector<int> list;
	for (;;) {
		int w, h;
		cin >> w >> h;
		if (!(w&&h))return 0;
		int cnt = 1;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				int buf;
				cin >> buf;
				map[i][j] = (buf?cnt++:0);
			}
		}
		bool update = true;
		for (;update;){
			cnt = 0;
			update = false;
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					if (!map[i][j])continue;
					for (int n = max(0, i - 1); n <= min(h-1, i + 1); n++) {
						for (int m = max(0, j - 1); m <= min(w-1, j + 1); m++) {
							if (!map[n][m])continue;
							if (map[i][j]>map[n][m]) {
								map[i][j] = map[n][m];
								update = true;
							}
						}
					}
				}
			}
		}
		vector<int> num;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				num.push_back(map[i][j]);
			}
		}
		sort(num.begin(), num.end());
		num.erase(unique(num.begin(),num.end()),num.end());
		cout << num.size()-1<<endl;
	}
	return 0;
}

C++: #include<queue>
#include<cstdio>
#include<utility>
using namespace std;
typedef pair<int, int> P;
int main() {
	int W, H;
	int i, j, k;
	int map[50][50];
	int vector[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
	int count;
	queue<P> q;
	while (1) {
		scanf("%d %d", &W, &H);
		if (!(W||H)) break;
		count = 0;
		for (i = 0; i < H; i ++) for (j = 0; j < W; j ++) scanf("%d", &map[i][j]);
		for (i = 0; i < H; i ++) {
			for (j = 0; j < W; j ++) {
				if (map[i][j]) {
					map[i][j] = 0; count ++;
					q.push(P(i, j));
					while (!q.empty()) {
						P p = q.front(); q.pop();
						int x, y;
						for (k = 0; k < 8; k ++) {
							x = p.first + vector[k][0]; y = p.second + vector[k][1];
							if (-1 < x && x < H && -1 < y && y < W && map[x][y]) {
								q.push(P(x, y));
								map[x][y] = 0;
							}
						}
					}
				}
			}
		}
		printf("%d\n", count);
	}
	return 0;
}