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>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=55,INF=1<<30;
int H,W;
int cnt;
int state[MAX][MAX];
vector<int> dx={1,0,-1,0,1,1,-1,-1},dy={0,-1,0,1,1,-1,1,-1};

void DFS(int us,int ut){
    for(int i=0;i<8;i++){
        if(0<=us+dx[i]&&us+dx[i]<H&&0<=ut+dy[i]&&ut+dy[i]<W){
            if(state[us+dx[i]][ut+dy[i]]==0){
                state[us+dx[i]][ut+dy[i]]=1;
                DFS(us+dx[i],ut+dy[i]);
            }
        }
    }
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        cin>>W>>H;
        if(H==0&&W==0) break;
        
        cnt=0;
        memset(state,0,sizeof(state));
        
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                int x;cin>>x;
                if(x==0) state[i][j]=1;
            }
        }
        
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                if(state[i][j]==0){
                    state[i][j]=1;
                    cnt++;
                    DFS(i,j);
                }
            }
        }
        
        cout<<cnt<<endl;
    }
}

C++: #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <set>
#include <cstdio>
#include <vector>
#include <iostream>
#include <utility>
#include <queue>
#include <map>

#define fir first
#define sec second
#define sz(s) (s).size()
#define pb push_back
#define get(n) scanf("%d",&n);
#define gets(s) string s;cin >> (s);
#define prfi(n) printf("%d", &n);
#define prfd(n) printf("%lf", &n);
#define All(s) (s).begin(), (s).end()
#define rep(i,j) for(int (i)=0;(i)<(j);(i)++)
#define For(i,j,k) for(int (i)=(j);(i)<(k);(i)++)
#define drep(i,j) for(int (i)=(j);(i)>=0;(i)--)
#define Ford(i,j,k) for(int (i)=(j);i>=(k);i--)
#define vfor(c,v) for(auto (c): (v))
#define mem(a,b) memset(a,b,sizeof(a));
#define dump(x)  std::cout << #x << " = " << (x) << std::endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
using ll = long long;
using ld = long double;
using pii = std::pair<int,int>;
using pll = std::pair<ll, ll>;
using vi = std::vector<int> ;
using vvi = std::vector<vi> ;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vd = std::vector<double> ;
using vvd = std::vector<vd> ;
using qi = std::queue<int> ;
using vpii = std::vector<std::pair<int, int> >;
using vpll = std::vector<pll>;
using namespace std;

const int Mod = (1e9) + 7;
const int INF = 1e9 + 19;
const ll INFL = 1e18 + 19;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const int dx2[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy2[] = {1, 0, -1, 1, -1, 1, 0, -1};
//_____________________________________Templates_________________________________________//

template<class T1, class T2> inline void chmin(T1 &a, T2 b){if(a > b) a = b;}
template<class T1, class T2> inline void chmax(T1 &a, T2 b){if(a < b) a = b;}
template<class T> inline void swap(T &a,T &b){T c = a;a = b;b = c;}
template<class T> inline void pri(T a){cout << a << endl;}
template<class Z> using vec = vector<Z>;
//mainly use for dynamic prog
template<class T1, class T2>
void update(T1 &a, T2 b){
  a += b;
  if(a > Mod) a %= Mod;
}

inline void IN(void){
  return;
}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
  cin >> first;
  IN(rest...);
  return;
}

inline void OUT(void){
  cout << "\n";
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
  cout << first << " ";
  OUT(rest...);
  return;
}

bool pairsort(pll pl, pll pr){
  if(pl.first == pr.first)return pl.second > pr.second;
  return pl.first < pr.first;
}

int cntbit(ll a,int n,int j){int res = 0;For(i,j,n){if(a>>i & 1){res++;}}return res;}
ll makebit(ll a,int n,int j){ll res = 0;For(i,j,n){if(a &(1 << i))res |= 1ll << (i - j);}return res;}
bool stdbit(int a, int b){return a&(1 << b); }
int GCD(int a, int b){if(b > a)return GCD(b,a);if(a%b == 0)return b;else return GCD(b, a%b);}
int LCM(int a, int b){return a*b/GCD(a,b);}
int roundup(int a, int b){if(a % b == 0)return a/b;else return (a+1)/b;}
int rounddown(int a, int b){if(a%b == 0)return a/b;else {return (a-1)/b;}}


//_____________________　following sorce code_________________________//
const int max_n = 3 * (1e5) + 1;
const int max_m = 83 * (1e5) + 1;

int n,m,k;
int h,w;
vvi dp;
string S;
int a,b,c;
vi v;
int cell[51][51];
int value;
int cellflag[51][51];
void rec(int x, int y)
{
  cellflag[x][y] = value;

  rep(j,8)
  {
    int nx = x + dx2[j] , ny = y + dy2[j];
    if(nx >= 0 && nx < h && ny >= 0 && ny < w && cell[nx][ny] == 1 && cellflag[nx][ny] == 0)rec(nx, ny);
  }
}

int main () {
  //cin.tie(0);
  //ios::sync_with_stdio(false);
  while(scanf("%d%d",&w,&h) && w)
  { 
    mem(cellflag,0);
    rep(i,h)rep(j,w)IN(cell[i][j]);
    value = 0;
    rep(i,h)rep(j,w){
      if(cellflag[i][j] == 0 && cell[i][j] == 1)
      {
        value++;
        rec(i,j);
      }
    }
    cout << value << endl;
    /*
    for(auto c : cellflag){
      for(auto d : c){
        cout << d << " ";
      }
      cout << endl;
    }
    */
  }
  //cout << ans << endl;
  //for(auto c : ans){cout << c << endl;}
  //cout << fixed << setprecision(15) << ans << endl;
  return 0;
}

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

struct UnionFind {
    vector<int> data;
    int sz;
    UnionFind(int sz) : data(sz, -1), sz(sz){ }
    bool unionSet(int x, int y) {
        if ((x = root(x)) == (y = root(y))) return false;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y]; data[y] = x; sz--;
        return true;
    }
    bool findSet(int x, int y) { return root(x) == root(y); }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    int size() { return sz; }
};

int main() {
  int w, h;
  while (cin >> w >> h) {
    if (w == 0 && h == 0) {
      break;
    }
    int count = 0;
    vector<vector<int>> c(h, vector<int>(w));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> c[i][j];
        if (c[i][j] == 0) {
          count++;
        }
      }
    }
    auto idx = [&](int i, int j) {
      return i*w + j;
    };
    auto outfield = [&](int i, int j) {
      return i < 0 || i >= h || j < 0 || j >= w;
    };
    UnionFind uf(w*h);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        for (int x = -1; x <= 1; x++) {
          for (int y = -1; y <= 1; y++) {
            if (outfield(i + x, j + y)) {
              continue;
            }
            if (c[i][j] == 1 && c[i + x][j + y] == 1) {
              uf.unionSet(idx(i, j), idx(i + x, j + y));
            }
          }
        }
      }
    }
    cout << uf.size() - count << endl;
  }
}