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++: #define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
//
#define int long long
#define ALL(x) (x).begin(), (x).end()
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
#define m0(x) memset((x), 0, sizeof(x))
#define mm(x) memset((x), -1, sizeof(x))
// bit_macro
#define bit(n) (1LL << (n))
#define bitset(a, b) (a) |= (1 << (b))
#define bitunset(a, b) (a) |= ~(1 << (b))
#define bitcheck(a, b) ((a) >> (b)) & 1
#define bitcount(a) __builtin_popcountll((a))
//
typedef long long lint;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;
//
const int INF = (int)1e18;
const int MOD = (int)1e9 + 7;
const double EPS = (double)1e-10;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
//
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
//
struct Accelerate_Cin {
  Accelerate_Cin() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
};

int w, h;
int c[60][60];

void dfs(int y, int x) {
  if (c[y][x] == 0) {
    return;
  }

  c[y][x] = 0;

  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      if (c[y + i][x + j] == 1) {
        dfs(y + i, x + j);
      }
    }
  }
}

signed main() {
  while (cin >> w >> h, (w != 0 || h != 0)) {
    m0(c);
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> c[i][j];
      }
    }

    int cont = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (c[i][j] == 1) {
          dfs(i, j);
          cont++;
        }
      }
    }

    cout << cont << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int (i) = 0;(i) < (n) ; ++(i))
#define REPS(a,i,n) for(int (i) = (a) ; (i) < (n) ; ++(i))
#define REVERSE(i,n) for(int (i) = n-1;(i) >= 0 ; --i)
#define ALL(c) (c).begin() , (c).end()
#define LL long long
#define int LL
#define inf  (1LL << 60)
#define mod 1000000007
#define QUICK_CIN ios::sync_with_stdio(false); cin.tie(0);
#define lower lower_bound
#define upper upper_bound
#define ZERO(c,n) memset(&c[0],0,sizeof(int)*n)
#define ZERO2(c,n) memset(&c[0][0],0,sizeof(int)*n
#define pl(a) cout << ""#a": " << a << endl;
#ifdef _DEBUG
#define debug_io fstream cin("input.txt");ofstream cout("output.txt");
template<class T>fstream& operator>>(fstream& is, vector<T>& v)
{
	int n = v.size();
	REP(i, n) {
		is >> v[i];
	}
	return is;
}
#else
#define debug_io ;
#endif
#define debug_input debug_io
#define DOWN(T) std::greater<T>()
template<class T>istream& operator>>(istream& is, vector<T>& v)
{
	int n = v.size();
	REP(i, n) {
		is >> v[i];
	}
	return is;
}
using vs = vector<string>; using vi = vector<int>; using pii = pair<int, int>;
using psi = pair<string, int>; using vvi = vector<vi>; using pss = pair<string, string>;
using vpii = vector<pii>;
template<class T>bool valid(T x, T w) { return 0 <= x&&x < w; }
//int dx[4] = { 1, -1, 0, 0 }; int dy[4] = { 0, 0, 1, -1 };
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------


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

bool ed[55][55];

signed main()
{
	QUICK_CIN;
	debug_input;
	int w, h;
	while (cin >> w >> h, w + h) {
		vector<vi> tile(h, vi(w));
		REP(i, h) {
			REP(j, w) {
				cin >> tile[i][j];
			}
		}

		int ans = 0;

		REP(i, h) {
			REP(j, w) {
				if (tile[i][j] != 1) {
					continue;
				}
				REP(i, 55) {
					REP(j, 55) {
						ed[i][j] = false;
					}
				}
				queue<pii> q;
				q.push({ i,j });
				while (q.size()) {
					auto t = q.front();
					q.pop();
					auto x = t.second;
					auto y = t.first;

					if (ed[y][x]) {
						continue;
					}
					ed[y][x] = true;
					tile[y][x] = 2;

					REP(k, 8) {
						auto nx = min(w-1,max(0LL,x + dx[k]));
						auto ny = min(h-1,max(0LL,y + dy[k]));

						if (tile[ny][nx] == 1 && !ed[ny][nx]) {
							q.push({ ny,nx });
						}
					}
				}
				ans++;
			}
		}
		cout << ans << endl;

	}
}

C++: #include <iostream>

using namespace std;

int c[50][50],w,h;
int dx[]={-1,-1,-1,0,0,1,1,1};
int dy[]={-1,0,1,-1,1,-1,0,1};
void dfs(int y,int x){
  if(c[y][x]){
    c[y][x]=0;
    int i;
    for(i=0;i<8;i++){
      if(y+dy[i]>=0 || y+dy[i]<h || x+dx[i]>=0 || x+dx[i]<w)
      dfs(y+dy[i],x+dx[i]);
    }
  }
  return;
}

int main(){
  int i,j,ans;
  cin >> w >> h;
  while(1){
    ans=0;
    if(w+h==0)break;
    //for(i=0;i<50;i++)for(j=0;j<50;j++)c[i][j]=0;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++)cin >> c[i][j];
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++)if(c[i][j]){
        dfs(i,j);
        ans++;
      }
    }
    cout << ans << endl;
    cin >> w >> h;
  }
  return 0;
}