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>
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;

int w, h;
int c[51][51];
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};

void dfs(int y, int x){
	c[y][x] = 0;
	for(int i = 0; i < 8; i++){
		int ny = y + dy[i], nx = x + dx[i];
		if(0<=ny and ny<h and 0<=nx and nx<w and c[ny][nx]==1){
			dfs(ny, nx);
		}
	}
}

int solve(){
	int res = 0;
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			if(c[i][j] == 1){
				dfs(i, j);
				res++;
			}
		}
	}

	return res;
}

int main(void){
	while(scanf("%d%d", &w, &h) != EOF){
		if(w == 0 and h == 0) break;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				scanf("%d", &(c[i][j]));
			}
		}
		printf("%d\n", solve());
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

const int MAX_H = 50;
const int MAX_W = 50;
int field[MAX_H][MAX_W];
int cnt;
int H, W;
// int dy[4] = {1, 0, -1, 0};
// int dx[4] = {1, 1, 0, -1};
bool seen[MAX_H][MAX_W];

void bfs(int y, int x)
{
    for (int dy = -1; dy <= 1; dy++)
    {
        for (int dx = -1; dx <= 1; dx++)
        {
            if (dy == 0 && dx == 0)
                continue;

            int ny = y + dy;
            int nx = x + dx;
            if (ny < 0 || ny >= H || nx < 0 || nx >= W)
                continue;
            if (field[ny][nx] == 0)
                continue;
            if (seen[ny][nx])
                continue;

            seen[ny][nx] = true;
            bfs(ny, nx);
        }
    }
}

int main()
{

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

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

        cnt = 0;
        for (int y = 0; y < H; y++)
        {
            for (int x = 0; x < W; x++)
            {
                seen[y][x] = false;
            }
        }

        for (int y = 0; y < H; y++)
        {
            for (int x = 0; x < W; x++)
            {
                if (seen[y][x] || field[y][x] == 0)
                    continue;
                cnt++;
                seen[y][x] = true;
                bfs(y, x);
            }
        }

        cout << cnt << endl;
    }

    return 0;
}

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

vector<vector<ll>> grid;
vector<vector<bool>> visited;
ll H, W;

void dfs(ll i, ll j) {
    if (visited[i][j]) return;
    visited[i][j] = true;
    rep(d, 8) {
        ll y = i + ddx[d];
        ll x = j + ddy[d];
        if (0 <= x && x < W && 0 <= y && y < H && grid[y][x]) {
            dfs(y, x);
        }
    }
    return;
};

void solve() {
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) return;
        grid.assign(H, vector<ll>(W, 0));
        rep(i, H) {
            rep(j, W) {
                cin >> grid[i][j];
            }
        }
        ll ans = 0;
        visited.assign(H, vector<bool>(W, false));
        rep(i, H) {
            rep(j, W) {
               if (grid[i][j] && !visited[i][j])  {
                   ans++;
                   dfs(i, j);
               }
            }
        }
        cout << ans << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <string>
#define FI first
#define SE second
#define PF push_front
#define PB push_back
#define PPF pop_front
#define PPB pop_back
#define MA make_pair
#define ll long long
#define PA pair<int,int>
#define VE vector<int>
#define VP vector<PA>
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ROF(i,a,b) for(int i=b-1;i>=a;i--)
#define YES(i) cout<<(i?"YES":"NO")<<endl
#define Yes(i) cout<<(i?"Yes":"No")<<endl
using namespace std;
//
const int INF=1e9+7;
const int mod=1e9+7;
//
struct poi{
	int X;int Y;int Z;
	bool operator<(const poi&R)const{
		return X==R.X ? Y==R.Y ? Z<R.Z : Y<R.Y : X<R.X;
	}
};
/*
ll GCD(ll A,ll B){
  return (B==0)?(A):(GCD(B,A%B));
}
ll LCM(ll A,ll B){
  return A/GCD(A,B)*B;
}
*/
int W,H;
int C[50][50];
void DFS(int x,int y){
	C[x][y]=0;
	FOR(i,-1,2){
		FOR(j,-1,2){
			if(0<=x+i&&x+i<H&&0<=y+j&&y+j<W&&C[x+i][y+j]==1){
				DFS(x+i,y+j);
			}
		}
	}
}
int main(){
	while(1){
		cin>>W>>H;
		if(W==0&&H==0){
			break;
		}
		//
		FOR(i,0,H){
			FOR(j,0,W){
				cin>>C[i][j];
			}
		}
		int ans=0;
		FOR(i,0,H){
			FOR(j,0,W){
				if(C[i][j]==1){
					DFS(i,j);
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstring>
using namespace std;
#define REP(i,n) for(int i=0; i<(int)(n); i++)

int w,h;
int island[50][50];
int visited[50][50];
const int dx[] = {0,-1,-1,-1,0,1,1,1};
const int dy[] = {-1,-1,0,1,1,1,0,-1};

void umeru(int x, int y){
	
	visited[y][x] = 1;
	//printf("%d,%d\n",x,y);
	REP(k,8){
		int tx = x + dx[k];
		int ty = y + dy[k];
		if( tx<0 || ty<0 || tx>=w || ty>=h )continue;
		if( island[ty][tx]==0 || visited[ty][tx]==1 )continue;
		umeru(tx,ty);
	}
	return;
}
int main(){
	while(cin>>w>>h,w|h){
		int ans = 0;
		memset(visited,0,sizeof(visited));
		REP(i,h)REP(j,w)cin>>island[i][j];
		REP(i,h)REP(j,w)if( island[i][j] && !visited[i][j] ){
			umeru(j,i);
			ans++;
		}
		cout << ans << endl;
	}
	return 0;
}