Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

C++: #include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9+7;
using Graph = vector<vector<ll>>;
using pll = pair<ll, ll>;
template<class T> inline bool chmin(T &a, T b) { if(a > b){ a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b) { if(a < b){ a = b; return true;} return false;}

// 0 false, 1 true 
// string to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

ll w, h; 
vector<pll> direction = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
//vector<vector<ll>> vec(h, vector<ll>(w));
//vector<vector<bool>> seen(h, vector<bool>(w));

void dfs(vector<vector<ll>> &vec, vector<vector<bool>> &seen, ll y, ll x){
    seen[y][x] = true;

    /*
    cout << "---" << endl;
    rep(i, h){
        rep(j, w){
            cout << vec[i][j] << " ";
        }
        br;
    }
    cout << "---" << endl;
    */

    if(vec[y][x] == 1){
        for(ll i = 0; i < direction.size(); i++){
            ll next_y = y + direction[i].first;
            ll next_x = x + direction[i].second;

            //場外なら終わり
            if(next_y < 0 || next_y >= h){
                continue;
            }
            if(next_x < 0 || next_x >= w){
                continue;
            }

            if(seen[next_y][next_x] == false && vec[next_y][next_x] == 1){
                dfs(vec, seen, next_y, next_x);
            }
        }
    }
}

int main() {
    std::cout << std::fixed << std::setprecision(15);

    vector<ll> res;

    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0){
            break;
        }
        vector<vector<ll>> vec(h, vector<ll>(w));
        vector<vector<bool>> seen(h, vector<bool>(w));
        vec.resize(h, vector<ll>(w, 0));
        seen.resize(h, vector<bool>(w, false));
        
        rep(i, h){
            rep(j, w){
                cin >> vec[i][j];
            }
        }

        ll ans = 0;
        rep(i, h){
            rep(j, w){
                if(vec[i][j] == 1 && seen[i][j] == false){
                    ans++;
                    dfs(vec, seen, i, j);
                }
            }
        }
        res.push_back(ans);

    }

    rep(i, res.size()){
        cout << res[i] << endl;
    }

}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int> P;

int di[] = { 1, 1, 1, 0, 0,-1,-1,-1};
int dj[] = { 1, 0,-1, 1,-1, 1, 0,-1};


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int w,h;
    while(cin >> w >> h){
        if(!w && !h) break;

        int island[h][w];
        rep(i,h) rep(j,w) cin >> island[i][j];

        stack<P> stack;

        int ans = 0;
        rep(i,h) rep(j,w) {
            if(island[i][j] == 0) continue;
            ans++;
            stack.emplace(i,j);
            while(!stack.empty()){
                P p = stack.top(); stack.pop();
                int i = p.first, j = p.second;
                island[i][j] = 0;
                rep(dir,8){
                    int ni = i + di[dir];
                    int nj = j + dj[dir];
                    if(ni<0||ni>=h||nj<0||nj>=w) continue;
                    if(island[ni][nj] == 0) continue;
                    stack.emplace(ni,nj);
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}

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

#define REP(i, n) for(int i = 0; i < n; i++)

const int MAX_N = 50;
int w, h; // wは横の個数 hは縦の個数
int c[MAX_N][MAX_N]; //マップ
int dx = 1, dy = 1; //移動単位
int island; //記憶

void sch(int x, int y) { //ひとつ前のx座標とy座標

	c[x][y] = 0;

	if ((x + dx < w) && (c[x + dx][y] == 1)) {
		sch(x + dx, y);
		return;
	}
	if (((x - dx >= 0) && (c[x - dx][y] == 1))) {
		sch(x - dx, y);
		return;
	}
	if ((y + dy < h) && (c[x][y + dy] == 1)) {
		sch(x, y + dy);
		return;
	}
	if ((y - dy >= 0) && (c[x][y - dy] == 1)) { 
		sch(y, y - dy); 
		return;
	}
	if (x + dx < w && y + dy < h && c[x + dx][y + dy] == 1) {
		sch(x + dx, y + dy); 
		return;
	}
	if (x - dx >= 0 && y + dy < h && c[x - dx][y + dy] == 1) {
		sch(x - dx, y + dy);
		return;
	}
	if (x + dx < w && y - dy >= 0 && c[x + dx][y - dy] == 1) {
		sch(x + dx, y - dy);
		return;
	}
	if (x - dx >= 0 && y - dy >= 0 && c[x - dx][y - dy] == 1) {
		sch(x - dx, y - dy);
		return;
	}

	island++;
	return;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while (1) {
		island = 0;
		cin >> w >> h;
		if (w == 0 && h == 0) break;

		REP(i, w) {
			REP(j, h) cin >> c[i][j];
		}

		REP(i, w) {
			REP(j, h) if (c[i][j] == 1) sch(i, j);
		}
		cout << island << "\n";
	}

	return 0;
}