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 <bits/stdc++.h>
using namespace std;

// region template
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return __gcd(a, b); }

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b) {
        ll d = euclid(b, a % b, y, x);
        return y -= a / b * x, d;
    }
    return x = 1, y = 0, a;
}

typedef unsigned long long ull;
typedef long double ld;
ull mod_mul(ull a, ull b, ull M) {
    ll ret = a * b - M * ull(ld(a) * ld(b) / ld(M));
    return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull mod_pow(ull b, ull e, ull mod) {
    ull ans = 1;
    for (; e; b = mod_mul(b, b, mod), e /= 2)
        if (e & 1)
            ans = mod_mul(ans, b, mod);
    return ans;
}

const ll mod = 1000000007;
struct Mod {
    ll x;
    Mod(ll xx) : x(xx) {}
    Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
    Mod operator-(Mod b) { return Mod((x - b.x) % mod); }
    Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
    Mod operator/(Mod b) { return *this * invert(b); }
    Mod invert(Mod a) {
        ll x, y, g = euclid(a.x, mod, x, y);
        assert(g == 1);
        return Mod((x + mod) % mod);
    }
    Mod operator^(ll e) {
        if (!e)
            return Mod(1);
        Mod r = *this ^ (e / 2);
        r = r * r;
        return e & 1 ? *this * r : r;
    }
};
// endregion

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    cin.exceptions(cin.failbit);

    for (;;) {
        int n, m; cin >> n >> m;
        swap(n, m);
        if (!n && !m) break;

        vector<vi> mp(n, vi(m, 0));
        vector<vi> vis(n, vi(m, 0));
        rep(i, 0, n) rep(j, 0, m) cin >> mp[i][j];

        int ans = 0;
        rep(i, 0, n) rep(j, 0, m) if (!vis[i][j] && mp[i][j] == 1) {
            queue<pii> q;
            q.push({i, j});
            vis[i][j] = true;

            while (!q.empty()) {
                pii p = q.front(); q.pop();
                for (int i = -1; i <= 1; i++)
                    for (int j = -1; j <= 1; j++)
                        if (p.first + i >= 0 && p.first + i < n && p.second + j >= 0 && p.second + j < m && !vis[p.first + i][p.second + j] && mp[p.first + i][p.second + j]) {
                            vis[p.first + i][p.second + j] = 1;
                            q.push({p.first + i, p.second + j});
                        }
            }

            ans++;
        }
        cout << ans << endl;
    }
}

C++: #include <iostream>
using namespace std;

int n, w, h;
int field[50][50] = {{0}};

void solve(int x, int y){
    if (!(0 <= x < w && 0 <= y < h)) return;
    if (field[x][y] != 1) return;
    field[x][y] = -1;
    for (int x_ = -1; x_ <= 1; x_++){
        for (int y_ = -1; y_ <= 1; y_++){
            if (!(x_ == x && y_ == y)){
                solve(x+x_, y+y_);
            }
        }
    }
}

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

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x+1][y+1]){f(8)k(x+u[i],y+u[i+8]);d[x+1][y+1]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(10000)d[i]=0;
	f(n*m)s(d[i/n][i%n]);
	f(n*m)if(d[i/n][i%n])a++,k(i/n,i%n);
	printf("%d\n",a);
	main();
}

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

using namespace std;

int w,h,i,j;
int field[52][52];

void dfs(int x, int y){
    //埋め立て
    field[x][y]=0;
    
    if(field[x][y+1]==1) dfs(x,y+1);
    if(field[x][y-1]==1) dfs(x,y-1);
    
    if(field[x+1][y+1]==1) dfs(x+1,y+1);
    if(field[x+1][y-1]==1) dfs(x+1,y-1);
    if(field[x+1][y]==1) dfs(x+1,y);
    
    if(field[x-1][y+1]==1) dfs(x-1,y+1);
    if(field[x-1][y-1]==1) dfs(x-1,y-1);
    if(field[x-1][y]==1) dfs(x-1,y);
}


int main(){
    while(cin>>w>>h && w!=0 && h!=0){
        for(i=1; i<=h; i++){
            for(j=1; j<=w; j++){
                int a;
                scanf("%d",&a);
                field[i][j]=a;    
            }
        }
    
        int cnt=0;
    
        for(i=1; i<=h; i++){
            for(j=1; j<=w; j++){
                if(field[i][j]==1){
                    dfs(i,j);
                    cnt++;
                }    
            }
        }
        
        cout<<cnt<<endl;
    }
    return 0;
}