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>
using namespace std;

int W, H;

int tansaku(vector<vector<int>> &data, int h, int w) {
    int ret = 0;
    if(data[h][w] == 1) {
        ret = 1;
        data[h][w] =0;
        if(h-1 >= 0          ) ret |= tansaku(data, h-1, w);
        if(h+1 <  H          ) ret |= tansaku(data, h+1, w);
        if(           w-1 >=0) ret |= tansaku(data, h  , w-1);
        if(           w+1 < W) ret |= tansaku(data, h  , w+1);
        if(h-1 >=0 && w-1 >=0) ret |= tansaku(data, h-1, w-1);
        if(h-1 >=0 && w+1 < W) ret |= tansaku(data, h-1, w+1);
        if(h+1 < H && w-1 >=0) ret |= tansaku(data, h+1, w-1);
        if(h+1 < H && w+1 < W) ret |= tansaku(data, h+1, w+1);
    }
    return ret;
}

int func() {
    cin >> W >> H;
    if(W==0 && H==0) return 0;
    //int data[H][W];
    vector<vector<int>> data;
    data.resize(H);

    for(int h=0; h<H; h++) {
        for(int w=0; w<W; w++) {
            int tmp;
            cin >> tmp;
            //data[h][w] = tmp;
            data[h].push_back(tmp);
        }
    }


    int counter = 0;
    for(int h=0; h<H; h++) {
        for(int w=0; w<W; w++) {
            counter += tansaku(data, h, w);
        }
    }
    cout << counter << endl;
    
    return 1;
}

int main() {
    while(func()) {}
    return 0;
}

C++:  #include<bits/stdc++.h>
 using namespace std;
#define INF 1000000000
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
typedef long long LL;
int w,h;
int c[101][101];
int dx[8]={0,0,1,1,1,-1,-1,-1};
int dy[8]={1,-1,0,1,-1,0,1,-1};
void dfs(int x,int y){
     c[x][y]=0;
     REP(i,8){
         if(c[x+dx[i]][y+dy[i]]==1){
            dfs(x+dx[i],y+dy[i]);
         }
     }
}
int main(){
    while(cin>>w>>h,w,h){
    REP(i,h+1){
        REP(j,w+1){
            c[i][j]=0;
        }
    }
    REP(i,h){
        REP(j,w){
            cin>>c[i+1][j+1];
        }
    }
    int ans=0;
    REP(i,h+1){
        REP(j,w+1){
            if(c[i][j]==1){
               dfs(i,j);
               ans++;
            }
        }
    }
    cout<<ans<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int, int> P;

int main(void){
  int w, h;
  int num=0;
  int map[51][51];
  int dir[8][2]={{1,0},{1,1},{1,-1},{0,1},{0,-1}, {-1,0},{-1,1},{-1,-1}};
  queue<P> que;
  cin >> w >> h;
  while(w+h){
    //input
    for (size_t y = 0; y < h; y++) {
      for (size_t x = 0; x < w; x++) {
        cin >> map[y][x];
      }
    }

    //output
    for (size_t y = 0; y < h; y++) {
      for (size_t x = 0; x < w; x++) {
        if (map[y][x]==1) {
          //search islands & added que & replace 0
          que.push(P(y, x));
          map[y][x]=0;
          while(que.size()){
            P p = que.front(); que.pop();
            for (size_t i = 0; i < 8; i++) {
              if (0<=p.first+dir[i][0] && p.first+dir[i][0]<h &&
              0<=p.second+dir[i][1] && p.second+dir[i][1]<w) {
                if(map[ p.first+dir[i][0] ][ p.second+dir[i][1] ]==1){
                  //added que & replace 0
                  que.push(P(p.first+dir[i][0], p.second+dir[i][1]));
                  map[ p.first+dir[i][0] ][ p.second+dir[i][1] ]=0;
                }
              }
            }
          }
          num+=1;
        }
      }
    }
    cout << num << endl;
    cin >> w >> h;
    num=0;
  }
  return 0;
}