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<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<map>
#include<tuple>
#include<bitset>
#include<iomanip>
#define rep(index,num) for(int index=0;index<num;index++)
#define rep1(index,num) for(int index=1;index<=num;index++)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-15
#define mp(a1,a2) make_pair(a1,a2)
typedef long long ll;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl=1e+18+1;
int INF=1e+9+1;
struct UnionFind{
  vector<int> par;
  vector<int> sizes;
  UnionFind(int n) : par(n), sizes(n, 1) {
    rep(i,n) par[i] = i;
  }
  int root(int x) {
    if (x == par[x]) return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return;
    if (sizes[x] < sizes[y]) swap(x, y);
    par[y] = x;
    sizes[x] += sizes[y];
  }
  bool same(int x, int y) {
    return root(x) == root(y);
  }
  int size(int x) {
    return sizes[root(x)];
  }
};
int main(){
	//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
	while(1){
		int w,h;
		scan(w);scan(h);
		if(w==0&&h==0) break;
		int A[51][51];
		rep(i,h){
			rep(j,w){
				scan(A[i][j]);
			}
		}
		UnionFind uf(h*w);
		rep(i,h){
			rep(j,w-1){
				if(A[i][j]==1&&A[i][j+1]==1) uf.unite(i*w+j,i*w+j+1);
			}
		}
		rep(i,h-1){
			rep(j,w){
				if(A[i][j]==1&&A[i+1][j]==1) uf.unite(i*w+j,(i+1)*w+j);
			}
		}
		rep(i,h-1){
			rep(j,w-1){
				if(A[i][j]==1&&A[i+1][j+1]==1) uf.unite(i*w+j,(i+1)*w+j+1);
				if(A[i+1][j]==1&&A[i][j+1]==1) uf.unite((i+1)*w+j,i*w+j+1);
			}
		}
		int ans=0;
		rep(i,h){
			rep(j,w){
				if(A[i][j]==0) continue;
				if(uf.root(i*w+j)==i*w+j) ans++;
			}
		}
		prin(ans);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF=1e18;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

// AOJ How Many Islands? 1160
int h, w;
vector<vector<bool> > square(50, vector<bool>(50));

// 幅優先探索
void dfs(int x, int y){

    // 一度通過した部分は塀とする
    square.at(x).at(y) = 0;

    rep2(dx, -1, 2){
        rep2(dy, -1, 2){
            int nx = x + dx;
            int ny = y + dy;
            if(0 <= nx && nx < w && 0 <= ny && ny < h && square.at(nx).at(ny) == 1) {
                dfs(nx, ny);
            }
        }
    }

    return;
}

int main(){
    h = 1;
    w = 1;
    while(true){
        cin >> w >> h;
        if(h == 0 && w == 0) break;
        int answer = 0;
    
        rep(i, h){
            rep(j, w) {
                int a;
                cin >> a;
                square.at(j).at(i) = a;
            }
        }
        rep(i, h){
            rep(j, w){
                if(square.at(j).at(i) == 1) {
                    dfs(j, i);
                    //cout << "x:" << j << " y:" << i << endl;
                    answer++;
                    //rep(i, h) {rep(j, w) {cout << square.at(j).at(i);} cout << "\n";} cout << "------\n";
                }
            }
        }
        cout << answer << endl;
    }
    
    


}