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<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cctype>

#define mp make_pair
#define pb push_back
#define REP(i,a,n) for(int i = a;i < (n);i++)
#define rep(i,n) for(int i = 0;i < (n);i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x,min,max) ((min) <= (x) && (x) <= (max))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int> > VII;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef pair<int,int> PII;

const int DX[]={-1,0,1,-1,1,-1,0,1},DY[]={-1,-1,-1,0,0,1,1,1};

int w, h;
VII cs;

void dfs(int y, int x){
	int ans = 0;
	cs[y][x] = 0;
	rep(i, 8){
		int ny = y + DY[i], nx = x + DX[i];
		if(range(ny, 0, h - 1) && range(nx, 0, w - 1) && cs[ny][nx]){
			dfs(ny, nx);
		}
	}
}

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	while(1){
		cs.clear();
		int ans = 0;
		cin >> w >> h;
		if(w == 0 && h == 0) break;
		rep(i, h){
			VI c;
			rep(j, w){
				int a;
				cin >> a;
				c.pb(a);
			}
			cs.pb(c);
		}
		rep(i, cs.size()){
			rep(j, cs[i].size()){
				if(!cs[i][j]) continue;
				dfs(i, j);
				ans++;
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

#define PI 3.141592653
#define rep(i,a,n) for(int i=a;i<(int)n;++i)
#define SZ(x) ((int)(x).size())	//size() unsigned -> int
#define descSort(a) sort(a.begin(),a.end(),std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;
ll gcd(ll x,ll y){
    if(x%y==0)return y;
    return gcd(y,x%y);
}

vector<vector<bool>> visited;
int dx[8]={-1,-1,0,1,1,1,0,-1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
int n,m;
int dfs(int x,int y,vector<vector<int>> field){
    if(visited[x][y])return 0;
    visited[x][y]=true;
    int ret=1;
    for(int i=0;i<8;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<0||ny<0||nx>=n||ny>=m)continue;
        if(visited[nx][ny]==false&&field[nx][ny]){
            ret=dfs(nx,ny,field);
        }
    }
    return ret;
}
int main(void){
    vector<int> ans;
    while(1){
        cin>>m>>n;
        if(n==0&&m==0)break;
        vector<vector<int>> field(n,vector<int>(m));
        rep(i,0,n){
            rep(j,0,m){
                cin>>field[i][j];
            }
        }
        visited=vector<vector<bool>>(n,vector<bool>(m));
        int ret=0;
        rep(i,0,n){
            rep(j,0,m){
                if(field[i][j]){
                    ret += dfs(i,j,field);
                }
            }
        }
        ans.push_back(ret);
    }
    for(auto i:ans){
        cout<<i<<endl;
    }
    return 0;
}

C++: #include<stdio.h>
int m[52][52];
void F(int x,int y)
{
	if(m[y][x])
	{
		m[y][x]=0;
		int i,j;
		for(i=-1;i<2;++i)for(j=-1;j<2;++j)F(x+i,y+j);
	}
}
int main()
{
	int h,w,i,j,r;
	while(scanf("%d%d",&w,&h),w)
	{
		for(i=1;i<=h;++i)for(j=1;j<=w;++j)scanf("%d",&m[i][j]);
		for(i=0;i<h+2;++i)m[i][w+1]=0;
		for(r=j=0;j<w+2;++j)m[h+1][j]=0;
		for(i=1;i<=h;++i)for(j=1;j<=w;++j)if(m[i][j])F(j,i),++r;
		printf("%d\n",r);
	}
	return 0;
}

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

using namespace std;
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(s) (s).begin(), (s).end()
#define dis distance
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase( unique(ALL(v)) , v.end());

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector <int> > vvi;
typedef vector<vb> vvb;
const double EPS = 1e-9;
const int MOD = 1e9+7;
const int INF = (1 << 29);
const ll LINF = 1e18;
const double PI = acos(-1);


typedef pair<int,bool> P;

int w,h;

bool check(int y, int x){
    if(y < 0 || y >= h)return false;
    if(x < 0 || x >= w)return false;
    return true;
}

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

void dfs(int y, int x, vvb &V){
    V[y][x] = false;
    REP(i,8){
        if(check(y + dy[i],x + dx[i]) && V[y + dy[i]][x + dx[i]]){
            dfs(y + dy[i], x + dx[i], V);
        }
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> w >> h;
    vi ansV;
    while(w != 0 && h != 0){
        int ans = 0;
        vvb V(h,vb(w,false));
        REP(i,h){
            REP(j,w){
                int a;
                cin >> a;
                if(a == 1)V[i][j] = true;
            }
        }
        REP(i,h){
            REP(j,w){
                if(V[i][j]){
                    dfs(i,j,V);
                    ans++;
                }
            }
        }
        ansV.push_back(ans);
        cin >> w >> h;
    }
    for(int a: ansV){
        cout << a << endl;
    }
    return 0;
}

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

typedef long long llint;
typedef long double ld;
typedef pair<int,int> P;

#define inf 1e18
#define mod 1000000007
#define sort(v) sort(v.begin(),v.end())
#define reverse(v) reverse(v.begin(),v.end())
#define rep(i,a,n) for(int i=a;i<n;i++)


priority_queue<llint,vector<llint>,greater<llint> > que;
priority_queue<llint> Que;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }


#define W 51
#define H 51

int c[H][W];


void dfs(int y,int x,int h,int w){
    c[y][x]=0;
    rep(dy,-1,2){
        rep(dx,-1,2){
            int nx=x+dx;
            int ny=y+dy;
            if(nx<0 || ny<0 || nx>=w || ny>=h)continue;
            if(c[ny][nx]==1)dfs(ny,nx,h,w);
        }
    }
}

void solve(){
    int h,w;
    while(cin >> w >> h){
        if(w==0 && h==0)break;
        rep(i,0,51)rep(j,0,51)c[i][j]=0;
        rep(i,0,h){
            rep(j,0,w){
                cin >> c[i][j];
            }
        }
        int ans=0;
        rep(i,0,h){
            rep(j,0,w){
                if(c[i][j]==1){
                    dfs(i,j,h,w);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }


    
}

int main(){
    solve();
    return 0;
}