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 <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <tuple>
#include <set>
#include <stack>
#include <queue>
#define REP(i, n) for(LL i = 0;i < n;i++)
#define REPR(i, n) for(LL i = n;i >= 0;i--)
#define FOR(i, m, n) for(LL i = m;i < n;i++)
#define FORR(i, m, n) for(LL i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

#define INF 999999999
#define MOD 1000000007
using namespace std;
typedef long long LL;
//typedef LL int;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> PP;
typedef pair<LL, LP> LPP;
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

int main(){
    int w, h;
    while(cin >> w >> h, w + h){
        int c[51][51];
        REP(i, h){
            REP(j, w){
            cin >> c[i][j];
            }
        }

        int ans = 0;

        REP(i, h){
            REP(j, w){
                if(c[i][j] == 2) continue;
                else if(c[i][j] == 0) continue;
                else if(c[i][j] == 1){
                    queue<int> que;
                    que.push(i); que.push(j);
                    
                    while(!que.empty()){
                        int x, y;
                        y = que.front(); que.pop();
                        x = que.front(); que.pop();
                        
                        c[y][x] = 0;

                        if(y-1 >= 0 && y-1 < h && x-1 >= 0 && x-1 < w && c[y-1][x-1] == 1){
                            que.push(y-1); que.push(x-1);
                        }else if(y-1 >= 0 && y-1 < h && x >= 0 && x < w && c[y-1][x] == 1){
                            que.push(y-1); que.push(x);
                        }else if(y-1 >= 0 && y-1 < h && x+1 >= 0 && x+1 < w && c[y-1][x+1] == 1){
                            que.push(y-1); que.push(x+1);
                        }else if(y >= 0 && y < h && x-1 >= 0 && x-1 < w && c[y][x-1] == 1){
                            que.push(y); que.push(x-1);
                        }else if(y >= 0 && y < h && x+1 >= 0 && x+1 < w && c[y][x+1] == 1){
                            que.push(y); que.push(x+1);
                        }else if(y+1 >= 0 && y+1 < h && x-1 >= 0 && x-1 < w && c[y+1][x-1] == 1){
                            que.push(y+1); que.push(x-1);
                        }else if(y+1 >= 0 && y+1 < h && x >= 0 && x < w && c[y+1][x] == 1){
                            que.push(y+1); que.push(x);
                        }else if(y+1 >= 0 && y+1 < h && x+1 >= 0 && x+1 < w && c[y+1][x+1] == 1){
                            que.push(y+1); que.push(x+1);
                        }
                    }
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }
}

C++: #include "bits/stdc++.h"

#define REP(i,num) for(ll i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){T x; cin >> x; return (x);}
template<typename T=ll,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

class ConnectNodeInfo{
	vector<vector<pair<ll,ll>>> graph;
public:
	ConnectNodeInfo(int node_num){
		graph.resize(node_num);
	}
	void AddNonDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
		graph[v].emplace_back(u,w);
	}
	void AddDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
	}
	vector<pair<ll,ll>>& operator[](ll index){
		return graph[index];
	}
	size_t size(){return graph.size();}
};

vector<int> D,F;

int dfs(int p,int num,int H,int W,vector<vector<int>>& M,vector<vector<int>>& D){
	int u=p%W,v=p/W;
	D[v][u] = num;
	if(u>0 && M[v][u-1] && D[v][u-1]==-1){
		dfs(u-1+v*W,num,H,W,M,D);
	}
	if(u<W-1 && M[v][u+1] && D[v][u+1]==-1){
		dfs(u+1+v*W,num,H,W,M,D);
	}
	if(v>0 && M[v-1][u] && D[v-1][u]==-1){
		dfs(u+(v-1)*W,num,H,W,M,D);
	}
	if(v<H-1 && M[v+1][u] && D[v+1][u]==-1){
		dfs(u+(v+1)*W,num,H,W,M,D);
	}

	if(u>0 && v>0 && M[v-1][u-1] && D[v-1][u-1]==-1){
		dfs(u-1+(v-1)*W,num,H,W,M,D);
	}
	if(u<W-1 && v<H-1 && M[v+1][u+1] && D[v+1][u+1]==-1){
		dfs(u+1+(v+1)*W,num,H,W,M,D);
	}
	if(v>0 && u<W-1 && M[v-1][u+1] && D[v-1][u+1]==-1){
		dfs(u+1+(v-1)*W,num,H,W,M,D);
	}
	if(v<H-1 && u>0 && M[v+1][u-1] && D[v+1][u-1]==-1){
		dfs(u-1+(v+1)*W,num,H,W,M,D);
	}
	return 0;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	while(true){
		int W=in(),H=in();
		if(!W && !H) break;

		vector<vector<int>> M(H,vector<int>(W,0)),D(H,vector<int>(W,-1));
		REP(i,H){
			REP(j,W){
				M[i][j]=in();
			}
		}

		int num=0;
		REP(i,H){
			REP(j,W){
				if(M[i][j] && D[i][j]==-1){
					dfs(j+i*W,++num,H,W,M,D);
				}
			}
		}
		out(num);
	}

	return 0;
}