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 rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;

int n, m;
int field[51][51];

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

void dfs(int y, int x){
    field[y][x] = 0;
    rep(i,8){
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(ny < 0 || n < ny || nx < 0 || m < nx) continue;
        if(field[ny][nx] == 0) continue;
        dfs(ny, nx);
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
        cin >> m >> n;
        if(m == 0 && n == 0) break;
        rep(i,n)rep(j,m) cin >> field[i][j];
        int ans = 0;
        rep(i,n)rep(j,m){
            if(field[i][j] == 0) continue;
            dfs(i, j);
            ans++;
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#include <bits/stdc++.h>
using namespace std;

int C[55][55]={0};
int W,H;

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

void dfs(int h,int w){
  C[h][w]=0;
  
  rep(i,8){
    int nh=h+dy[i];
    int nw=w+dx[i];
    
    if(nh<0 || nh>=H || nw<0 || nw>=W) continue;
    if(C[nh][nw]==0) continue;
    
    dfs(nh,nw);
  }
}

int main(){
  
  while(cin>>W>>H){
    if(W==0 && H==0) return 0;
    
    int cnt=0;
    rep(i,H){
      rep(j,W) cin>>C[i][j];
    }
    
    rep(i,H){
      rep(j,W){
        if(C[i][j]==0) continue;
        dfs(i,j);
        cnt++;
      }
    }
    
    cout<<cnt<<endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

vector<vector<int>> field(100, vector<int>(100));
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
void dfs(int y, int x) {
    field[y][x] = 0;
    for (int i = 0; i < 8; i++) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (!field[ny][nx]) continue;
        if (field[ny][nx]) dfs(ny, nx);
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int W, H;
    while (1) {
        cin >> W >> H;
        if (!W & !H) break;
        for (int i = 1; i <= H; i++) {
            for (int j = 1; j <= W; j++) {
                cin >> field[i][j];
            }
        }
        int cnt = 0;
        for (int i = 1; i <= H; i++) {
            for (int j = 1; j <= W; j++) {
                if (field[i][j] == 1) {
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        cout << cnt << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <string>
#include <algorithm>
#include <string>
#include <climits>

#define REP(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)
#define REG(i, a, b) for (int (i) = ((int)(a)); (i) < ((int)(b)); i++)
#define ALL(V) (V).begin(), (V).end()
#define PRINT(STR) cout << (STR) << endl

typedef long long ll;
using namespace std;

const int SEA = 0;
const int GROUND = 1;

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

void dfs(int y, int x) {
	if (x < 0 || x >= w) return;
	if (y < 0 || y >= h) return;
	if (board[y][x] == 0) return;
	board[y][x] = 0;
	dfs(y - 1, x);
	dfs(y - 1, x + 1);
	dfs(y, x + 1);
	dfs(y + 1, x + 1);
	dfs(y + 1, x);
	dfs(y + 1, x - 1);
	dfs(y, x - 1);
	dfs(y - 1, x - 1);
}

void calc() {
	int count = 0;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			if (board[i][j] == GROUND) {
				count++;
				dfs(i, j);
			}
		}
	}
	PRINT(count);
}

int main() {
	cin.tie(0); //対話型には使えない
	ios::sync_with_stdio(false); //printfと共には使えない
	while (cin >> w >> h, w + h != 0) {
		REP(i, h) {
			REP(j, w) {
				char c;
				cin >> c;
				if(c == '0') {
					board[i][j] = SEA;
				} else {
					board[i][j] = GROUND;
				}
			}
		}
		calc();
	}
	return 0;
}

C++: #include <iostream>
using namespace std;

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

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

//void printmap() {
//	for (int i = 0 ; i < h; i++) {
//		for (int j = 0; j < w; j++)
//			cout << map[i][j] << " ";
//		cout << endl;
//	}
//}

void oneIsland(int x, int y) {
	for (int i = 0; i < 8; i++) {
		int nextX = x + dx[i];
		int nextY = y + dy[i];
		if (x < 0 || w <= x) continue;
		if (y < 0 || w <= y) continue;
		if (map[nextY][nextX] == 0) continue;
		map[nextY][nextX] = 0;
		oneIsland(nextX, nextY);
	}
}

int main() {
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> map[i][j];
			}
		}
		
		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					map[i][j] = 0;
					oneIsland(j, i);
					//printmap();
					count++;
				}
			}
		}
		cout << count <<endl;
	}
}