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<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 50
#define NIL -1

static char M[MAX][MAX];
static int color[MAX][MAX];
int W, H;

void dfs(int h, int w, int c) {
	color[h][w] = c;
	if (h != H - 1 && M[h + 1][w] == '1'&&color[h + 1][w] == NIL)
		dfs(h + 1, w, c);
	if (h != 0 && M[h - 1][w] == '1'&&color[h - 1][w] == NIL)
		dfs(h - 1, w, c);
	if (w != W - 1 && M[h][w + 1] == '1'&&color[h][w + 1] == NIL)
		dfs(h, w + 1, c);
	if (w != 0 && M[h][w - 1] == '1'&&color[h][w - 1] == NIL)
		dfs(h, w - 1, c);

	if (h != H - 1 && w != W - 1 && M[h + 1][w + 1] == '1'&&color[h + 1][w] == NIL)
		dfs(h + 1, w + 1, c);
	if (h != 0 && w != 0 && M[h - 1][w - 1] == '1'&&color[h - 1][w - 1] == NIL)
		dfs(h - 1, w - 1, c);
	if (h != 0 && w != W - 1 && M[h - 1][w + 1] == '1'&&color[h - 1][w + 1] == NIL)
		dfs(h - 1, w + 1, c);
	if (h != H - 1 && w != 0 && M[h + 1][w - 1] == '1'&&color[h + 1][w - 1] == NIL)
		dfs(h + 1, w - 1, c);
}


int main() {
	while (cin >> W >> H&&H&&W) {
		for (int h = 0; h < H; h++) {
			for (int w = 0; w < W; w++) {
				cin >> M[h][w];
			}
		}
		fill(color[0], color[MAX], NIL);
		int c = 0;
		for (int h = 0; h < H; h++) {
			for (int w = 0; w < W; w++) {
				if (color[h][w] == NIL&&M[h][w] == '1') { dfs(h, w, c++); }
			}
		}
		cout << c << endl;
	}
	return 0;
}

C++: //
//  main.cpp
//  ICPC2009B
//
//  Created by aki33524 on 2014/07/02.
//  Copyright (c) 2014年 aki33524. All rights reserved.
//

#include <iostream>
using namespace std;

int H, W;
int field[50][50];
int dh[] = {0, 0, 1, -1};
int dw[] = {1, -1, 0, 0};

void dfs(int h, int w){
    field[h][w] = 0;
    for(int i=-1; i<=1; i++){
        for(int j=-1; j<=1; j++){
            int nh = h + i;
            int nw = w + j;
            if(0<=nh&&nh<H && 0<=nw&&nw<W && field[nh][nw])
                dfs(nh, nw);
        }
    }
}

void solve(){
    int ans = 0;
    for(int h=0; h<H; h++){
        for(int w=0; w<W; w++){
            if(field[h][w]){
                dfs(h, w);
                ans++;
            }
        }
    }
    cout << ans << endl;
}

int main(int argc, const char * argv[])
{
    while(cin >> W >> H, H|W){
        for(int h=0; h<H; h++){
            for(int w=0; w<W; w++){
                cin >> field[h][w];
            }
        }
        solve();
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
#include <vector>
#include <numeric>
#define PI 3.14159265358979323846
#define MAXINF (1e18L)
#define INF (1e9L)
#define EPS (1e-9)
#define MOD ((ll)(1e9+7))
#define REP(i, n) for(int i=0;i<int(n);++i)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define ALL(v) v.begin(),v.end()
#define FIND(v,x) (binary_search(ALL(v),(x)))
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v));reverse(ALL(v))
#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
#define Yes(n) cout<<((n)?"Yes":"No")<<endl
#define YES(n) cout<<((n)?"YES":"NO")<<endl
#define pb push_back
#define fi first
#define se second
using namespace std;
template<class A>void pr(A a){cout << (a) << endl;}
template<class A,class B>void pr(A a,B b){cout << a << " "  ;pr(b);}
template<class A,class B,class C>void pr(A a,B b,C c){cout << a << " " ;pr(b,c);}
template<class A,class B,class C,class D>void pr(A a,B b,C c,D d){cout << a << " " ;pr(b,c,d);}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;

int lake[50][50];
int w,h;

bool dfs(int x, int y){
    if(lake[x][y]==0) return false;

    int dx[8]={-1,1,0,0,1,1,-1,-1};
    int dy[8]={0,0,-1,1,1,-1,1,-1};
    
    lake[x][y] = 0;

    REP(i, 8){
        int nx = x + dx[i]; 
        int ny = y + dy[i];
        if(0 <= nx && nx < h && 0 <= ny && ny < w && lake[nx][ny] == 1){
            dfs(nx,ny);
        } 
    }
    return true;
}

int main(void)
{
    vector<int> ans;

    while(1){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        REP(i, h){
            REP(j, w){
                cin >> lake[i][j];
            }
        }

        int a = 0;
        REP(i, h){
            REP(j, w){
                a += dfs(i,j);
            }
        }
        ans.pb(a);
    }

    REP(i, ans.size()){
        pr(ans[i]);
    }
}