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 <stack>

using namespace std;

struct locate{
  int x;
  int y;
};

int graph[64][64] = {{}};
int flag[64][64] = {{}};
int w,h;

int dfs(locate );


int main(){

  int sum = 0;
  locate in;

  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 >> graph[i][j];
    }
  }
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
	if(graph[i][j] == 1 && flag[i][j] == 0){
	  sum++;
	  in.x = j;
	  in.y = i; 
	  if(dfs(in) == -1){
	    sum-= 1;
	  }
	  //	cout << "sum = " << sum << endl;
	}
      }
    }

    cout << sum << endl;
    sum = 0;
    for(int i = 0;i < 64;i++){
      for(int j = 0;j < 64;j++){
	graph[i][j] = 0;
	flag[i][j] = 0;
      }
    }
  }
  return 0;
}



int dfs(locate in){

  stack<locate> S;
  locate u,v,re;
  int  dx,dy,a = 0;

  S.push(in);

  while(!S.empty()){
    v = S.top();S.pop();
    for(dy = -1;dy <= 1;dy++){
      if(v.y + dy < 0 || v.y + dy > 63) continue;
      for(dx = -1;dx <= 1;dx++){
	if(v.x + dx < 0 || v.x + dx > 63 || (dx == 0 && dy == 0)) continue;
	//	cout << "x = " << v.x << "y = " << v.y << endl; 
	if(graph[v.y + dy][v.x + dx] == 1 && flag[v.y + dy][v.x + dx] == 0){
	  // cout << 'a' << endl;
	  flag[v.y + dy][v.x + dx] = 1;
	  re.x = v.x + dx;
	  re.y = v.y + dy;
	  a = 1;
	  dfs(re);
	}
	/*else if(graph[v.y + dy][v.x + dx] == 1 && flag[v.y + dy][v.x + dx] == 1){
	  cout << 'b' << endl;
	  a = 0;
	  return -1;
	  }*/
      }
    }
  }
  return a;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<math.h>
#include<stdio.h>
using namespace std;
#define mod (1000000000+7)
#define N (10007)
#define INF 1e16
typedef long long ll;
typedef pair<ll,ll> P;
int h,w;
int board[100][100];

bool check(int i, int j) {
	return (i >= 0) && (i < h) && (j >= 0) && (j < w) && (board[i][j] == 1);
}

void dfs(int i,int j){
	for(int dx=i-1;dx<=i+1;dx++){
		for(int dy=j-1;dy<=j+1;dy++){
			if(check(dx,dy)){
				board[dx][dy]=0;
				dfs(dx,dy);
			}
		}
	}
}

int main(void){
	while(true){
		cin>>w>>h;
		if(w==0&&h==0)break;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>board[i][j];
			}
		}
		int ans=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(board[i][j]){
					dfs(i,j);
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
    return 0;
}

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

vector<vector<int>> m;
int xv[]={1,1,1,0,-1,-1,-1,0},yv[]={1,0,-1,-1,-1,0,1,1};


void bfs(int x,int y){
    stack<pair<int,int>> stk;
    m[x][y]=0;
    for(int i=0;i<8;i++){
        if(m[x+xv[i]][y+yv[i]]){
            stk.push(make_pair(x+xv[i],y+yv[i]));
        }
    }
    while(!stk.empty()){
        pair<int,int> tg=stk.top();
        stk.pop();
        bfs(tg.first,tg.second);
    }
    
}


void solve(int c,int r){
    int in,ans=0;
    m=vector<vector<int>>(r+2,vector<int>(c+2,0));
    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            cin>>in;
            m[i][j]=in;
        }
    }
    stack<pair<int,int>> stk;
    
    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            if(m[i][j]){
                ans++;
                bfs(i,j);
            }
        }
    }
    
    cout<<ans<<endl;
}

int main(void){
    int a,b;
    while(cin>>a>>b,a){
        solve(a,b);
    }
}