Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

C++: /*
    問題をよく読もう！
    論理的に考えよう！
    サンプルを確認しよう!
    絶対に諦めるな！
    工夫をしろ！
    配列は少し多めにとっておく

    Twitterは終わるまでログアウト！
    （間違えて解法をツイートしてはいけないから）

*/
//include
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
#include<stdio.h>
#include<cstring>
//namespace
using namespace std;
//繰り返し
#define REP(i, m, n) for(int i=(int)m; i<(int)n; ++i)
#define rep(i, n) REP(i, 0, n)
//イテレータ
#define all_range(C) begin(C), end(C)
//簡略化
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
typedef pair<string,int> pst;

const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
//最大最小
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) {if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) {if(a<b) a=b;}

//関数

//最大公約数を求める
ll my_gcd(ll x, ll y) {
  ll r;

  while(y!=0) {
    r = x%y;
    x = y;
    y = r;
  }

  return x;
}

//最小公約数を求める
ll my_lcm(ll x, ll y) {

  return (x*y)/my_gcd(x,y);
}

//xのy乗を求める ※参考：10^18乗まで
ll my_pow(ll x, ll y) {
  ll sum = 1;

  while(y>0) {
    sum *= x;
  }

  return sum;
}

/*考えを書くスペース
xxxxxxxxxx  0000000000 最初のsearchで
xoooooooxx  0111111100
xxoooooxxx  0011111000
xxxoooxxxx  0001110000
xxxxoxxxxx  0000100000
xxxxxxxxxx  0000000000
xxxxoxxxxx  0000200000
xxxoooxxxx  0002220000
xxoooooxxx  0022222000
xxxxxxxxxx  0000000000
*/
//main.cpp----------------------------
#define max_h 50
#define max_w 50

void solve();
void search(int,int);

int h, w;
int c[max_h][max_w];

int main() {

  solve();

  return 0;
}

void solve() {

  while(1) {
    scanf("%d %d", &w, &h);
    if(w==0&&h==0) return;
    rep(i, h) {
      rep(j, w) {
        scanf("%d", &c[i][j]);
      }
    }

    int cnt = 0;
    rep(i, h) {
      rep(j, w) {
        if(c[i][j]==1) {
          search(i, j);
          cnt += 1;
        }
      }
    }

    cout << cnt << endl;
  }
}

void search(int x,int y) {

  if(x<0||x>=h||y<0||y>=w||c[x][y]==0)
    return;

  c[x][y] = 0;

  for(int dx=-1; dx<=1; ++dx) {
    for(int dy=-1; dy<=1; ++dy) {
      search(x+dx, y+dy);
    }
  }
}

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

#ifdef LOCAL
#include "../dump.hpp"
#else
#define dump(...)
#endif

#define rep(i,n) for(ll i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(ll i=(a),i##_cond=(b);i<i##_cond;++i)
#define ROF(i,a,b) for(ll i=(a)-1,i##_cond=(b);i>=i##_cond;--i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend() //sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
#define SUM(a) accumulate(all(a),0)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll,ll> pii;

const ll inf = 1ll<<62;
const ll mod = 1e9+7;

#define int ll

vi dx = {1, 0, -1, 0, 1, -1, -1, 1}, dy = {0, 1, 0, -1, 1, 1, -1, -1};

main()
{
  while(1){
    int h,w; cin >> w >> h;
    if(h == 0) break;
    vvi s(h,vi(w));
    rep(i,h) rep(j,w) cin >> s[i][j];
    vvi look(h,vi(w));

    int ans = 0;
    rep(i,h) rep(j,w){
      if(not look[i][j] and s[i][j]){
	ans++;
	queue<pii> q;

	q.push(pii(i,j));
	look[i][j] = 1;

	while(not q.empty()){
	  pii p = q.front();
	  q.pop();

	  rep(k, sz(dx)){
	    int nx = p.fst + dx[k], ny = p.snd + dy[k];
	    if(0 <= nx and nx < h and 0 <= ny and ny < w and s[nx][ny] and look[nx][ny] == 0){
	      q.push(pii(nx, ny));
	      look[nx][ny] = 1;
	    }
	  }
	}
      }
    }
    //dump(look);
    cout << ans << endl;
  }
}

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

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		ans.push_back(res);

		if (w == 0&&h == 0) {
			break;
		}
	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}