Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
const int dx[8] = {0,1,1,1,0,-1,-1,-1};
const int dy[8] = {1,1,0,-1,-1,-1,0,1};
int w,h,grid[51][51];
void s(int x, int y){
  grid[y][x] = 0;
  REP(r,8){
    if(x+dx[r]>=0&&x+dx[r]<w&&y+dy[r]>=0&&y+dy[r]<h&&grid[y+dy[r]][x+dx[r]]){
      s(x+dx[r],y+dy[r]);
    }
  }
} 


int main(void){
  while(cin>>w>>h){
    if(w==0) break;
    REP(y,h)
      REP(x,w)
        cin>>grid[y][x];
    int c = 0;
    REP(y,h)
      REP(x,w)
        if(grid[y][x]){
          c++;
          s(x,y);
        }
    cout<<c<<endl;
  }
  return 0;
}

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,P> PP;

//-----------------------------------------------------------------------------

int h,w;
int fld[50][50];
int dx[]={1,-1,0,0,1,1,-1,-1},dy[]={0,0,1,-1,-1,1,-1,1};

void bfs(int sy,int sx) {
	queue<P> q;
	fld[sy][sx]=0;
	q.push(P(sx,sy));
	while(!q.empty()) {
		P p=q.front();q.pop();
		REP(i,8) {
			int nx=p.fi+dx[i],ny=p.se+dy[i];
			if(nx<0||nx>=w||ny<0||ny>=h||fld[ny][nx]==0) continue;
			fld[ny][nx]=0;
			q.push(P(nx,ny));
		}
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(true) {
		cin>>w>>h;
		if(w==0) break;
		REP(i,h) REP(j,w) cin>>fld[i][j];
		int cnt=0;
		REP(i,h) {
			REP(j,w) {
				if(fld[i][j]==1) {
					cnt++;
					bfs(i,j);
				}
			}
		}
		cout<<cnt<<endl;
	}

	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#ifndef FAST_IO
 #define IOS ios_base::sync_with_stdio(false);cin.tie(nullptr);
#endif

#define int long long
#define FILE_IO freopen("input.txt","r",stdin)/#define int long long
#define f first
#define s second
#define endl '\n'
#define all(c) c.begin(),c.end()
#define bp __builtin_popcount
#define debug(x) cerr << #x << " = " << x << endl;

const int MOD = 1e9 + 7;
const int INF = 1e18;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>void print(T t){for (auto &it : t) cout << it << " ";}
inline int powerexp(int a, int b, int m = MOD){ int res = 1;while(b){if(b&1)res = res * a % m;a = a * a % m,b >>= 1;}return res % m;}
inline int modinv(int a){ return powerexp(a,MOD-2,MOD);}
inline int lcm(int a, int b){ int g = __gcd(a,b);return a/g*b;}
inline int add(int x, int y){ x += y; if (x >= MOD) x -=  MOD; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % MOD;}
inline int sub(int x, int y){ x -= y; if (x<0) x += MOD; return x ;}

//-------------------< start here >-------------------------// 

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

void solve(){
    int w, h;
    while (cin >> w >> h, w && h){
		int g[h][w];
		bool visited[h][w];
		for (int i = 0; i < h; i++){
		  for (int j = 0; j < w; j++){
			  cin >> g[i][j]; 
			}	
		}
	  memset(visited, false, sizeof visited);
	  int ret = 0;
	  for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
			if (g[i][j] == 1 && !visited[i][j]){
			  ret++;
			  queue< pair<int, int> > q;
			  q.push({i, j});
			  while (!q.empty()){
				 auto p = q.front();
				 q.pop();
				 int ny = p.f, nx = p.s;
				 for (int k = 0; k < 8; k++){
					 int X = nx + dx[k];
					 int Y = ny + dy[k];
					 if (X >= 0 && Y >= 0 && X < w && Y < h){
					  if (g[Y][X] == 1 && !visited[Y][X]){
						   visited[Y][X] = true;
						   q.push({Y, X}); 
						 }	 
					  } 
				   }  
				}	
			 }
		  } 
		}
	 cout << ret << endl;	
	}
}

//-----------------------------
//comment from here for TOPCODER 
int32_t main(){
   IOS;
   solve();
   //find();
   #ifdef LOCAL_DEFINE
   cerr << "time elapsed " << (double)clock()/CLOCKS_PER_SEC << endl;
   #endif
}

C++: //
// Created by elegant-belltree on 15/06/03.
//

#include <iostream>
#include <set>

using namespace std;

int main(){
    int w,h;
    int c[50][50];

    while (cin>>w>>h && (w>0 || h>0)) {
        int label=2;
        set<int> labels;
        int n = -1;
        int count;

        do{
            n++;
            count=0;
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    if(n==0) {
                        cin >> c[j][i];
                    }
                    if (0<c[j][i]) {
                        for (int k = -1; (n==0 && k < 1) || (0<n && k<2); ++k) {
                            for (int l = -1; (0<n || k!=0 || l!=0) && l < 2 && j + l < w; ++l) {
                                if (-1 < i + k && -1 < j + l && 0 < c[j + l][i + k] && (c[j][i] == 1 || c[j + l][i + k] < c[j][i])) {
                                    if(0<n){
                                        labels.erase(c[j][i]);
                                        count++;
                                    }
                                    c[j][i] = c[j + l][i + k];
                                }
                            }
                        }
                        if (c[j][i] == 1) {
                            c[j][i] = label++;
                            labels.insert(c[j][i]);
                        }
                    }
                }
            }
        }while(n==0 || 0<count);
        /*for (int m = 0; m < h; ++m) {
            for (int i = 0; i < w; ++i) {
                cout<<c[i][m]<<" ";
            }
            cout<<endl;
        }*/
        cout<<labels.size()<<endl;
    }
    return 0;
}