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;
struct edge {
    int to;     // 辺の行き先
    int weight; // 辺の重み
    edge(int t, int w) : to(t), weight(w) { }
};
using Graph = vector<vector<int>>;
using dou =long double;
string yes="yes";
string Yes="Yes";
string YES="YES";
string no="no";
string No="No";
string NO="NO";

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
#define rep(i, n)         for(int i = 0; i < (int)(n); i++)
#define brep(n)           for(int bit=0;bit<(1<<n);bit++)
#define erep(i,container) for (auto i : container)
#define irep(i, n)        for(int i = n-1; i >= (int)0; i--)
#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)
#define reprep(i,j,h,w) rep(i,h)rep(j,w)
#define all(x) (x).begin(),(x).end()
#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
#define sum(a) accumulate(all(a),0ll)
#define keta fixed<<setprecision
#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))
#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));
#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));
#define case std::cout << "Case #" <<qqq+1<<": "
#define res resize
#define as assign
#define ffor for(;;)
#define ppri(a,b) std::cout << a<<" "<<b << std::endl
#define pppri(a,b,c) std::cout << a<<" "<<b <<" "<< c<<std::endl
#define aall(x,n) (x).begin(),(x).begin()+(n)
 
typedef long long ll;
typedef pair<int,int> P;
//typedef long long T;
const int INF = 2000000000;
const ll INF64 = 922337203685477580ll;
const ll MOD = 1000000007ll;
int w,h;
void dfs(std::vector<std::vector<int> > &a,int y,int x){
    a[y][x]=0;
    rrep(i,max(0,y-1),min(y+2,h)){
        rrep(j,max(0,x-1),min(x+2,w)){
            if(y==i&&x==j)continue;
            if(a[i][j]==0)continue;
            dfs(a,i,j);
        }
    }
}
int main(){
    for(;;){
        
        std::cin >> w>>h;
        if(w==0)exit(0);
        vvector(a,int,h,w,0);
        reprep(i,j,h,w)std::cin >> a[i][j];
        int ans=0;
        reprep(i,j,h,w){
            if(a[i][j]){
                ans++;
                dfs(a,i,j);
            }
        }
        std::cout << ans << std::endl;
    }
    
    
}

C++: #include<iostream>

using namespace std;

int h, w, map[51][51];

const int vx[] = {1,0,-1,0}, vy[] = {0,1,0,-1};

void island(int _w, int _h) {
	map[_w][_h] = 2;
	for (int i = 0;i < 4;i++) {
		if (_w + vy[i] >= 1 && _w + vy[i] <= w&&_h + vx[i] >= 1 && _h + vx[i] <= h&&map[_w+vy[i]][_h+vx[i]] == 1) {
			island(_w + vy[i], _h + vx[i]);
		}
	}
}

int main() {
	while (cin >> w >> h, w + h) {
		for (int i = 1;i <= w;i++) {
			for (int j = 1;j <= h;j++) {
				cin >> map[i][j];
			}
		}
		int count = 0;
		for (int i = 1;i <= w;i++) {
			for (int j = 1;j <= h;j++) {
				if (map[i][j] == 1) {
					count++, island(i, j);
				}
			}
		}
		cout << count << endl;
		for (int i = 1;i <= w;i++)for (int j = 1;j <= h;j++)map[i][j] = -1;
	}
	return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160
// How Many Islands?

#define SUBMIT

#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;
using ui64 = unsigned long long;
using i64 = long long;

enum class space_type {
    sea = 0,
    land = 1
};

const int NOT_GROUPED = -1;
const int MAX_SIDE = 50;
space_type field[MAX_SIDE][MAX_SIDE];
int field_id[MAX_SIDE][MAX_SIDE];
int h, w;
int n_groups;

void dfs(int y, int x) {
    if (y < 0 || h <= y || x < 0 || w <= x) return;
    if (field[y][x] == space_type::sea) return;
    if (field_id[y][x] != NOT_GROUPED) return;

    field_id[y][x] = n_groups;

    dfs(y - 1, x - 1);
    dfs(y - 1, x);
    dfs(y - 1, x + 1);

    dfs(y, x - 1);
    dfs(y, x + 1);

    dfs(y + 1, x - 1);
    dfs(y + 1, x);
    dfs(y + 1, x + 1);
}

void start_dfs() {
    for (int y = 0; y < h; ++y) {
        for (int x = 0; x < w; ++x) {
            field_id[y][x] = NOT_GROUPED;
        }
    }

    n_groups = 0;
    for (int y = 0; y < h; ++y) {
        for (int x = 0; x < w; ++x) {
            if (field[y][x] == space_type::land && field_id[y][x] == NOT_GROUPED) {
                dfs(y, x);
                n_groups++;
            }
        }
    }
}

int main() {
#ifdef SUBMIT
    auto& stream = cin;
#else
    string program_input_str = R"(1 1
0
2 2
0 1
1 0
3 2
1 1 1
1 1 1
5 4
1 0 1 0 0
1 0 0 0 0
1 0 1 0 1
1 0 0 1 0
5 4
1 1 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 1 1
5 5
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0
)";
    stringstream stream(program_input_str);
#endif
    while (true) {
        stream >> w >> h;
        if (h == 0 && w == 0) break;

        int c;
        for (int y = 0; y < h; ++y) {
            for (int x = 0; x < w; ++x) {
                stream >> c;
                field[y][x] = static_cast<space_type>(c);
            }
        }

        start_dfs();
        cout << n_groups << endl;
    }

    return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int main() {
	vvi map(52, vi(52, 0));

	int w, h;

	while(cin >> w >> h, (w!=0 && h!=0)) {
		FOR(i, 1, h+1) {
			FOR(j, 1, w+1) {
				int p;
				cin >> p;
				map[i][j] = p;
			}
		}
	
		int num = 0;

		FOR(i, 1, h+1) {
			FOR(j, 1, w+1) {
				if(map[i][j]) {
					if(map[i][j+1] || map[i+1][j-1] || map[i+1][j] || map[i+1][j+1]) {

					} else {
						num++;
					}
				}
			}
		}

		cout << num << endl;

	}
}