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 <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
#include <utility>
#include <iomanip>

#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9+7;

signed main(){
    while(true){
        int w, h;
        cin >> w >> h;

        if(w == 0 && h == 0){
            break;
        }

        vector<vector<bool>> area(h, vector<bool>(w, false));
        rep(i, h){
            rep(j, w){
                int x;
                cin >> x;
                if(x == 1){
                    area[i][j] = true;
                }
            }
        }

        vector<vector<int>> mark(h, vector<int>(w, 0));
        int c = 1;

        vector<int> dx = {1, 0, -1, 0, 1, 1, -1, -1};
        vector<int> dy = {0, 1, 0, -1, 1, -1, 1, -1};

        rep(i, h){
            rep(j, w){
                if(mark[i][j] != 0 || not area[i][j]){
                    continue;
                }

                stack<P> st;
                st.emplace(i, j);

                while(not st.empty()){
                    P p = st.top();
                    st.pop();

                    int x = p.first;
                    int y = p.second;

                    if(mark[x][y] != 0 || not area[i][j]){
                        continue;
                    }

                    mark[x][y] = c;

                    rep(k, 8){
                        int nx = x + dx[k];
                        int ny = y + dy[k];
                        if(0 <= nx && nx < h && 0 <= ny && ny < w && area[nx][ny]){
                            st.emplace(nx, ny);
                        }
                    }
                }
                c++;
            }
        }
        cout << c - 1 << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int,int> pint;

#define DE 1
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define ALL(s) (s).begin(),(s).end()
#define REP(i,n) for (int i = 0; i < (int)(n); ++i)
#define EACH(i,s) for (typeof((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl

template<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<", "<<P.second<<'>';}
template<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<"{ ";for(int i=0;i<P.size();++i){if(i>0)s<<", ";s<<P[i];}return s<<" }"<<endl;}
template<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<"{ ";for(typeof(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<", ";s<<'<'<<it->first<<"->"<<it->second<<'>';}return s<<" }"<<endl;}



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

int w,h;
int C[100][100];
bool seen[100][100];

int res = 0;
void dfs(int x, int y) {
    seen[x][y] = true;
    
    for (int i = 0; i < 8; ++i) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
        if (!seen[nx][ny] && C[nx][ny] == 1) {
            dfs(nx, ny);
        }
    }
}

int main() {
    //freopen( "/Users/macuser/Documents/Programming/Contest/input.in", "r", stdin );
    
    while (cin >> w >> h) {
        if (w == 0) break;
        
        memset(C, 0, sizeof(C));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                cin >> C[i][j];
            }
        }
        
        res = 0;
        memset(seen, 0, sizeof(seen));
        
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                if (!seen[i][j] && C[i][j] == 1) {
                    dfs(i, j);
                    ++res;
                }
            }
        }
        
        cout << res << endl;
    }
    
    return 0;
}

C++: #pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template <typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template <typename T>
void print(T x) {
    cout << x << "\n";
}

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9+7;

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

    while(true) {
        int w, h; cin >> w >> h;

        if(w == 0 && h == 0) break;

        vector<vi> m(h, vi(w));
        REP(i, h) REP(j, w) cin >> m[i][j];

        vector<vector<bool>> visit(h, vector<bool>(w, false));

        vi dx = {0, 1, -1}, dy = {0, 1, -1};
        auto check = [&] (int x, int y) -> bool {
            return x >= 0 && x < w && y >= 0 && y < h;
        };

        auto dfs = [&] (auto &&f, int cx, int cy) -> void {
            visit[cy][cx] = true;

            REP(i, 3) REP(j, 3) {
                int nx = cx + dx[i], ny = cy + dy[j];
                if (check(nx, ny)){
                    if(!visit[ny][nx] && m[ny][nx] == 1) {
                        f(f, nx, ny);
                    }
                }
            }
        };

        int ans = 0;
        REP(i, h) REP(j, w) {
            if (m[i][j] == 1 && !visit[i][j]) dfs(dfs, j, i), ans++;
        }

        print(ans);
    }

}