Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

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

int H, W;
int s[50][50];
int d[50][50];

bool inside(int x, int y) {
    return 0 <= x && x < H && 0 <= y && y < W;
}

int bfs(int sx = -1, int sy = -1, int gx = -1, int gy = -1) {
    queue<P> que;
    que.push(P(sx, sy));

    while (!que.empty()) {
        P p = que.front(); que.pop();
        if (p.first == gx && p.second == gy)
             break;

        rep(k, 8) {
            int nx = p.first + dx[k], ny = p.second + dy[k];

            if (inside(nx, ny) && s[nx][ny] != 0 && d[nx][ny] == INF) {
                que.push(P(nx, ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
        }
    }
    return d[gx][gy];
}

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

    while (cin >> W >> H, W) {
        rep(i, H) {
            rep(j, W) {
                cin >> s[i][j];
                d[i][j] = INF;
            }
        }

        int ans = 0;
        rep(i, H) {
            rep(j, W) {
                if (d[i][j] == INF && s[i][j] == 1) {
                    bfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
int m[55][55];
int qx[1000000],qy[1000000];
int s,e,x,y,ans;

void push(int bx,int by){
  e++;
  qx[e]=bx;
  qy[e]=by;
}
void pop(){
  x=qx[s];
  y=qy[s];
  s++;
}
void bfs(int bx,int by){
  s=0; e=-1;
  push(bx,by);
  m[by][bx]=0;
  while(s<=e){
    pop();
    for(int i=y-1;i<y+2;++i){
      for(int j=x-1;j<x+2;++j){
	if(m[i][j]){
	  push(j,i);
	  m[i][j]=0;
	}
      }
    }
  }
}
int main(){
  int w,h;
  while(cin>>w>>h,w){
    //init
    rep(i,55) rep(j,55) m[i][j]=0;
    ans=0;
    
    rep(i,h){
      rep(j,w){
	int a; cin>>a;
	m[i][j]=a;
      }
    }
    rep(i,h) rep(j,w)
      if(m[i][j]){
	bfs(j,i);
	ans++;
      }    
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#include<algorithm>
#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000
int MOD=1000000007;
int w,h;
int tansaku[51][51];
int c[51][51];
int a[3]={1,-1,0};
void dfs(int x,int y){
  if(tansaku[x][y]||x<0||x>=h||y<0||y>=w||c[x][y]==0){return;}
  tansaku[x][y]=1;
  rep(i,3){
    rep(j,3){
          dfs(x+a[i],y+a[j]);
      }
    }
  }
signed main() {

while(true){
  cin>>w>>h;
  int ans=0;
if(w==0&&h==0){break;}
rep(i,51){
  rep(j,51){
    tansaku[i][j]=0;
  }
}
rep(i,h){rep(j,w)cin>>c[i][j];}
rep(j,h){
  rep(i,w){
    if((!tansaku[j][i])&&(c[j][i]==1)){dfs(j,i);ans++;}
  }
}
cout<<ans<<endl;
}
}