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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
typedef long long ll;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

#define EPS (1e-7)
#define INF (1e15)
#define PI (acos(-1))
#define MOD 1000000007

#define REP(i,n) for(int i=0;i<n;i++)
#define REPS(i,f,n) for(int i=(f);i<(n);i++)
#define EACH(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)

#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define arrdump(n,a) for(int i=0;i<n;i++) {cout<<((i>0)? " ":"")<<a[i];}cout<<"\n";


typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int dy4[]={0, 0, 1, -1};
int dx4[]={1, -1, 0, 0};
int dx8[]={0, 0, 1, -1, 1, 1, -1, -1};
int dy8[]={1, -1, 0, 0, 1, -1, -1, 1};

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

int C[99][99];
int w, h;

static void dfs(int i , int j) {
    C[i][j] = 0;

    REP(k, 8) {
        int nx = j + dx8[k];
        int ny = i + dy8[k];
        if (nx >= 0 && nx < w && ny >= 0 && ny < h) {
            if (C[ny][nx] == 1) {
                dfs(ny, nx);
            }
        }
    }          
}

int main() {

    while (true) {
        cin >> w >> h;
        if (w==0&&h==0) break;

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> C[i][j];
            }
        }

        int cnt = 0;
        while(true) {
            bool found = false;
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    if (C[i][j] == 1) {
                        dfs(i, j);
                        cnt++;
                    }
                }
            }

            if (!found) break;
        }

        cout << cnt << "\n";
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int H,W;
bool fld[50][50];
void del(int x,int y){
	fld[x][y]=0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			int nx=x+i,ny=y+j;
			if(nx>=0 && nx<W && ny>=0 && ny<H && fld[nx][ny])del(nx,ny);
		}
	}
}
int main(){
	while(cin>>W>>H,W,H){
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>fld[j][i];
			}
		}
		int cnt=0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(fld[j][i]){
					cnt++;
					fld[j][i]=0;
					del(j,i);
				}
			}
		}
		cout<<cnt<<endl;
	}
}

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

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		if (w == 0&&h == 0) {
			break;
		}
		ans.push_back(res);


	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++)
#define PRINT(x) cout<<(x)<<endl
#define INF 1000000000000 // 10^12
#define MOD 1000000007 // 10^9+7
#define PB push_back
#define MP make_pair

const int MAX_W = 50, MAX_H = 50;

int w, h, c[MAX_H][MAX_W];

bool visited[MAX_H][MAX_W];

void dfs(int, int);

int main() {
    while (true) {
        // input
        cin >> w >> h;
        
        if (w == 0 && h == 0) break;

        REP(i, h) {
            REP(j, w) cin >> c[i][j];
        }

        // init visited
        REP(i, h) {
            REP(j, w) visited[i][j] = false;
        }

        // count island by DFS 
        int cnt_island = 0;
        REP(i, h) {
            REP(j, w) {
                if (visited[i][j] == false && c[i][j] == 1) {
                    dfs(i, j);
                    cnt_island++;
                }
            }
        }

        // output
        PRINT(cnt_island);
    }
    return 0;
}

void dfs(int x, int y) {
    visited[x][y] = true;
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            int nx = x + dx;
            int ny = y + dy;
            if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
            if (visited[nx][ny] || c[nx][ny] == 0) continue;
            dfs(nx, ny);
        }
    }
}

C++: 
#include <iostream>
#include <vector>

using namespace std;

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

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

int dp[55][55];
int c[55][55];

int check(int x, int y)
{
	if (dp[x][y]) return 0;
	if (c[x][y] == 0) return 0;
	dp[x][y] = 1;
	
	rep(i, 8) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		int take = check(xx, yy);
	}
	return 1;
}

int main(void)
{
	while(1) {
		int w, h;
		cin >> w >> h;
		if (w == 0) break;
		rep(i, 52) rep(j, 52) c[i + 1][j + 1] = 0;
		rep(i, h) rep (j, w) cin >> c[i + 1][j + 1];
		rep(i, 52) rep(j, 52) dp[i][j] = 0;
		rep(i, 55) {
			dp[0][i] = 1;
			dp[i][0] = 1;
			dp[h + 1][i] = 1;
			dp[i][w + 1] = 1;
		}

		int ans = 0;
		rep(i, h) rep(j, w) if (c[i + 1][j + 1]) ans += check(i + 1, j + 1);
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
#define MAX 50

int map[MAX][MAX];
int ans, w, h;
int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
int dy[] = { 0, 1, 0, -1, -1, 1, 1, -1 };

void dfs(int x, int y) {
	if (x < 0 || x >= w || y < 0 || y >= h) return;
	if (map[y][x]==0) return;
	map[y][x] = 0;
	for (int i=0; i<8; i++)
		dfs(x+dx[i], y+dy[i]);
}

int main() {
	while (cin >> w >> h, w||h) {
		for (int i=0; i<h; i++)
		for (int j=0; j<w; j++)
			cin >> map[i][j];

		ans=0;
		for (int i=0; i<h; i++)
		for (int j=0; j<w; j++)
			if (map[i][j]) { ans++; dfs(j, i); }

		cout << ans << endl;
	}
	return 0;
}