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 <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <ctime>
#include <string>
#include <map>
#include <stack>
#include <queue>

using namespace std;

int w, h;
const int MAX = 50;
int c[MAX][MAX];


void check(int x, int y){
	c[x][y] = 0;
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            if(x + dx >= 0 && x + dx <= h  && y + dy >= 0 && y + dy <= w && c[x + dx][y + dy] == 1){
            	check(x + dx, y + dy);
            } 
        }
    }
	
}

int main(void){

	for(int i = 0; i < MAX ; i++){
		for(int j = 0; j < MAX; j++){
			c[i][j] = 0;
		}
	}

	cin >> w >> h;
	while(w != 0 && h != 0){
		int ans = 0;
		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				cin >> c[i][j];
			}
		}

		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				if(c[i][j] == 1){
					check(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;

		cin >> w >> h;
	}
	
    return 0;
}

C++: #include<iostream>

using namespace std;

const int MAX = 50;
int data[MAX][MAX];
int w,h;

int x[8] = {1,0,-1,0,1,-1,-1,1};
int y[8] = {0,1,0,-1,1,-1,1,-1};

void dfs(int i, int j){
  data[i][j] = 0;
  for(int k = 0; k < 8; k++){
    int ni = i+y[k], nj = j+x[k];
    if(ni < 0 || ni>= h || nj < 0 || nj>= w) continue;
    if(data[ni][nj]) dfs(ni,nj);
  }
}

int main(){

  while(cin >> w >> h && w+h){
    int ans = 0;
    for(int i = 0; i < h; i++)
      for(int j = 0; j < w; j++) cin >> data[i][j];

    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	if(data[i][j]){
	  dfs(i,j);
	  ans++;
	}
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ck(i, x) ((i) >= 0 && (i) < (x))
using namespace std;
int w, h;
int area[51][51];
int dx[] = {-1,0,1}, dy[] = {-1,0,1};
void dfs(int y, int x){
  area[y][x] = 0;
  //  printf("%d %d\n", y, x);
  rep(i, 3) rep(j, 3)
    if(ck(y+dy[i], h) && ck(x+dx[j], w) && area[y+dy[i]][x+dx[j]]) dfs(y+dy[i], x+dx[j]);

  return;
}
int main(){
  while(scanf("%d%d", &w, &h) && w && h){
    //    printf("in area\n");
    rep(i, h) rep(j, w) scanf("%d", &area[i][j]);
    int cnt = 0;
    //    puts("start solve");
    rep(i, h) rep(j, w) if(area[i][j]) dfs(i, j), cnt++;
    //    puts("end solve");
    printf("%d\n", cnt);
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) FOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

const double eps = 1e-10;
const int MOD = 1000000007;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, s.size()) {
    cout << s[i] << " ";
  }
  cout << endl;
}

int main () {
  cin.tie(0);
  cout << setprecision(10);

  
  while(1) {
    int w, h; cin >> w >> h;
    if(w == 0 && h == 0) break;
    vector<vector<bool>> c(h);
    int tmp = 0;
    REP(i, h) {
      c[i].resize(w);
      REP(j, w) {
        cin >> tmp;
        c[i][j] = (tmp == 1);
      }
    }

    int cnt = 0;
    vector<vector<bool>> sel(h);
    REP(i, h) {
      sel[i].resize(w, false);
    }

    REP(i, h) {
      REP(j, w) {
        if(!sel[i][j] && c[i][j]) {
          P st = make_pair(i, j);
          // cout << i << ":" << j << endl;
          vi dx = {-1, 0, 1}, dy = {-1, 0, 1};
          queue<P> q;
          q.push(st);
          sel[i][j] = true;
          while(!q.empty()) {
            P now = q.front();q.pop();
            REP(k, 3) {
              REP(l, 3) {
                int tmpx = now.fi + dx[k], tmpy = now.se + dy[l];
                if(0 <= tmpx && tmpx <= h-1 &&
                   0 <= tmpy && tmpy <= w-1 &&
                   !sel[tmpx][tmpy] && c[tmpx][tmpy] && (dx[k] != 0 || dy[l] != 0)) {
                  q.push(make_pair(tmpx, tmpy));
                  sel[tmpx][tmpy] = true;
                }
              }
            }
          }
          cnt++;
        }
      }
    }

    cout << cnt << endl;
  }

}