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))
#define REPS(a,i,n) for(int (i) = (a) ; (i) < (n) ; ++(i))
#define REVERSE(i,n) for(int (i) = n-1;(i) >= 0 ; --i)
#define ALL(c) (c).begin() , (c).end()
#define LL long long
#define int LL
#define inf  (1LL << 60)
#define mod 1000000007
#define QUICK_CIN ios::sync_with_stdio(false); cin.tie(0);
#define lower lower_bound
#define upper upper_bound
#define ZERO(c,n) memset(&c[0],0,sizeof(int)*n)
#define ZERO2(c,n) memset(&c[0][0],0,sizeof(int)*n
#define pl(a) cout << ""#a": " << a << endl;
#ifdef _DEBUG
#define debug_io fstream cin("input.txt");ofstream cout("output.txt");
template<class T>fstream& operator>>(fstream& is, vector<T>& v)
{
	int n = v.size();
	REP(i, n) {
		is >> v[i];
	}
	return is;
}
#else
#define debug_io ;
#endif
#define debug_input debug_io
#define DOWN(T) std::greater<T>()
template<class T>istream& operator>>(istream& is, vector<T>& v)
{
	int n = v.size();
	REP(i, n) {
		is >> v[i];
	}
	return is;
}
using vs = vector<string>; using vi = vector<int>; using pii = pair<int, int>;
using psi = pair<string, int>; using vvi = vector<vi>; using pss = pair<string, string>;
using vpii = vector<pii>;
template<class T>bool valid(T x, T w) { return 0 <= x&&x < w; }
//int dx[4] = { 1, -1, 0, 0 }; int dy[4] = { 0, 0, 1, -1 };
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------

bool ed[55][55];
int dx[8] = { 0,0,1,-1,1,-1,1,-1 };
int dy[8] = { 1,-1,0,0,1,1,-1,-1 };

signed main()
{
	QUICK_CIN;
	debug_input;
	int w, h;
	while (cin >> w >> h, w + h) {
		vector<vi> tile(h,vi(w));
		REP(i, h) {
			REP(j, w) {
				cin >> tile[i][j];
			}
		}

		int ans = 0;
		REP(i, 55) {
			REP(j, 55) {
				ed[i][j] = false;
			}
		}

		REP(i, h) {
			REP(j, w) {
				if (tile[i][j] != 1)
					continue;
				queue<pii> q;
				q.push({ i,j });
				while (q.size()) {
					auto t = q.front();
					q.pop();
					auto y = t.first;
					auto x = t.second;
					if (ed[y][x]) {
						continue;
					}
					tile[y][x] = 2;
					ed[y][x] = true;
					REP(k, 8) {
						auto nx = min(w-1,max(0LL,x + dx[k]));
						auto ny = min(h-1,max(0LL,y + dy[k]));
						if (!ed[ny][nx]&&tile[ny][nx]==1) {
							q.push({ ny,nx });
						}
					}
				}
				ans++;
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
typedef long long ll;

int field[55][55];

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

int w,h;

void dfs(int i, int j){
	field[i][j] = 0;
	for (int k = -1; k <= 1; ++k) {
		for (int l = -1; l <= 1 ; ++l) {
			if(k == 0 && l == 0) continue;
			int x = j + k;
			int y = i + l;
			if(x<0 || x>= w || y<0 || y>=h) continue;
			if(field[y][x] == 1){
				dfs(y,x);
			}
		}
	}
	return ;
}

int main(){
	cin >> w >> h;
	
	while(w != 0 || h != 0) {
		rep(i, h) {
			rep(j, w) {
				cin >> field[i][j];
			}
		}
		
		int ans = 0;
		
		rep(i, h) {
			rep(j, w) {
				if (field[i][j] == 1) {
					dfs(i, j);
					ans++;
				}
			}
		}
		
		cout << ans << endl;
		cin >> w >> h;
	}
    return 0;
}

C++: #include<stdio.h>
#include<iostream>

int field[51][51];
int w,h;

void land(int x,int y){
	field[x][y]=0;

	for(int yy=-1;yy<=1;yy++){
		for(int xx=-1;xx<=1;xx++){
			if(x+xx>=0&&x+xx<=w&&y+yy>=0&&y+yy<=h){
			   if(field[x+xx][y+yy]==1) land(x+xx,y+yy);
			}
		}
	}
	return;
}

int main(void){

	int kazu=0;

	scanf("%d %d",&w,&h);

	if(w==0||h==0) return 0;

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			scanf("%d",&field[j][i]);
		}
	}

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[i][j]==1){
				land(i,j);
				kazu++;
			}

		}
	}

	printf("%d",kazu);
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back 
#define pf push_front 
#define mp make_pair
#define fr first
#define sc second
#define Rep(i,n) for(int i=0;i<(n);i++)
#define All(v) v.begin(),v.end()
typedef pair<int, int> Pii; typedef pair<int, Pii> Pip;
const int INF = 1107110711071107;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};     

int w, h;
bool data[51][51];

void dfs( int y, int x )
{
  data[y][x] = 0;

  Rep(i, 8) {
    int nx = x + dx[i], ny = y + dy[i];
    if( nx < 0 || nx >= w || ny < 0 || ny >= h || !data[ny][nx] ) continue;
    dfs( ny, nx );
   }
}


main()
{
  while( cin >> w >> h, !(w == 0 && h == 0) ) {
    
    Rep(i, h) Rep(j, w) cin >> data[i][j];

    int cnt = 0;
    Rep(i, h) Rep(j, w) {
      if( data[i][j] ) dfs(i, j), cnt++; 
    }

    cout << cnt << endl;
  }
   
}