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++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

int w,h;
int solve(){
    int res = 0;
    vector<vector<int>> grid(h+2,vector<int>(w+2));
    for(int i = 1; i <= h; i++) for(int j = 1; j <= w;j++) cin >> grid[i][j];
    for(int i = 1; i <= h; i++) for(int j = 1; j <= w;j++){
        if(grid[i][j]){
            queue<pii> q; q.push({i,j});
            while(q.size()){
                int y,x;  tie(y,x) = q.front(); q.pop();
                if(grid[y][x] == 0) continue;
                grid[y][x] = 0;
                for(int dy = -1; dy <= 1; dy++){
                    for(int dx = -1; dx <= 1; dx++){
                        q.push({y+dy,x+dx});
                    }
                }
            }
            res++;
        }
    }
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    while(cin >> w >> h,w){
        cout << solve() << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 10007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { 1, -1, 0, 0,  1, -1,  1, -1 };
static const int dy[] = { 0, 0, 1, -1 , 1,  1, -1, -1 };

string str;
int w, h, ans;
VVI ar;

int gcd( int m, int n ) {
	if ( n == 0 ) return m;
	return gcd( n, m % n );
}

void solve( int y, int x ) {
	ar[ y ][ x ] = 0;
	for ( int i = 0; i < 8; i++ ) {
		if ( ar[ y + dy[ i ] ][ x + dx[ i ] ] ) {
			solve( y + dy[ i ], x + dx[ i ] );
		}
	}
}

int main() {
	while ( ~scanf("%d %d", &w, &h) && w && h ) {
		ar.resize( h + 2 );
		for ( int i = 0; i < h + 2; i++ ) {
			ar[ i ].resize( w + 2 );
		}
		ans = 0;
		for ( int i = 0; i < w + 2; i++ ) {
			ar[ 0 ][ i ] = 0;
			ar[ h + 1 ][ i ] = 0;
		}
		for ( int i = 1; i < h + 1; i++ ) {
			for ( int j = 1; j < w + 1; j++ ) {
				scanf( "%d", &ar[ i ][ j ] );
			}
		}
		for ( int i = 0; i < h + 2; i++ ) {
			for ( int j = 0; j < w + 2; j++ ) {
				if ( ar[ i ][ j ] ) {
					ans++;
					solve( i, j );
				}
			}
		}
		printf( "%d\n", ans );
		ar.clear();
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <sstream>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <complex>
#include <vector>
#include <cstdio>
#include <math.h>
#include <cmath>
#include <time.h>
#define all(c) ((c).begin(),(c).end())
#define rall(c) (c).rbegin(),(c).rend()
#define sort(v,n) sort(v,v+n)
#define vsort(v) sort(v.begin(),v.end())
#define vvsort(v) sort(v.begin(),v.end(),greater<int>())
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------------------------//
int w, h;
bool c[55][55];
int ans=0;

void dfs(int nowy, int nowx) {
	c[nowy][nowx] = 0;
	for (int i = -1; i < 2; i++) {
		for (int j = -1; j < 2; j++) {
			if (nowy == 0 && i == -1)continue;
			if (nowx == 0 && j == -1)continue;
			if (nowy == h - 1 && i == 1)continue;
			if (nowx == w - 1 && j == 1)continue;
			if (c[nowy + i][nowx + j] == 1) {
				dfs(nowy + i, nowx + j);
			}
		}
	}
}



int main() {
	while (cin >> w >> h, w) {
		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) {
					dfs(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
}