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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
using P = pair<int,int>;

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_H = 55, MAX_W = 55;
const int INF = 1000100100;

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


void init(){
    rep(i,MAX_H){
        rep(j,MAX_W){
            color[i][j] = INF;
            c[i][j] = 0;
        }
    }
}

void dfs(int hi, int wi, int col){
    if(color[hi][wi] == INF) color[hi][wi] = col;

    for(int i=-1; i<=1; i++){   // 高さ
        for(int j=-1; j<=1; j++){   // 幅
            int nh = hi+i, nw = wi+j;
            if(nh<0 || nw<0 || nh>=h || nw>=w) continue;
            if(c[nh][nw] == 1 && color[nh][nw] == INF) dfs(nh, nw, col);
        }
    }
}

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

        init();

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

        int col = 0;
        rep(i,h){
            rep(j,w){
                if(color[i][j] == INF && c[i][j] == 1){
                    dfs(i, j, col);
                    ++col;
                }
            }
        }
        set<int> s;
        rep(i,h){
            rep(j,w){
                if(color[i][j] != INF) s.insert(color[i][j]);
            }
        }
        cout << s.size() << endl;
    }
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())
#define ll long long
#define pb(a) push_back(a)
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

typedef vector<unsigned int>vec;
typedef vector<vec> mat;

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; }
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

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

struct Edge{int to,weight; Edge(int t, int w) : to(t), weight(w) {}};
using DirectedGraph = vector<vector<Edge>>;
using UndirectedGraph = vector<vector<int>>;

int H,W;
char field[51][51];
bool seen[51][51];

void dfs(int h,int w){
    if(seen[h][w]) return;

    seen[h][w] = true;

    REP(i,8){
        int nh=h+dy[i];
        int nw=w+dx[i];

        if(nh<0 || nh>=H || nw<0 || nw>=W || field[nh][nw]=='0') continue;
        dfs(nh,nw);
    }
    return;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
        cin>>W>>H;
        if(!H && !W) return 0;
        REP(i,H) REP(j,W) cin>>field[i][j];
        
        memset(seen,0,sizeof(seen));

        int ans=0;
        REP(i,H) REP(j,W){
            if(field[i][j]=='1' && !seen[i][j]){
                //cout<<i<<":"<<j<<endl;
                dfs(i,j);
                ans++;
            }
        }

        cout<<ans<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
static const int N = 50;

int W, H;
bool visited[N][N];
int map[N][N];

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

void dfs(int h, int w) {
    visited[h][w] = true;

    for (int i = 0; i < 8; ++i) {
        int nh = h + dx[i];
        int nw = w + dy[i];

        if (nh < 0 || nh > H-1 || nw < 0 || nw >W-1) continue;
        if (visited[nh][nw]) continue;
        if (map[nh][nw] == 0) continue;

        dfs(nh, nw);
    }
}

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

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

        memset(visited, 0, sizeof(visited));

        int count = 0;
        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                if (visited[h][w] || map[h][w] == 0) continue;
                dfs(h, w);
                ++count;
            }
        }

        cout << count << endl;
    }


    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i ,n) for(int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int>vint;
typedef vector<ll>vll;
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int h,w;
vector<vector<int>> c(51,vector<int>(51));
int di[] = {-1,0,1,0,-1,-1,1,1};
int dj[] = {0,-1,0,1,-1,1,-1,1};

bool dfs(int i,int j){
    if(0 == c[i][j])return false;
    c[i][j] = 0;
    for(int s=0; s<8; s++){
        int ni = i+di[s];
        int nj = j+dj[s];
        if(ni < 0 || nj < 0 || ni >= h || nj >= w)continue;
        if(0 == c[ni][nj])continue;
        dfs(ni,nj);
    }
    return true;
}

int main()
{
    bool flag = true;
    vint ans;
    while(flag){
        cin >> w >> h;
        if(0 == w && 0 == h){
            flag = false;
            break;
        }
        REP(i,h){
            REP(j,w){
                cin >> c[i][j];
            }
        }
        int cnt=0;
        REP(i,h){
            REP(j,w){
                if(dfs(i,j)){
                    cnt++;
                }
            }
        }
        ans.pb(cnt);
    }
    for(auto a:ans){
        cout << a << endl;
    }
}