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 <bits/stdc++.h>
using namespace std;
#define int long long
using ll=long long;
using vi=vector<int>;
using vl=vector<long long>;
using pii=pair<int,int>;
using pll=pair<long long,long long>;
#define ITR(i,c) for(auto i=begin(c);i!=end(c);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define REPW(i,n) for(i=0;i<(int)(n);++i)
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)   // c++14
#define SZ(c) ((int)c.size())
#define EXIST(c,x) (c.find(x)!=end(c))
#define OUTOFRANGE(y,x,h,w) (y<0||x<0||y>=h||x>=w)
#define dump(...)
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
#define INF (1001001001)
#define INFLL (1001001001001001001ll)
template<class T> ostream& operator << (ostream &os,const vector<T> &v) {
    ITR(i,v) os << *i << (i==end(v)-1 ? "" : " "); return os; }
template<class T> istream& operator >> (istream &is,vector<T> &v) {
    ITR(i,v) is >> * i; return is; }
template<class T> istream& operator >> (istream &is, pair<T,T> &p) {
        is >> p.first >> p.second; return is; }
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
//------------------------------------------------------------------------------
struct before_main_function {
    before_main_function() {
        #ifdef int
            #undef INF
            #define INF INFLL
            #define stoi stoll
        #endif
        cin.tie(0);ios::sync_with_stdio(false);
        cout<<setprecision(15)<<fixed;
    }
} before_main_function;
//------------------------------------------------------------------------------

void solve(int h,int w) {
    vector<vector<int>> c(h,vector<int>(w,0));
    cin>>c;
    int ans=0;
    REP(i,h) {
        REP(j,w) {
            if(c[i][j]) {
                queue<pii> q;
                q.push({i,j});
                while(!q.empty()) {
                    int y=q.front().first;
                    int x=q.front().second; q.pop();
                    REP(d,8) {
                        int yy=y+DY[d];
                        int xx=x+DX[d];
                        if(OUTOFRANGE(yy,xx,h,w)) continue;
                        if(!c[yy][xx]) continue;
                        q.push({yy,xx});
                        c[yy][xx]=0;
                    }
                }
                ans++;
            }
        }
    }
    cout<<ans<<endl;
}
signed main() {
    while(1) {
        int w,h;
        cin>>w>>h;
        if(w+h==0) break;
        solve(h,w);
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
int map[114][514];
int h, w;
bool dfs(int i, int j) {
	map[i][j] = 0;
	if (i > 0) {
		if (j > 0) {
			if (map[i - 1][j - 1] == 1)dfs(i - 1, j - 1);
		}
		if(map[i - 1][j] = 1)dfs(i - 1, j);
		if (j < w) {
			if (map[i - 1][j + 1] == 1)dfs(i - 1, j + 1);
		}
	}
	if (j > 0) {
		if (map[i][j - 1] == 1)dfs(i, j - 1);
	}
	if (j < w) {
		if (map[i][j + 1] == 1)dfs(i, j + 1);
	}
	if (i < h) {
		if (j > 0) {
			if (map[i + 1][j - 1] == 1)dfs(i + 1, j - 1);
		}
		if (map[i + 1][j] = 1)dfs(i + 1, j);
		if (j < w) {
			if (map[i + 1][j + 1] == 1)dfs(i + 1, j + 1);
		}
	}
	return;
}
int main() {
	while (cin >> h >> w) {
		if (!h && !w)return 0;
		int INF = 0;
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)cin >> map[i][j];
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)if (map[i][j] == 1) {
			INF++;
			dfs(i, j);
		}
		cout << INF << endl;
	}
}

C++: #include<iostream>
using namespace std;

void aruku(int** house,int w,int h,int row,int col){
	if(house[col][row]==0)return;
	house[col][row]=0;
	if(row+1<w)aruku(house,w,h,row+1,col);
	if(row+1<w && col+1<h)aruku(house,w,h,row+1,col+1);
	if(col+1<h)aruku(house,w,h,row,col+1);
	if(row-1>=0 && col+1<h)aruku(house,w,h,row-1,col+1);
	if(row-1>=0)aruku(house,w,h,row-1,col);
	if(row-1>=0 && col-1>=0)aruku(house,w,h,row-1,col-1);
	if(col-1>=0)aruku(house,w,h,row,col-1);
	if(row+1<w && col-1>=0)aruku(house,w,h,row+1,col-1);
return;
}

int tansaku( int** house,int w,int h){
	int row=0;int col=0;int cnt=0;
	for(int i=0;i<h;i++)
	for(int j=0;j<w;j++){
		if(house[i][j]==1){row=j;col=i;cnt++;
			aruku(house,w,h,row,col);
		}
	}
	return cnt;
}


int main(void){
int h,w;
	
	do{
	cin>>w;cin>>h;
	int** house=(int**)calloc(h,sizeof(int*));
	for(int i=0;i<h;i++)house[i]=(int*)calloc(w,sizeof(int));

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){cin>>house[i][j];}
	}
	
	int ret=tansaku(house,w,h);
	if(w!=0 && h!=0)cout<<ret<<endl;
	for(int i=0;i<h;i++)free(house[i]);
	free(house);
	}while(w!=0 && h!=0);
	
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

//???

int w,h,field[50][50],ans;

void dfs(int x,int y){
	if(x<0 || x>=w || y<0 || y>=h)return;
	if(field[x][y]==0)return;
	field[x][y]=0;
	dfs(x+1,y);
	dfs(x-1,y);
	dfs(x,y+1);
	dfs(x,y-1);
	dfs(x+1,y+1);
	dfs(x-1,y-1);
	dfs(x-1,y+1);
	dfs(x+1,y-1);
}

int main(void){
	while(1){
		ans=0;
		cin>>w>>h;
		if(w==0 && h==0)break;
		rep(i,h){
			rep(j,w){
				cin>>field[j][i];
			}
		}
		rep(i,h){
			rep(j,w){
				if(field[j][i]==1){
					dfs(j,i);
					ans++;
					// cout<<i<<","<<j<<endl;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}