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 <iostream>
#include <queue>
using namespace std;

int map[20][20];

typedef pair<int, int> pxy;

int h, w, ans = 0;

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

void bfs(int sx, int sy)
{
  queue<pxy> q;
  q.push(pxy(sx, sy)); 
  while(!q.empty()){
    int x, y;
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if(map[x][y]){
      map[x][y] = 0;
      for(int i = 0; i < 8; i++){
	int next_x = x + dx[i], next_y = y + dy[i];
	if(map[next_x][next_y]){
	  q.push(pxy(next_x, next_y));
	}
      }
    }
  }
}

int main()
{
  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0){
      break;
    }else{
      for(int i = 0; i < h; i++)
	for(int j = 0; j < w; j++)
	  cin >> map[j][i];
      for(int i = 0; i < h; i++)
	for(int j = 0; j < w; j++)
	  if(map[j][i]){
	    ans++;
	    bfs(j, i);
	  }
      cout << ans << endl;
      ans = 0;
    }
  }
}

C++: #include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define dbg(x) cout<<#x"="<<x<<endl

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;

#define INF 1000000000
#define SIZE 51

bool f[SIZE][SIZE]; //true=land (1)
int w,h;

void wfs(int i, int j){
  //width-first search
  int d[]={1,0,-1};
  rep(di,3){
    int ni=i+d[di];
    if(ni<0 || ni>=h) continue;
    rep(dj,3){
      int nj=j+d[dj];
      if(nj<0 || nj>=w) continue;
      if(f[ni][nj]){
	f[ni][nj]=false;
	wfs(ni,nj);
      }
    }
  }
}

int main(){
  while(true){
    cin >>w>>h;
    if(w==0&&h==0) break;
    
    rep(i,h){
      rep(j,w){
	int d;
	scanf("%d", &d);
	if(d==0) f[i][j]=false;
	else  f[i][j]=true; //1=land
      }
    }

    int ans=0;
    rep(i,h){
      rep(j,w){
	if(f[i][j]){
	  ans++;
	  wfs(i,j);
	}
      }
    }

    cout << ans << endl;

  }
}

C++: #include<iostream>
#include<queue>

using namespace std;

int w,h;
int map[100][100];

void delIsland(int y,int x){

  queue<pair<int,int> > que;
  que.push(make_pair(y,x));


  int next[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
  while(!que.empty()){
    pair<int,int> now  = que.front();
    que.pop();

    if(now.first<0 || now.first>=h || now.second<0 || now.second>=w || map[now.first][now.second]!=1){
      continue;
    }else{
      map[now.first][now.second] = 0;
    }


    for(int i=0;i<8;i++){
      int ny = now.first + next[i][0];
      int nx = now.second + next[i][1];
      que.push(make_pair(ny,nx));
    }
  }
}

int main(){
  cin>>w>>h;
  while(w!=0 && h!=0){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
       cin>>map[i][j];
     }
   }

   int ans = 0;
   for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
     if(map[i][j] == 1){
       ans++;
       delIsland(i,j);
     }
   }
 }

 cout<<ans<<endl;
 cin>>w>>h;
}

return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<cstdlib>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int qx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int qy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

vector<vector<int> > mp;
int w, h;


void hoge(int wo, int ho) {
	mp[wo][ho] = 0;
	REP(i, 8) {
		int nw = wo + qx[i];
		int nh = ho + qy[i];
		if(nw >= 0 && nw < w && nh >= 0 && nh < h && mp[nw][nh] == 1) hoge(nw, nh);
	}
}
int main(void) {
	while(true) {
	ll count = 0;
	cin>>w>>h;
		if(w == 0 && h == 0) return 0;
		swap(w, h);
	mp.resize(w);
	REP(i, w) mp[i].resize(h);
	REP(i, w) {
		REP(j, h) {
			cin>> mp[i][j];
		}
	}
	REP(i, w) {
		REP(j, h) {
			if(mp[i][j] == 1) {
				count++;
				hoge(i, j);
			}
		}
	}
	cout<<count<<endl;
}
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long L;
L X[]={0,1,1,1,0,-1,-1,-1};
L Y[]={1,1,0,-1,-1,-1,0,1};
L h,w,mark[51][51],c=0;
L grid[51][51];
void call(L x,L y){
    mark[x][y]=1;
    for(L i=0;i<8;i++){
        L xx=X[i]+x,yy=Y[i]+y;
        if((xx>=0&&xx<h &&yy >=0&&yy<w) && grid[xx][yy] &&mark[xx][yy]==0){
            call(xx,yy);
        }
    }
}
int main()
{
    while(scanf("%lld%lld",&w,&h)&& w||h ){
        for(L i=0;i<h;i++){
            for(L j=0;j<w;j++)scanf("%lld",&grid[i][j]);
        }
        for(L i=0;i<h;i++){
            for(L j=0;j<w;j++){
                if(!mark[i][j] && grid[i][j]){
                    c++;
                    call(i,j);
                }
            }
        }
        printf("%lld\n",c);c=0;
        memset(mark,0,sizeof(mark));
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
using Graph = vector<vector<int>>;

int H, W;
vector<int> f1{-1, -1, -1, 0, 0, 0, 1, 1, 1};
vector<int> f2{-1, 0, 1, -1, 0, 1, -1, 0, 1};
void dfs(Graph &G, int h, int w){
  G[h][w] = -1;
  rep(i, 9){
    int nh = h + f1[i];
    int nw = w + f2[i];
    if (nh < 0 || nw < 0 || nh >= H || nw >= W) continue;
    if (G[nh][nw] == -1 || G[nh][nw] == 0) continue;
    dfs(G, nh, nw);
  }
}

int main(){
  while(1){
    cin >> W >> H;
    if (H == 0 && W == 0) break;
    Graph G(H, vector<int>(W));
    rep(i, H)rep(j, W) cin >> G[i][j];
    int count = 0;
    rep(i, H)rep(j, W){
      if (G[i][j] == -1 || G[i][j] == 0) continue;
      dfs(G, i, j);
      count++;
    }
    cout << count << endl;
  }
  
  return 0;
}