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 <iostream>
#include <cmath>
#include <vector>
using namespace std;
int draw ( int width, int height,std::vector<std::vector<int>> map){
	for(int i =0; i < height; i ++){
		for(int j = 0; j < width; j ++){
			cout << map[j][i] << " ";
		}
		cout << endl;

	}
	return 0;
}
//未探索陸地か
bool judge (int width,int height,std::vector<std::vector<int>> map, int x, int y){
	if( x >=0 && x  < width && y >= 0 && y < height && map[x][y] == 1){
		return true;
	}
	return false;
}
//周辺を探査して塗りつぶす
std::vector<std::vector<int>> Conqure (int width,int height,std::vector<std::vector<int>> map, int x, int y, int color) {
	map[x][y] = color;
	for(int dx = -1; dx <= 1; dx++){
		for(int dy = -1; dy <= 1; dy++){
			if(judge(width,height,map,x+dx,y+dy)){
				map[x+dx][y+dy] = color;
				if(!(dx == 0 && dy == 0)){
					map = Conqure(width,height,map,x+dx,y+dy,color);
				}
				
			}
		}
	}
	return map;
}
int main () {
	while(true){
			int w,h;
	cin >> w >> h;
	if(w == 0 && h == 0){
		break;
	}
	std::vector<std::vector<int>> map;
		map.resize(w);		// ()内の数字が要素数になる
	for( int i=0; i<w; i++ ){
		map[i].resize(h);
	}
	int landCount = 0;
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			cin >> map[j][i];
		}
	}
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			if(judge(w,h,map,j,i)){
				landCount += 1;
				map = Conqure(w,h,map,j,i,landCount + 1);
				//draw(w,h,map);
			}
		}
	}
	cout <<  landCount << endl;
	}

}

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