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++: #pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace::std;
#define rep(i, n) for(lint i = 0; i < (lint)(n); i++)
#define repi(i,a,b) for(lint i=lint(a);i<lint(b);i++)
typedef long long lint;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<typename T=lint> T in(){
    return *istream_iterator<T>(cin);
}

vector<lint> dx={-1,1,0,0,1,1,-1,-1};
vector<lint> dy={0,0,-1,1,1,-1,1,-1};
int main(){
    while(1){
        lint w,h;
        cin>>w>>h;
        if(w==0&&h==0)break;
        vector<vector<lint>>v(h,vector<lint>(w));
        rep(i,h)rep(j,w){
            cin>>v[i][j];
        }
        lint ans=0;
        rep(i,h)rep(j,w){
            if(v[i][j]==0)continue;
            ans++;
            stack<pair<lint,lint>>s;
            s.emplace(i,j);
            while(!s.empty()){
                lint r,c;
                tie(r,c)=s.top();
                s.pop();
                rep(k,8){
                    if(r+dy[k]<0||h<=r+dy[k]||c+dx[k]<0||w<=c+dx[k])continue;
                    if(v[r+dy[k]][c+dx[k]]==1){
                        v[r+dy[k]][c+dx[k]]=0;
                        s.emplace(r+dy[k],c+dx[k]);
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
}

C++: 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define unsigned long long 
#define rep(i,a,b) for(ull (i)=(a);(i)<(b);i++)
void dfs(int w, int h);
using namespace std;
int field[50][50];
int x, y;
int cnt;
int main(void){
	while (true){
		cin >> x >> y;
		if (x == 0)
			break;
		cnt = 0;
		for (int i = 0; i < y; i++){
			for (int j = 0; j < x; j++){
				cin >> field[i][j];
			}
		}
		for (int i = 0; i < y; i++){
			for (int j = 0; j < x; j++){
				if (field[i][j] == 1){
					cnt++;
					dfs(i, j);
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}


void dfs(int h, int w){
	field[h][w] = 0;

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

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

vector<int> dx = {1, 1, 1, -1, -1, -1, 0, 0}, dy = {1, -1, 0, 1, -1, 0, 1, -1};
int w, h;
vector<vector<bool>> field;

void dfs(int x, int y)
{
  field.at(y).at(x) = false;
  rep(direction, 8)
  {
    int nx = x + dx.at(direction), ny = y + dy.at(direction);
    if (0 <= nx && nx < w && 0 <= ny && ny < h && field.at(ny).at(nx))
    {
      dfs(nx, ny);
    }
  }
}

int main()
{
  while (1)
  {
    cin >> w >> h;
    if (!w && !h)
    {
      break;
    }
    field.resize(h, vector<bool>(w));
    field.assign(h, vector<bool>(w, false));
    rep(y, h)
    {
      rep(x, w)
      {
        int c;
        cin >> c;
        if (c)
        {
          field.at(y).at(x) = true;
        }
      }
    }
    int ans = 0;
    rep(y, h)
    {
      rep(x, w)
      {
        if (field.at(y).at(x))
        {
          dfs(x, y);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}

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

using namespace std;

// Repetitions
#define FOR(i,a,n) for(ll i = ((ll) a); i < ((ll) n); ++i)
#define FORR(i, s, g) for(ll i = ((ll)s - 1); i >= ((ll) g); --i)
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) FORR(i, n, 0)

// Container Utils
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define SORTALL(v) sort(ALL(v))
#define SZ(a) int((a).size())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())

// For Debug
#define DMP(x)  cerr << #x << " = " << (x) << endl
#define DBG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl

// Aliases
#define PB push_back
#define MP make_pair
typedef long long ll;
typedef string str;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

// I/O Asynchronizing
struct aaa{
    aaa(){
        cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);
    };
}aaaaaaa;

#define MAX_H 50
#define MAX_W 50

int h, w;
bool is_land[MAX_H][MAX_W] = { false };

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

inline bool is_in_range(const int y, const int x) {
    return 0 <= y && y < h && 0 <= x && x < w;
}

void dfs(const int y, const int x) {
    is_land[y][x] = false;
    REP(i, 8) {
        int nx = x + dx[i], ny = y + dy[i];
        if(is_in_range(ny, nx) && is_land[ny][nx]) dfs(ny, nx);
    }
}

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

    return 0;
}