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 <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;
    }
}