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;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define CMAX(m, x)  m = max(m,x);
#define CMIN(m, x)  m = min(m,x);
#define itn int
typedef long long lint;
typedef long double ldouble;
const int INF = 1e9;
const lint LINF = 1e18;

int cnt = 0;
bool dfs(vector<vector<int> > &bi, vector<vector<int> > &flag,int x, int y){
    bool re = false;
    if(bi[y][x] == 1){
        if(flag[y][x] == 0){
            flag[y][x] = cnt;
            re = true;
            for(int i = -1;i <= 1; i++){
                for(int j = -1; j <= 1; j++){
                    dfs(bi,flag,x + i,y + j);
                }
            }
        }
    }
    return re;
}
int main(){
    int w, h;
    vector<int> ans;
    while(1){
        cin >> w >> h;
        if(w == 0 && h == 0)
            break;
        w += 2;
        h += 2;
        vector<vector<int> > bi(h,vector<int>(w));
        vector<vector<int> > flag(h,vector<int>(w,0));
        REP(i,h - 2){
            REP(j,w - 2)
                cin >> bi[i + 1][j + 1];
        }
        REP(i,h){
            bi[i][0] = 0;
            bi[i][w -1] = 0;
        }
        REP(i,w){
            bi[0][i] = 0;
            bi[h - 1][i] = 0;
        }
        REP(i,h){
            REP(j,w){
                if(bi[i][j] == 1 && flag[i][j] == 0){
                    cnt++;
                    if(!dfs(bi,flag,j,i)) cnt --;
                }
            }
        }
        ans.push_back(cnt);
        cnt = 0;
    }
    
    REP(i,ans.size())
        cout << ans[i] << endl;
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define REP(i,a,b) for(i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) (a).begin(),(a).end()
#define ifnot(a) if(not a)
#define dump(x)  cerr << #x << " = " << (x) << endl
using namespace std;

void reader(int &a) { scanf("%d", &a); }
void reader(double &a) { scanf("%lf", &a); }
void reader(char a[]) { scanf("%s", a); }
void reader(char &a) { scanf(" %c", &a); }
void reader(ll &a) { scanf("%lld", &a); }
void reader(ull &a) { scanf("%llu", &a); }
// void reader(string& a){cin >> a;};
template<class T, class U> void reader(T& t, U& u) { reader(t); reader(u); }
template<class T, class U, class V> void reader(T& t, U& u, V& v) { reader(t); reader(u); reader(v); }

void writer(const int a, char c) { printf("%d", a); putchar(c); }
void writer(const ll a, char c) { printf("%lld", a); putchar(c); }
void writer(const ull a, char c) { printf("%llu", a); putchar(c); }
void writer(const double a, char c) { printf("%.20lf", a); putchar(c); }
void writer(const char a[]) { printf("%s", a); };
void writer(const char a[], char c) { printf("%s", a); putchar(c); };
void writer(const char a, char c) { putchar(a); putchar(c); };
template<class T> void writerLn(T t) { writer(t, '\n'); }
template<class T, class U> void writerLn(T t, U u) { writer(t, ' '); writer(u, '\n'); }
template<class T, class U, class V> void writerLn(T t, U u, V v) { writer(t, ' '); writer(u, ' '); writer(v, '\n'); }
template<class T> void writerArr(T x[], int n) { int i; if (!n) { putchar('\n'); return; }rep(i, n - 1) writer(x[i], ' '); writer(x[n - 1], '\n'); }
template<class T> void writerVec(vector<T> x) { int n = x.size(); int i; if (!n) { putchar('\n'); return; }rep(i, n - 1) writer(x[i], ' '); writer(x[n - 1], '\n'); }

vector<string> split(const string &str, char sep) { vector<string> v; stringstream ss(str); string buffer; while (getline(ss, buffer, sep)) { v.push_back(buffer); }return v; }
// #define int ll
bool test = 1;
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
#define MAX MAX000
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................

int W, H;
char c[100][100];
void grid_dfs(int y, int x) {
	int i, j;
	c[y][x] = '0';
	for (i = -1; i <= 1; i++) {
		for (j = -1; j <= 1; j++) {
			int nx = x + i;
			int ny = y + j;
			if (nx < 0 || W <= nx) continue;
			if (ny < 0 || H <= ny) continue;
			if (c[ny][nx] == '0') continue;
			grid_dfs(ny, nx);
		}
	}
}
signed main(void)
{
	int i, j, k;
	while (1) {
		cin >> W >> H;
		if (W == 0) continue;
		rep(i, H) rep(j, W) {
			reader(c[i][j]);
		}
		/*rep(i, H) {
			writerArr(c[i],W);
		}*/
		int cnt = 0;
		rep(i, H) rep(j, W) {
			if (c[i][j] == '1') {
				cnt++;
				grid_dfs(i, j);
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

C++:     #include<iostream>
    #include<stdio.h>
    //#include <bits/stdc++.h>
    #include<vector>
    #include<float.h>
    #include<iomanip>
    #include<algorithm>
    #include<string>
    #include<cstring>
    #include<math.h>
    #include<cmath>
    #include<sstream>
    #include<set>
    #include<map>
    #include<queue>
    #include<cassert>
    #include<cmath>
    #include<cstdint>
using namespace std;

bool seen[55][55];
vector<vector<int>> s(55, vector<int>(55));
int w, h;
void dfs(int y, int x){
    seen[y][x] = true;

    for(int i = -1; i <= 1; i++){
        for(int j = -1; j <= 1; j++){
            if(i == 0 && j == 0)continue;
            int ny = y + i, nx = x + j;
            
            if(ny < 0 || ny >= h || nx < 0 || nx >= w)continue;
            if(seen[ny][nx])continue;
            if(s[ny][nx] != 1)continue;
            dfs(ny, nx);
        }
    }
}
int main(){
    while(cin >> w >> h){
    if(w == 0 && h == 0)return 0;
    for(int i = 0; i < h; i++)for(int j = 0; j < w; j++){cin >> s[i][j];seen[i][j] = false;}
    int cnt = 0;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(seen[i][j])continue;
            if(s[i][j] == 0)continue;
            cnt++;
            dfs(i, j);
        }
    }
    cout << cnt << endl;
    }
}