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 <iostream>
#include <vector>
#include <map>
#include <set>
using namespace std;

void add_label(map<int,int> &tbl, int a, int b){
	if( a == b ) return;
	if( a > b ) tbl[a] = b;
	else tbl[b] = a;
}

int main(void){
	int w, h;
	while( cin>>w>>h, w||h ) {
		vector<vector<int> > field( h );
		vector<vector<int> > labeled( h );
		for( int i=0; i<h; ++i ) {
			field[i] = vector<int>( w );
			labeled[i] = vector<int>( w );
			for( int j=0; j<w; ++j ) {
				cin>>field[i][j];
				labeled[i][j] = 0;
			}
		}
		int label = 1;
		map<int, int> tbl;
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				if( field[i][j] ){
					bool f = false;
					if( i ) {
						if( j && labeled[i-1][j-1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j-1];
							f = true;
						}
						if( labeled[i-1][j] ) {
							if(!f) labeled[i][j] = labeled[i-1][j];
							else add_label( tbl, labeled[i][j] ,labeled[i-1][j] );
							f = true;
						}
						if( j < w - 1 && labeled[i-1][j+1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j+1];
							else add_label( tbl, labeled[i][j] ,labeled[i-1][j+1] );
							f = true;
						}
						if( j && labeled[i][j-1] ) {
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j] ,labeled[i][j-1] );
							f = true;
						}
						if(!f) labeled[i][j] = ++label;
					} else {
						if( j && labeled[i][j-1] ){
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j] ,labeled[i][j-1] );
							f = true;
						}
						if(!f) labeled[i][j] = ++label;
					}
				}
			}
		}
		for( auto x : tbl ) {
			int a = x.first;
			int b = x.second;
			while( tbl.find(b) != tbl.end() ) {
				b = tbl.find(b)->second;
			}
			for( int i=0; i<h; ++i ) {
				for( int j=0; j<w; ++j ) {
					if( labeled[i][j] == a ) labeled[i][j] = b;
				}
			}
		}
		
		for( int i=0; i<h; ++i ) {
			cerr<<"!";
			for( int j=0; j<w; ++j ) {
				cerr<<labeled[i][j];
			}
			cerr<<endl;
		}
		
		set<int> nums;
		nums.insert(0);
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				nums.insert( labeled[i][j] );
			}
		}
		cout<<nums.size()-1<<endl;
	}
	return 0;
}

C++: # include <algorithm>
# include <cassert>
# include <cctype>
# include <cstdio>
# include <cstdlib>
# include <cstring>
# include <cmath>
# include <iostream>
# include <map>
# include <queue>
# include <set>
# include <sstream>
# include <stack>
# include <string>
# include <vector>
# include <iomanip>

# define rep(i, n) for (int i = 0; i < (int)(n); i++)
# define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
# define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;
int w, h, ans;

template <typename T>
void show_all(T t) {
  rep (i, t.size()) {
    rep (j, t[0].size()) {
      cout << setfill('0') << setw(5) << t[i][j] << ' ';
    }
    cout << endl;
  }
  cout << endl;
}


void dfs(vector<vector<int>> &c, int i, int j) {
  vector<int> next_i = {0, 1, 1, 1, 0, -1, -1, -1};
  vector<int> next_j = {1, 1, 0, -1, -1, -1, 0, 1};

  rep (k, 8) {
    int ii = i + next_i[k];
    int jj = j + next_j[k];

    if (0 <= ii && 0 <= jj && ii < c.size() && jj < c[0].size() && c[ii][jj] == 1) {
      c[ii][jj] = 1000;
      dfs(c, ii, jj);
    }
  }
}


int main() {

  while (cin >> w >> h && w && h) {
    vector<vector<int>> c;
    ans = 0;

    rep (i, h) c.push_back(vector<int>(w));
    rep (i, h) {
      rep (j, w) {
        cin >> c[i][j];
      }
    }

    rep (i, h) {
      rep (j, w) {
        if (c[i][j] == 1) {
          ans++;
          c[i][j] = 1000;
          dfs(c, i, j);
        }
      }
    }

    cout << ans << endl;
  }
}

C++: #include<iostream>
using namespace std;
void kazoe(int p,int P, int box[50][50]){
  if(box[p][P+1]==1){
    box[p][P+1]=0;
    P=P+1;
    kazoe(p,P,box);
  }
  if(box[p+1][P+1]==1){
    box[p+1][P+1]=0;
    P=P+1;
    p=p+1;
 kazoe(p,P,box);
  }
  if(box[p+1][P]==1){
    box[p+1][P]=0;
    p=p+1;
 kazoe(p,P,box);
  }
  if(box[p+1][P-1]==1){
    box[p+1][P-1]=0;
    P=P-1;
    p=p+1;
 kazoe(p,P,box);
  }
  if(box[p][P-1]==1){
    box[p][P-1]=0;
    P=P-1;
 kazoe(p,P,box);
  }
  if(box[p-1][P-1]==1){
    box[p-1][P-1]=0;
    P=P-1;
    p=p-1;
 kazoe(p,P,box);
  }
  if(box[p-1][P]==1){
    box[p-1][P]=0;
    p=p-1;
 kazoe(p,P,box);
  }
  if(box[p-1][P+1]==1){
    box[p-1][P+1]=0;
    P=P+1;
    p=p-1;
 kazoe(p,P,box);
  }
}
int main(){
  int w,h,i,I,p,P,a=0;
  int box[50][50];
  while(1){
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	cin>>box[i][I];
      }
    }
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1){
	  p=i;
	  P=I;
	  box[p][P]=0;
	  kazoe(p,P,box);
	  box[i][I]=1;
	}
      }
    }
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) a++;
      }
    }
  
    cout<<a<<endl;
    a=0;
  }
  return 0;
}