Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

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

#define int long long
#define double long double

#define REP(i, b) for(int i = 0; i < (b); i++)
#define REPS(i, b) for(int i = 1; i <= (b); i++)
#define ALL(v) (v).begin(), (v).end()

#define fi first
#define se second
#define pb push_back
#define mp make_pair

using namespace std;

using pi = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vs = vector<string>;
using vb = vector<bool>;
using vpi = vector<pi>;
using vvi = vector<vi>;
using vvb = vector<vb>;

const int INF = 1e16;
const int MOD = 1e9+7;

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; }

int w, h;
int d[] = {-1, 0, 1};

void dfs(vvi &c, int y, int x)
{
    c[y][x] = 0;
    REP(i, 3)
    {
        REP(j, 3)
        {
            int ny = y + d[i];
            int nx = x + d[j];
            if(0 <= ny && ny < h && 0 <= nx && nx < w && c[ny][nx] == 1) dfs(c, ny, nx);
        }
    }
}

signed main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    while(true)
    {
        int ans = 0;
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        vvi c(h, vi(w));
        REP(i, h) REP(j, w) cin >> c[i][j];
        REP(i, h)
        {
            REP(j, w)
            {
                if(c[i][j] == 1)
                {
                    ans++;
                    dfs(c, i, j);
                }
            }
        }
        cout << ans << endl;
    }
}

C++: /************************************
 * ???????????¢?´¢??§??¶?????°?????°??????????????°?????? *
 ************************************/

#include <iostream>
#include <queue>
#include <map>

#define LAND '1'  //??? char???
#define SEA  '0'   //??? char???

using namespace std;

/* ?????????
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
*/

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

bool input(vector< vector<char> > &m);
void delete_island(vector< vector< char > > &m, int x, int y);
void output(vector< vector< char> > m);


int main(){
  int w, h;
    
  while(cin >> w >> h, w && h){
    vector< vector< char > > m(h+2, vector<char>(w+2));
    input(m);
    
    int island = 0;
    for(int y = 0; y < m.size(); y++){
      for(int x = 0; x < m[y].size(); x++){
	if(m[y][x] == LAND){
	  delete_island(m,x,y);
	  island++;
	}
      }
    }
    cout << island << endl;
  }
}


bool input(vector< vector<char> > &m){
  for(int y = 0; y < m.size(); y++){
    for(int x = 0; x < m[y].size(); x++){
      if(y == 0 || x == 0 ||
	 y == m.size()-1 || x == m[y].size()-1){
	m[y][x] = SEA;
      }else{
	if(!(cin >> m[y][x])) return false;
      }
    }
  }
  return true;
}


 void delete_island(vector< vector< char > > &m, int x, int y){
   queue< pair< int, int > > q;
   q.push(make_pair(x,y));

   while(!(q.empty())){
     pair<int, int> cur = q.front();
     q.pop();
     if(m[cur.second][cur.first] == LAND){
       m[cur.second][cur.first] = SEA;
       for(int i = 0; i < sizeof(dx)/sizeof(int); i++){
	 q.push(make_pair(cur.first+dx[i],cur.second+dy[i]));
       }
     }
   }
}

void output(vector< vector< char> > m){
  for(auto y : m){
    for(auto x: y){
      cout << x << " ";
    }
    cout << endl;
  }
  cout << endl;
}

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

int main() {
  int w, h;
  while(cin >> w >> h, w || h) {
    set<pair<int, int> > blacks;
    for(int y = 0; y < h; ++y) for(int x = 0; x < w; ++x) {
      int d;
      cin >> d;
      if(d) blacks.insert(make_pair(y, x));
    }
    function<void(int, int)> countIslands = [&](int y, int x) {
      if(blacks.count(make_pair(y, x))) {
        blacks.erase(make_pair(y, x));
        int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
        int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
        for(int i = 0; i < 8; ++i) countIslands(y + dy[i], x + dx[i]);
      }
    };
    int ans = 0;
    while(!blacks.empty()) {
      countIslands((*begin(blacks)).first, (*begin(blacks)).second);
      ++ans;
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int box[52][52];
int main(){
  int w,h,i,I,a;
  while(1){
    a=0;
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<=h+1;i++){
      for(I=0;I<=w+1;I++){
	box[i][I]==0;
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	cin>>box[i][I];
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	if(box[i][I]==1){
	  dfs(i,I);
	  a++;
	}
      }
    }
    cout<<a<<endl;
  }
  return 0;
}
  void dfs(int i,int I){
  if(box[i][I]==0) return;
  box[i][I]=0;
  dfs(i,I+1);
  dfs(i+1,I+1);
  dfs(i+1,I);
  dfs(i+1,I-1);
  dfs(i,I-1);
  dfs(i-1,I-1);
  dfs(i-1,I);
  dfs(i-1,I+1);
  }

C++: #include <iostream>
#include <cmath>
#include <algorithm>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i,k,n) for(int i = k; i < (int)(n); i++)
using namespace std;

int w,h;
int field[51][51];

void dfs(int y, int x){
        field[y][x]= 0;

        int dx[] = {-1,-1,-1,0,0,1,1,1};
        int dy[] = {-1,0,1,-1,1,-1,0,1,};
        rep(i,8){
                int nx = x+dx[i];
                int ny = y+dy[i];
                if(nx>=0 && nx<w && ny>=0 && ny<h && field[ny][nx] == 1) dfs(ny,nx);
        }
        return;
}

int main(){
        while(true) {
                cin >> w >> h;
                if(w==0) break;
                rep(y,h) rep(x,w){
                        cin >> field[y][x];
                }
                int ans = 0;
                rep(y,h) rep(x,w){
                        if(field[y][x] == 1) {
                                dfs(y,x);
                                ans++;
                        }
                }
                cout << ans << endl;
        }

        return 0;
}