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 <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <complex>
#include <fstream>
#include <stdio.h>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VEC;
typedef vector<VEC> MAT;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define MT(a,b,c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second


//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define EPS 1e-8

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

int m[55][55];
int main(){
	while(1){
		int h,w,ret=0;
		cin>>w>>h;
		if(w==0)return 0;
		memset(m, 0, sizeof(m));
		REP(i,w+2){
			m[0][i]=0;
			m[h+1][i]=0;
		}
		REP(i,h+2){
			m[i][0]=0;
			m[i][w+1]=0;
		}
		REP(i,h)REP(j,w)cin>>m[i+1][j+1];
		priority_queue<PII> q;
		FOR(i,1,h+1){
			FOR(j,1,w+1){
				if(m[i][j]==1){
					ret++;
					q.push(MP(i,j));
					while(!q.empty()){
						PII tmp = q.top();
						q.pop();
						if(m[tmp.first][tmp.second]==0)continue;
						m[tmp.first][tmp.second] = 0;
						REP(i,8)q.push(MP(tmp.first+dy[i], tmp.second+dx[i]));
					}
				}
			}
		}
		cout << ret << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include <unordered_map>  
#include <algorithm>
#include <functional>
#include <map>
#include <string>
#include <sstream>
#include <cmath>
#include <float.h>
using namespace std;

bool checked[50][50];
int island[50][50];
int count1 = 0;
int w, h;

void checkIsland(int x, int y) {
	if (x < 0 || x >= w || y >= h || y < 0) { return; }
	if (!island[x][y]) return;
	checked[x][y] = true;
	checkIsland(x + 1, y);
	checkIsland(x + 1, y + 1);
	checkIsland(x + 1, y - 1);
	checkIsland(x - 1, y + 1);
	checkIsland(x, y + 1);
}

int main() {
	cin >> w >> h;
	for (int i = 0; i < w; ++i) {
		for (int j = 0; j < h; ++j) {
			checked[i][j] = false;
			cin >> island[i][j];
		}
	}
	for (int i = 0; i < w; ++i) {
		for (int j = 0; j < h; ++j) {
			if (checked[i][j]) continue;
			if (island[i][j]) {
				count1++;
				checkIsland(i, j);
			}
			else {
				checked[i][j] = true;
			}
		}
	}
	cout << count1 << endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int field[64][64];
int cnt;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int w, h;

void dfs(int x, int y);

int main()
{
	int x, y;

	while (true){
		cnt = 0;

		scanf("%d %d", &w, &h);
		if (w == 0){
			break;
		}

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				scanf("%d", &field[i][j]);
			}
		}

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (field[i][j]){
					cnt++;
					dfs(j, i);
				}
			}
		}

		printf("%d\n", cnt);
	}

	return (0);
}

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

	for (int i = 0; i < 8; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];

		if (field[ny][nx] && nx >= 0 && ny >= 0 && nx < w && ny < h){
			dfs(nx, ny);
		}
	}
}

C++: #include <iostream>
#define R 1
#define U 0
using namespace std;
int w,h;
int c[60][60];

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

  c[x][y]=U;
  for( int i=-1;i<=1;i++ ){
    for( int j=-1;j<=1;j++ ){
      if( i==j && i==0 ) continue;
      rec( x+i,y+j );
    }
  }
}

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];
    int ans=0;
    for( int i=0;i<h;i++ ){
      for( int j=0;j<w;j++ ){
	if( c[i][j]==R ){
	  ans++;
	  rec( i,j );
	}
      }
    }
    cout << ans << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P ,P> PP;
constexpr ll MOD = ll(1e9) + 7;
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(2e5) + 5;
constexpr double EPS = 1e-9;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i > 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

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

void dfs(int i, int j){
    field[i][j] = 0;

    for(int di=-1;di<=1;di++){
        for(int dj=-1;dj<=1;dj++){
            int ni = i + di, nj = j + dj;
            if(ni < 0 || ni >= h) continue;
            if(nj < 0 || nj >= w) continue;
            if(field[ni][nj] == 1){
                dfs(ni, nj);
            }
        }
    }
}

int main() {
    while(cin >> w >> h, w){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> field[i][j];
            }
        }
        int ans = 0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(field[i][j] == 1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}