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++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for(int i = (s); i < (int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i = (int)(s - 1); i >= (int)(e); i--)
#define pb push_back
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-8;

template <typename T> T chmax(T &a, const T &b) { return a = (a > b ? a : b); }
template <typename T> T chmin(T &a, const T &b) { return a = (a < b ? a : b); }

struct UF { // O(loga(n))
    int n;
    int m;
    vi d, r;
    UF(int n) : n(n), m(n), d(n, -1), r(n, 0){};
    int root(int i) {
        if(d[i] < 0) return i;
        return d[i] = root(d[i]);
    }
    bool same(int x, int y) { return root(x) == root(y); }
    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        if(x == y) return false;

        if(r[x] < r[y])
            swap(x, y);
        else if(r[x] == r[y])
            r[x]++;
        d[x] += d[y];
        d[y] = x;
        --m;
        return true;
    }
    int size(int i) { return -d[root(i)]; }
    int size() { return m; }
};

int main() {
    int w, h;
    while(cin >> w >> h) {
        if(w == 0 && h == 0) break;
        vector<vi> d(h, vi(w));
        rep(i, h) rep(j, w) cin >> d[i][j];
        UF uf(h * w);
        rep(i, h) rep(j, w) {
            if(d[i][j]) REP(dy, -1, 2) REP(dx, -1, 2) {
                int x = j + dx, y = i + dy;
                if(min(x, y) < 0 || x >= w || y >= h || d[y][x] == 0)
                    continue;
                uf.unite(i * w + j, y * w + x);
            }
        }
        int ans = 0;
        rep(i, h) rep(j, w) if(d[i][j] && uf.root(i * w + j) == i * w + j) {
            ++ans;
            // cout << " " << i << " " << j << endl;
        }
        cout << ans << endl;
    }
    return 0;
}

C++: # include <iostream>

const int MAX_W = 50;
const int MAX_H = 50;

int w, h;

int c[MAX_H][MAX_W];

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

void dfs(int x, int y) {
	c[y][x] = 0;
	for (int i = 0; i < 8; ++i) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx<0 || ny<0 || nx>w || ny>h)
			continue;
		if (c[ny][nx])
			dfs(nx, ny);
	}
}

int solve() {
	int sum = 0;
	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			if (c[i][j] == 1) {
				dfs(j, i);
				++sum;
			}
		}
	}
	return sum;
}

int main() {
	while (1) {
		
		std::cin >> w >> h;
		if ((w + h) == 0)
			break;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				c[i][j] = 0;
		}
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				std::cin >> c[i][j];
		}
		std::cout << solve() << std::endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(),(v).end()

using namespace std;

using ll=long long;
using pii=pair<int, int>;
using vi=vector<int>;

const ll INF=1LL<<60;
const int MOD=1e9+7;

vector<vector<char>> ch;
vector<vector<bool>> visit;
int dx[8]={1, 0, -1, 0, 1, 1, -1, -1};
int dy[8]={0, 1, 0, -1, 1, -1, 1, -1};
int w, h;

void dfs(int x, int y){
    visit[x][y]=true;
    for(int i=0; i<8; i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<0 || nx>=h || ny<0 || ny>=w) continue;
        if(visit[nx][ny] != 0 || ch[nx][ny] == '0') continue;
        dfs(nx, ny);
    }
}

int main(){
    while(cin >> w >> h){
        if(w==0 && h==0) break;
        ch.assign(h, vector<char>(w));
        visit.assign(h, vector<bool>(w,0));
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cin >> ch[i][j];
            }
        }
        int res=0;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(ch[i][j] == '1' && visit[i][j] == 0){
                    dfs(i, j);
                    res++;
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}

C++: #include <iostream>
using namespace std;
int mas[50][50];
int dx[8] = {0,1,0,-1,1,-1,1,-1};
int dy[8] = {1,0,-1,0,-1,1,1,-1};
int w,h;
void dfs(int,int);

int main(){
    int cnt;
    while(1){
        cnt = 0;
        cin >> w >> h;
        if(w == 0 && h == 0)break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> mas[i][j];
            }
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(mas[i][j] == 1){
                    cnt++;
                    dfs(j,i);
                }
            }
        }
        cout << cnt << endl;
    }
}

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