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>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
int h,w,a[50][50];
int dy[8]={-1,-1,-1,0,0,1,1,1},dx[8]={-1,0,1,-1,1,-1,0,1};
ll uni[2500];
ll root(ll u){
    if(uni[u]<0)return u;
    return uni[u]=root(uni[u]);
}

void connect(int a,int b){
    a=root(a);
    b=root(b);
    if(a==b)return;
    if(uni[a]>uni[b]){
        a^=b;
        b^=a;
        a^=b;
    }
    uni[a]+=uni[b];
    uni[b]=a;
    return;
}
ll size(ll u){
    return -uni[root(u)];
}

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    vector<int> v;
    while(1){
    	cin>>w>>h;
    	if(!(h||w))break;
    	for(int i=0;i<h;i++){
    		for(int j=0;j<w;j++){
    			cin>>a[i][j];
    			uni[j*h+i]=(a[i][j])?-1:0;
    		}
    	}
    	for(int i=0;i<h;i++){
    		for(int j=0;j<w;j++){
    			if(a[i][j]){
    				for(int k=0;k<8;k++){
    					int x=dx[k]+j;
    					int y=dy[k]+i;
    					if(x<0||x>=w||y<0||y>=h)continue;
    					if(a[y][x]){
    						connect(j*h+i,x*h+y);
    					}
    				}
    			}
    		}
    	}
    	int ans=0;
    	for(int i=0;i<h;i++){
    		for(int j=0;j<w;j++){
    			if(a[i][j]&&uni[j*h+i]<0)ans++;
    		}
    	}
    	v.pb(ans);
    }
    for(auto& i:v)cout << i<<endl;
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main(){
	int w,h;
	while(1){
		cin>>w>>h;if(w==0&&h==0)break;
	int map[55][55]={};
	int W=0;
	for(int i=0;i<h;i++)
		for(int j=0;j<w;j++)
		{int x;cin>>x;if(x==1){W++;map[i+1][j+1]=W;}}

	int MAP[55][55]={};int c=0;
	for(int i=0;i<h+1;i++)
		for(int j=0;j<w+1;j++)
		{if(map[i][j]>0){c=map[i][j];

	int G[10]={map[i-1][j-1],map[i-1][j],map[i-1][j+1],map[i][j-1],map[i][j],map[i][j+1],map[i+1][j-1],map[i+1][j],map[i+1][j+1]};
			if(map[i-1][j-1]>0&&c>map[i-1][j-1])c=map[i-1][j-1];
			if(map[i-1][j]>0&&c>map[i-1][j])c=map[i-1][j];
			if(map[i-1][j+1]>0&&c>map[i-1][j+1])c=map[i-1][j+1];
			if(map[i][j-1]>0&&c>map[i][j-1])c=map[i][j-1];
			if(map[i][j+1]>0&&c>map[i][j+1])c=map[i][j+1];
			if(map[i+1][j-1]>0&&c>map[i+1][j-1])c=map[i+1][j-1];
			if(map[i+1][j]>0&&c>map[i+1][j])c=map[i+1][j];
			if(map[i+1][j+1]>0&&c>map[i+1][j+1])c=map[i+1][j+1];
			for(int I=0;I<53;I++)
				for(int J=0;J<53;J++)
					for(int H=0;H<9;H++)
					{if(map[I][J]==G[H]&&map[I][J]>0)map[I][J]=c;}
	}
		}
	bool C[2502]={};
	for(int i=0;i<h+1;i++){//cout<<endl;
		for(int j=0;j<w+1;j++){//cout<<map[i][j]<<" ";
			if(map[i+1][j+1]>0)C[map[i+1][j+1]]=true;
		}
	}
	int R=0;
	for(int i=0;i<2501;i++)if(C[i]==true)R++;
			cout<<R<<endl;

	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, 1, 0, -1, -1, -1, 0, 1};
int dy[]={0, -1, -1, -1, 0, 1, 1, 1};

int main(){
    while(1){
        int w, h; cin>>w>>h;
        if(w == 0) return 0;

        vector<vector<int>> c(h, vector<int>(w));
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cin>>c[i][j];
            }
        }

        int ans=0;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(c[i][j]){
                    ans++;
                    c[i][j] = 0;

                    queue<P> que;
                    que.push(P(i, j));
                    while(que.size()){
                        int cy, cx;
                        tie(cy, cx) = que.front();
                        que.pop();

                        for(int k=0; k<8; k++){
                            int ny = cy + dy[k];
                            int nx = cx + dx[k];

                            if(0 <= ny && ny < h && 0 <= nx && nx < w){
                                if(c[ny][nx]){
                                    c[ny][nx] = 0;
                                    que.push(P(ny, nx));
                                }
                            }
                        }
                    }
                }
            }
        }

        cout << ans << endl;
    }
}