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++: //#define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

#define int long long

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

signed main()
{
    int w, h;
    while(cin>>w>>h, w) {
        int field[h+1][w+1];
        for(int i=0; i<h; ++i) {
            for(int j=0; j<w; ++j) {
                int c;
                cin >> c;
                field[i][j] = c;
            }
        }

        int ans = 0;
        for(int i=0; i<h; ++i) {
            for(int j=0; j<w; ++j) {
                if(field[i][j]==1) {
                    ans++;
                    queue<pair<int, int> > q;
                    q.push(make_pair(i, j));
                    while(!q.empty()) {
                        pair<int, int> p = q.front();
                        q.pop();
                        int x = p.first;
                        int y = p.second;
                        field[x][y] = 0;
                        for(int k=0; k<8; ++k) {
                            int nx = x + dx[k];
                            int ny = y + dy[k];
                            if(nx>=0&&nx<h&&ny>=0&&ny<w&&field[nx][ny]==1) {
                                q.push(make_pair(nx, ny));
                            }
                        }
                    }
                }
            }
        }

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

C++: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,b) for(ll i=0;i<b;i++)

using namespace std;

int field[50][50];
int N,M;
void dfs(int x,int y){
    field[x][y]=0;
    for(int dx=-1;dx<=1;dx++){
        for(int dy=-1;dy<=1;dy++){
            int nx=x+dx,ny=y+dy;
            if(0<=nx&&nx<N&&0<=ny&&ny<M&&field[nx][ny]==1)dfs(nx,ny);
        }
    }
}
int main(){
    for(;;) {
        cin>>M>>N;
        if(N==0)break;
        rep(i,N){
            rep(j,M){
                cin>>field[i][j];
            }
        }
        int res = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (field[i][j] == 1) {
                    dfs(i, j);
                    res++;
                }
            }
        }
        cout << res << endl;
    }
}

C++: #include<iostream>
using namespace std;
int Island[52][52];
void solve(int y,int x) {
	Island[y][x] = 0;
	for (int i = -1; i <= 1; ++i) {
		for (int j = -1; j <= 1; ++j) {
			if (Island[y + i][x + j]) solve(y + i, x + j);
		}
	}
}
int main() {
	int w,h;
	while (cin >> w >> h&&w&&h) {
		int cnt = 0;
		for (int i = 0; i < h + 2; ++i) {
			for (int j = 0; j < w + 2; ++j) {
				if (i == 0 || j == 0 || i == h + 1 || j == w + 1) Island[i][j] = 0;
				else cin >> Island[i][j];
			}
		}
		for (int i = 1; i < h + 1; ++i) {
			for (int j = 1; j < w + 1; ++j) {
				if (Island[i][j]) {
					solve(i, j);
					++cnt;
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;

struct UnionFind {
        int n;
        vector<int> parent;
        vector<int> rank;
        vector<int> num;
        int find(int x) {
                if (parent[x] == x) return  x;
                return parent[x] = find(parent[x]);
        }
        UnionFind(int n_) {
                n = n_;
                parent.resize(n);
                for (int i = 0; i < n; i ++) parent[i] = i;
                rank.assign(n, 0);
                num.assign(n, 1);
        }
        void unite(int x, int y) {
                if ((x = find(x)) != (y = find(y))) {
                        if (rank[x] < rank[y]) {
                                parent[x] = y;
                                num[y] += num[x];
                        } else {
                                parent[y] = x;
                                if (rank[x] == rank[y]) rank[x] ++;
                                num[x] += num[y];
                        }
                        n --;
                }
        }
        bool same(int x, int y) { return find(x) == find(y); }
        int get() { return n; }
        int get(int x) { return num[find(x)]; }
};

int main() {
        int w, h;
        while (scanf("%d%d", &w, &h), w) {
                vector<vector<int>> s(h, vector<int> (w));
                int lands = 0;
                map<pair<int, int>, int> ptol;
                for (int i = 0; i < h; i ++) {
                        for (int j = 0; j < w; j ++){
                                scanf("%d", &s[i][j]);
                                if (s[i][j]) {
                                        ptol[make_pair(i, j)] = lands;
                                        lands ++;
                                }
                        }
                }
                UnionFind uf(lands);
                auto get = [&](int a, int b) { return ptol[make_pair(a, b)]; };
                for (int i = 0; i < h; i ++) {
                        for (int j = 0; j < w; j ++) {
                                if (s[i][j]) {
                                        if (i - 1 >= 0 && j + 1 < w && s[i - 1][j + 1]) uf.unite(get(i, j), get(i - 1, j + 1));
                                        if (j + 1 < w && s[i][j + 1])                   uf.unite(get(i, j), get(i, j + 1));
                                        if (i + 1 < h && j + 1 < w && s[i + 1][j + 1])  uf.unite(get(i, j), get(i + 1, j + 1));
                                        if (i + 1 < h && s[i + 1][j])                   uf.unite(get(i, j), get(i + 1, j));
                                }
                        }
                }
                printf("%d\n", uf.get());
        }
        return 0;
}

C++: #include <iostream>
#define UMI 0
#define RIKU 1
using namespace std;

int w,h;
int c[50][50];

void pr(){
  for( int i=0;i<h;i++ ){
    for( int j=0;j<w;j++ )
      cout << c[i][j] << " ";
    cout << endl;
  }
}

void check( int x,int y){
  if( x<0 || x>=h ) return;
  if( y<0 || y>=w ) return;
  if( c[x][y]==UMI ) return;

  c[x][y] = UMI;
  check( x-1, y-1);
  check( x-1, y  );
  check( x-1, y+1);
  check( x  , y-1);
  check( x  , y+1);
  check( x+1, y-1);
  check( x+1, y  );
  check( x+1, y+1);
}

int main(){
  while( cin>>w>>h,w&&h ){
    for( int i=0;i<h;i++ )
      for( int j=0;j<w;j++ )
	cin >> c[i][j];
    //cout << w <<" "<< h<<endl;
    //    pr();
    int sum = 0;
    for( int i=0;i<h;i++ )
      for( int j=0;j<w;j++ )
	if( c[i][j]==RIKU ){
	  ++sum;
	  check(i,j);
	}
    cout << sum << endl;
  }

  return 0;
}