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 <vector>
#include <map>
#include <set>
using namespace std;

void add_label(map<int,int> &tbl, int a, int b){
	if( a == b ) return;
	if( a > b ) tbl[a] = b;
	else tbl[b] = a;
}

int main(void){
	int w, h;
	while( cin>>w>>h, w||h ) {
		vector<vector<int> > field( h );
		vector<vector<int> > labeled( h );
		for( int i=0; i<h; ++i ) {
			field[i] = vector<int>( w );
			labeled[i] = vector<int>( w );
			for( int j=0; j<w; ++j ) {
				cin>>field[i][j];
				labeled[i][j] = 0;
			}
		}
		int label = 1;
		map<int, int> tbl;
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				if( field[i][j] ){
					bool f = false;
					if( i ) {
						if( j && labeled[i-1][j-1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j-1];
							f = true;
						}
						if( labeled[i-1][j] ) {
							if(!f) labeled[i][j] = labeled[i-1][j];
							else add_label( tbl, labeled[i][j], labeled[i-1][j] );
							f = true;
						}
						if( j < w - 1 && labeled[i-1][j+1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j+1];
							else add_label( tbl, labeled[i][j], labeled[i-1][j+1] );
							f = true;
						}
						if( j && labeled[i][j-1] ) {
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j], labeled[i][j-1] );
							f = true;
						}
						if(!f) labeled[i][j] = ++label;
					} else {
						if( j && labeled[i][j-1] ){
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j], labeled[i][j-1] );
							f = true;
						}
						if(!f) labeled[i][j] = ++label;
					}
				}
			}
		}
		for( auto x : tbl ) {
			int a = x.first;
			int b = x.second;
			while( tbl.find(b) != tbl.end() ) {
				b = tbl.find(b)->second;
			}
			for( int i=0; i<h; ++i ) {
				for( int j=0; j<w; ++j ) {
					if( labeled[i][j] == a ) labeled[i][j] = b;
				}
			}
		}
		
		/*
		for( int i=0; i<h; ++i ) {
			cerr<<"!";
			for( int j=0; j<w; ++j ) {
				cerr<<labeled[i][j];
			}
			cerr<<endl;
		}*/
		
		set<int> nums;
		nums.insert(0);
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				nums.insert( labeled[i][j] );
			}
		}
		cout<<nums.size()-1<<endl;
	}
	return 0;
}

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

#define ul unsigned long long
#define ll long long

ll td[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};

void dfs(int p[50][50],ll w,ll h,ll cx,ll cy){
  if (cx < 0 || cx >= w || cy < 0 || cy >= h)
    return;
  //cout << cx << "," << cy <<":"<<(int)p[cy][cx]<< endl;
  if (p[cy][cx] == 0)
    return;
  p[cy][cx]=0;
  ll nx,ny;
  for (ll i = 0; i < 8; i++)
  {
    nx = cx + td[i][0];
    ny = cy + td[i][1];
    dfs(p,w,h,nx,ny);
  }
}

int main()
{
  ll w,h;
  int p[50][50];
  ul ct = 0;

  cin >> w >> h;
  while (w | h)
  {
    ct=0;
    for (ll i = 0; i < h; i++)
    {
      for (ll j = 0; j < w; j++)
      {
        cin >> p[i][j];
      }
    }

    for (ll i = 0; i < h; i++)
    {
      for (ll j = 0; j < w; j++)
      {
        if (p[i][j])
          ct++;
        dfs(p, w, h, j, i);
      }
    }
    cout << ct << endl;
    cin >> w >> h;
  }

  return 0;
}

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

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

int w,h,ans;
queue<pair<int,int>> que;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> w >> h,w){
        vector<vector<int>> m(h,vector<int>(w));
        for (int i=0;i<h;++i)
            for (int j=0;j<w;++j)
                cin >> m[i][j];
        vector<vector<int>> visited(h,vector<int>(w,0));
        ans=0;
        for (int i=0;i<h;++i){
            for (int j=0;j<w;++j){
                if (visited[i][j]||!m[i][j]) continue;
                ++ans;
                visited[i][j]=1; que.emplace(i,j);
                while(!que.empty()){
                    pair<int,int> p=que.front(); que.pop();
                    for (int k=0;k<3;++k){
                        for (int l=0;l<3;++l){
                            int nh=p.first+dx[k],nw=p.second+dx[l];
                            if (nh<0||h<=nh||nw<0||w<=nw) continue;
                            if (visited[nh][nw]||!m[nh][nw]) continue;
                            visited[nh][nw]=1; que.emplace(nh,nw);
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int w, h;
int m[50][50];
int visited[50][50] = {0};

void dfs(int x, int y) {
  visited[x][y] = 1;
  for(int i = -1; i <= 1; i++) {
    for(int j = -1; j <= 1; j++) {
      int nx = x + i;
      int ny = y + j;
      if(0 <= nx && nx < h && 0 <= ny && ny < w && visited[nx][ny] == 0 && m[nx][ny] == 1) {
        dfs(nx, ny);
      }
    }
  }
  
}

int main() {
  while(true) {
    rep(i, h) {
      rep(j, w) {
        visited[i][j] = 0;
      }
    }
    cin >> w >> h;
    if(w == 0 && h == 0) break;
    rep(i, h) {
      rep(j, w) {
        cin >> m[i][j];
      }
    }
    int count = 0;
    for(int i = 0; i < h; i++) {
      for(int j = 0; j < w; j++) {
        if(m[i][j] == 1 && visited[i][j] == 0) {
          count++;
          dfs(i, j);
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

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

int H, W;
bool grid[55][55];

void island(int h, int w) {
  grid[h][w] = 0;

  for(int i = -1; i <= 1; ++i) {
    for(int j = -1; j <= 1; ++j) {
      if(i == 0 && j == 0) continue;
      int nh = h + i;
      int nw = w + j;
      if(0 <= nh && nh < H && 0 <= nw && nw < W && grid[nh][nw]) island(nh, nw);
    }
  }
}

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 >> grid[i][j];

    int ans = 0;
    for(int i=0; i < H; ++i) {
      for(int j=0; j < W; ++j) {
        if(grid[i][j]) {
          island(i,j);
          ++ans;
        }
      }
    }
    cout << ans << '\n';
  }

  return 0;
}