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;
vector<string>v;
vector<int>ansl;
string s;
int ary[100005] = {0};
int mp[51][51];
int cnt = 0;
int d_x[] = {0,1,0,-1};
int d_y[] = {1,0,-1,0};
int W,H;

void dfs(int x,int y){
  mp[y][x] = 0;
  for(int dx = -1;dx<2;dx++){
    for(int dy = -1;dy<2;dy++){
    int nx = x+dx,ny = y+dy;
    if((nx>=0)&&(ny>=0)&&(ny<H)&&(nx<W)&&(mp[ny][nx] == 1)){
      dfs(nx,ny);
    }
  }
}
}
int main(){
  cin >>W>>H;

  while((W!=0)&&(H!=0)){
    cnt = 0;
    for(int i = 0;i<51;i++){
      for(int j = 0;j<51;j++){
        mp[i][j] = 0;
      }
    }
    for(int i = 0;i<H;i++){
      for(int j = 0;j<W;j++){
        cin >>mp[i][j];
      }
    }
    for (int y = 0; y < H; y++){
      for (int x = 0; x < W; x++) {
        if (mp[y][x] == 1) {
          dfs(x, y);
          cnt++;
        }
      }
    }
    cout <<cnt<<endl;
    cin >>W>>H;
  }
  return 0;
}

C++: //default
#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
typedef pair<int, int> P_i;
typedef pair<double,double> P_d;
#define rep(i,n) for(int i=0;i<(n);++i)
#define alrep(i,j,n) for(int i=0;i<(n);++i)for(int j=i+1;j<(n);++j)
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define answer cout << ans << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
const int MOD=1e9+7;
const long long INF = 1LL << 60;

bool seen[55][55];
int h,w,ans=0;
char field[55][55];
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};

void dfs(int x,int y) {
	seen[x][y] = true; // v を訪問済にする
	// v から行ける各頂点 next_v について
	rep(i,8){
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(nx<0 || nx>=w || ny<0 || ny>=h) continue;
		if(seen[nx][ny]==true) continue;
		if(field[nx][ny]=='0') continue;
		dfs(nx,ny);
	}
}

signed main(){
	while(cin >> w >> h && (w!=0 || h!=0)){
		ans=0;
		rep(y,h)rep(x,w){
			cin >> field[x][y];
		}
		memset(seen,false,sizeof(seen));
		rep(y,h)rep(x,w){
			if(seen[x][y]==true) continue;
			if(field[x][y]=='0') continue;
			ans++;
			dfs(x,y);
		}
		cout << ans << endl;
	}

	return 0;
}

C++: 
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define veci vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vec>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))


using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
//template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
//template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
    for(T& x:vec) is >> x;
    return is;
}
const ll INF=1e9+7;

ll c[100][100];
ll w,h;

void dfs(ll y,ll x){
    c[y][x]=0;
    for(ll p=-1;p<=1;p++){
        for(ll q=-1;q<=1;q++){
            if(p==0 && q==0) continue;
            ll ny=y+p;
            ll nx=x+q;
            if(0<=ny && ny<=h-1 && 0<=nx && nx<=w-1 && c[ny][nx]==1){
                dfs(ny,nx);
            }
        }
    }
}

int main()
{
    while(true){
        cin >> w >> h;
        if(w==0 && h==0){
            break;
        }
        rep(i,h){
            rep(j,w){
                cin >> c[i][j];
            }
        }
        ll cnt=0;
        rep(i,h){
            rep(j,w){
                if(c[i][j]==1){
                    dfs(i,j);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>

using namespace std;
typedef std::vector<std::vector<int> > Field;
typedef std::vector<std::vector<bool> > Seen;

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

// DFS
bool dfs(const Field& field, Seen& seen, const int ih, const int iw, const int H, const int W) {
    seen[ih][iw] = true;
    if (field[ih][iw] == 0) return false;
    // search oct
    for (int i = 0; i < 8; ++i) {
        const int curh = ih + dx[i];
        const int curw = iw + dy[i];

        // check the boundaries
        if (curh >= H || curh < 0) continue;
        if (curw >= W || curw < 0) continue;

        if (seen[curh][curw]) continue;

        dfs(field, seen, curh, curw, H, W);
    }
    return true;
}

int search(const Field& field, const int H, const int W) {
    Seen seen(H, std::vector<bool>(W, false));
    int cnt = 0;
    for (int ih = 0; ih < H; ++ih) {
        for (int iw = 0; iw < W; ++iw) {
            if (seen[ih][iw]) continue;
            if (field[ih][iw] == 0) continue;
            if (dfs(field, seen, ih, iw, H, W)) ++cnt;
        }
    }
    return cnt;
}

int main() {
    vector<int> res;
    for (;;) {
        int W, H; cin >> W >> H;
        if ( (W == 0 || H == 0) ) break;
        Field field(H, std::vector<int>(W, 0));
        for (int ih = 0; ih < H; ++ih) {
            for (int iw = 0; iw < W; ++iw) {
                cin >> field[ih][iw];
            }
        }
        res.push_back(search(field, H, W));
    }
    for (unsigned int i = 0; i < res.size(); ++i) {
        cout << res[i] << "\n";
    }
    return 0;
}

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

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

void dfs(int y, int x)
{
	fld[y][x] = 0;
	for(int i=0; i<8; i++)
	{
		int ny = y+dy[i];
		int nx = x+dx[i];

		if(fld[ny][nx])
		{
			dfs(ny,nx);
		}
	}
}


int main()
{
	int w,h,c;
	while(cin >> w >> h, w||h)
	{
		memset(fld,0,sizeof(fld));

		for(int y=1; y<=h; y++)
		for(int x=1; x<=w; x++)
		{
			cin >> fld[y][x];
		}

		c=0;
		for(int y=1; y<=h; y++)
		for(int x=1; x<=w; x++)
		{
			if(fld[y][x])
			{
				c++;
				dfs(y,x);
			}
		}

		cout << c << endl;
	}
}