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>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
typedef long long ll;
template<typename T> bool chmax(T &a, T b) {if(a <= b){a = b; return true;}return false;}
template<typename T> bool chmin(T &a, T b) {if(a >= b){a = b; return true;}return false;}
#define sea 0
#define rand 1

int W, H;
vector<vector<int> > c;
vector<vector<bool> > memo;

int dw[8] = {0,1,1,1,0,-1,-1,-1};
int dh[8] = {1,1,0,-1,-1,-1,0,1};
void dfs(int h, int w){
    memo[h][w] = true;
    for(int i = 0; i < 8; i++){
        int next_h = h + dh[i];
        int next_w = w + dw[i];
        //地図の範囲外でないかの確認
        if(next_w < 0 || next_h < 0 || W <= next_w || H <= next_h)continue;
        //行き先が見探索であるか確認
        if(memo[next_h][next_w])continue;
        dfs(next_h, next_w);
    }
}

main(void) {
    while (true) {
        cin >> W >> H;
        if(W + H == 0)break;
        memo.resize(H);
        rep(h,H){
            memo[h].resize(W);
            rep(w,W)memo[h][w] = false;
        }
        c.resize(H);
        for(int h = 0; h < H; h++){
            c[h].resize(W);
            for(int w = 0; w < W; w++){
                cin >> c[h][w];
                if(c[h][w] == sea)memo[h][w] = true;
            }
        }
        int ans = 0;
        rep(h,H){
            rep(w, W){
                if(!memo[h][w]){
                    dfs(h, w);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

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

#define rep(i,a,b) for(int i=a;i<b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define llrep(i,a,b) for(ll i=a;i<b;i++)
#define llrepd(i,a,b) for(ll i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
using namespace std;
void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; const ll MOD = 1000000007;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

//---------------------------------------------------------------------------------------------------
int dx[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[8] = { 1, 0, -1, 0 ,1, -1, 1, -1 };
ll H, W;
vector<vector<int>> area;

void dfs(int i, int j) {
    area[i][j] = 0;
    rep(n, 0, 8) {
        int nx = i + dx[n], ny = j + dy[n];
        if (nx >= H || ny >= W || nx < 0 || ny < 0) continue;
        if (area[nx][ny] == 1) dfs(nx, ny);
    }
}


void resolve(ll W, ll H) {
    area = vector<vector<int>>(H, vector<int>(W));
    rep(i, 0, H) rep(j, 0, W) cin >> area[i][j];
    ll count = 0;
    rep(i, 0, H) {
        rep(j, 0, W) {
            if (area[i][j] == 1) {
                count++;
                dfs(i, j);
            }
        }
    }
    cout << count << endl;
}

void _main() {
    while(true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        resolve(W,H);
    }
}

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

int dfs(int h, int w, vector<vector<int>> &field, vector<vector<int>> &flag) {
    flag[h][w] = 1;
    int dh[] = {-1, -1, 0, 1, 1, 1, 0, -1};
    int dw[] = {0, 1, 1, 1, 0, -1, -1, -1};
    
    for (int i = 0; i < 8; i++) {
        int nh = h + dh[i];
        int nw = w + dw[i];
        if (field[nh][nw] == 1 && flag[nh][nw] == 0) {
            dfs(nh, nw, field, flag);
        }
    }
    return 0;
}


int solve(int H, int W) {
    vector<vector<int>> field(H+2, vector<int>(W+2));
    for (int h = 1; h <= H; h++) {
        for (int w = 1; w <= W; w++) {
            cin >> field[h][w];
        }
    }
    
    int ret = 0;
    vector<vector<int>> flag(H+2, vector<int>(W+2));
    for (int h = 1; h <= H; h++) {
        for (int w = 1; w <= W; w++) {
            if (field[h][w] == 1 && flag[h][w] == 0) {
                dfs(h, w, field, flag);
                ret++;
            }
        }
    }
    return ret;
}

int main(void){
    int h, w;
    vector<int> ans;
    while (1) {
        cin >> w >> h;
        if (!h && !w) break;
        ans.push_back(solve(h, w));
    }
    for (auto &i : ans) {
        cout << i << endl;
    }
    
}