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<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<deque>
#include<numeric>
#include<queue>
#include<stack>
#include<cstring>
#include<limits>
#include<functional>
#include<unordered_set>
#include<iomanip>
#include<cassert>
#include<regex>
#include<bitset>
#include<complex>
#include<chrono>
#include<random>
#define rep(i,a) for(int i=(int)0;i<(int)a;++i)
#define rrep(i,a) for(int i=(int)a-1;i>=0;--i)
#define REP(i,a,b) for(int i=(int)a;i<(int)b;++i)
#define RREP(i,a,b) for(int i=(int)a-1;i>=b;--i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(),x.end()
using ll=long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

ll gcd(ll n, ll m) {
	ll tmp;
	while (m!=0) {
		tmp = n % m;
		n = m;
		m = tmp;
	}
	return n;
}

ll lcm(ll n, ll m) {
	return abs(n) / gcd(n, m)*abs(m);//gl=xy
}
 
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;
}
 
using namespace std;

vector<vector<bool>>visited;
int h,w;
vector<vector<int>>c;

void dfs(int x,int y){
  visited[x][y]=true;
  for(int i=-1;i<=1;++i){
    for(int j=-1;j<=1;++j){
      if(i==0&&j==0)continue;
      if(x+i>=0&&x+i<h&&y+j>=0&&y+j<w){
        if(c[x+i][y+j]==1){
          if(!visited[x+i][y+j])dfs(x+i,y+j);
        }
      }
    }
  }
}

void solve(){
  visited.assign(h,vector<bool>(w));
  c.assign(h,vector<int>(w));
  rep(i,h)rep(j,w)cin>>c[i][j];
  int ans=0;
  rep(i,h){
    rep(j,w){
      if((!visited[i][j])&&(c[i][j]==1))dfs(i,j),++ans;
    }
  }
  cout<<ans<<"\n";
}

int main(){
	ios::sync_with_stdio(false);
  cin.tie(0);
	cout<<fixed<<setprecision(15);
  while(1){
    cin>>w>>h;
    if(h==0&&w==0)return 0;
    solve();
  }
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <sstream>
#include <cmath>
#include <set>
#include <map>
using namespace std; 

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define len(x) ((int)(x).size())
#define tmax(a,b,c) max((a),max((b),(c)))
#define tmin(a,b,c) min((a),min((b),(c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<vector<int> > VVi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int dy[8] = {1,1,0,-1,-1,-1,0,1}; // n ne e se s sw w nw
int dx[8] = {0,1,1,1,0,-1,-1,-1};

int m[51][51];
int cnt;

void bfs(int sx, int sy, int w, int h)
{
	rep(i,0,8) {
		int nx = sx + dx[i];
		int ny = sy + dy[i];
		if (ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
		if (m[ny][nx]) {
			m[ny][nx] = 0;
			bfs(nx, ny, w, h);
		}
	}
}

int main()
{
	while(1) {
		int w, h;
		cin >> w >> h;
		if (!w && !h) break;
		rep(i,0,h) rep(j,0,w) cin >> m[i][j];
		cnt = 0;
		rep(i,0,h) rep(j,0,w) if (m[i][j])	{
			cnt++;
			m[i][j] = 0;
			bfs(j,i,w,h);
		};
		cout << cnt << endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
using namespace std;
typedef long long ll;
int INF = (1LL << 30) - 1;
int MOD = 1e9+7;
int H,W;
void dfs(int h,int w,vector<vector<int> > &table){
    //cout << h << " " << w << endl;
    table[h][w] = 0;
    int dx[] = {-1,-1,-1,0 ,0,1 ,1,1};
    int dy[] = {-1,0 ,1 ,-1,1,-1,0,1};
    rep(i,0,8){
        int nx = w + dx[i];
        int ny = h + dy[i];
        if (nx >= W || nx < 0 || ny >= H || ny < 0) continue;
        if(table[ny][nx])dfs(ny,nx,table);
    }
}
main(){
    cin >> W >> H;
    vector< vector<int> > table;
    while(W){
        table = vector< vector<int> > (H,vector<int>(W) );
        rep(i,0,H)rep(j,0,W)cin >> table[i][j];
        int cnt = 0;
        rep(i,0,H)rep(j,0,W){
            if(table[i][j] == 1){
                //cout << i << " " << j << endl;
                dfs(i,j,table);
                cnt++;
            }
        }
        cout << cnt << endl;
        cin >> W >> H;
    }
}

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

int W, H;
vector<vector<int>> field;

void dfs(int h, int w) {
    field[h][w] = 0; // 探索済みにする

    // 場外、探索済みは飛ばす
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            int nh = h + dx, nw = w + dy;
            if (nh < 0 || H <= nh || nw < 0 || W <= nw)
                continue;
            if (field[nh][nw] == 0)
                continue;

            // 再帰的に探索
            dfs(nh, nw);
        }
    }
}

int main() {

    while (true) {
        // 入力
        cin >> W >> H;
        if (W == 0 && H == 0)
            break;

        field.assign(H, vector<int>(W, 0)); // 初期化

        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                cin >> field[h][w];
            }
        }

        // 探索と島のカウント
        int count = 0;
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                if (field[h][w] == 0) {
                    continue; // 海か探索済みなら飛ばす
                }

                dfs(h, w);
                count++;
            }
        }

        // 出力
        cout << count << endl;
    }
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<cstring>
using namespace std;

int w,h,c[50][50];
void island(int i,int j){
	int x,y;
	c[i][j]=0;
	for(x=-1;x<=1;x++){
		for(y=-1;y<=1;y++){
			if(0<=j+x&&j+x<w&&0<=i+y&&i+y<h&&c[i+y][j+x]==1){
				island(i+y,j+x);
			}
		}
	}

}
int main(){
	while(1){
		int sum=0,i,j,i1,j1;
		for(i=0;i<50;i++){
			for(j=0;j<50;j++){
				c[i][j]=0;
			}
		}
		cin>>w>>h;
		if(w==0&&h==0)break;
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				cin>>c[i][j];
			}
		}
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				if(c[i][j]==1){
					island(i,j);
					sum++;
				}
			}
		}
		cout<<sum<<endl;
	}
    return 0;
}