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 <vector>
using namespace std;
using Mat = vector<vector<bool>>;
int w, h;
const int dxy[10] = {0,1,1,1,0,-1,-1,-1,0,1};
void search(const Mat &map, Mat &searched, int x, int y, int dir) {
    if (x < 0 || w <= x || y < 0 || h <= y) {
        return;
    } else if (!map[x][y] || searched[x][y]) {
        return;
    }
    
    searched[x][y] = true;
    
    for (int i = 0; i < 8; ++i) {
        search(map, searched, x + dxy[i], y + dxy[i+2], i);
    }
}
int main(void){
    while (cin >> w >> h && w && h) {
        Mat map(w, vector<bool>(h, false));
        for (int y = 0; y < h; ++y) {
            for (int x = 0; x < w; ++x) {
                int c;
                cin >> c;
                map[x][y] = c;
            }
        }
        Mat searched(w, vector<bool>(h, false));
        int count = 0;
        for (int y = 0; y < h; ++y) {
            for (int x = 0; x < w; ++x) {
                if (searched[x][y] || !map[x][y]) continue;
                search(map, searched, x, y, -1);
                ++count;
            }
        }
        cout << count << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Point{int x,y;};
int main(){
  int w,h,p[51][51]={},v[51][51]={},i,j,k,o;
  queue <Point> q;
  Point n,b;
  cin >> w >> h;
  while(w!=0){
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	cin >> p[i][j];
	v[i][j]=0;
      }
      
    }
    o=0;
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	if(v[i][j]==0&&p[i][j]==1){
	  o++;
	  n.x=i;n.y=j;
	  q.push(n);
	  while(!q.empty()){
	    n=q.front();
	    q.pop();
	    v[n.x][n.y]=1;
	    if(n.x+1<w&&v[n.x+1][n.y]==0&&p[n.x+1][n.y]==1) {
	      b.x=n.x+1;b.y=n.y;
	      q.push(b);
	    }
	    if(n.y+1<h&&v[n.x][n.y+1]==0&&p[n.x][n.y+1]==1) {
	      b.x=n.x;b.y=n.y+1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&v[n.x-1][n.y]==0&&p[n.x-1][n.y]==1) {
	      b.x=n.x-1;b.y=n.y;
	      q.push(b);
	    }
	    if(n.y-1>=0&&v[n.x][n.y-1]==0&&p[n.x][n.y-1]==1) {
	      b.x=n.x;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y+1<h&&v[n.x+1][n.y+1]==0&&p[n.x+1][n.y+1]==1) {
	      b.x=n.x+1;b.y=n.y+1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y-1>=0&&v[n.x-1][n.y-1]==0&&p[n.x-1][n.y-1]==1) {
	      b.x=n.x-1;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y-1>=0&&v[n.x+1][n.y-1]==0&&p[n.x+1][n.y-1]==1) {
	      b.x=n.x+1;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y+1<h&&v[n.x-1][n.y+1]==0&&p[n.x-1][n.y+1]==1) {
	      b.x=n.x-1;b.y=n.y+1;
	      q.push(b);
	    }
	  }
	}
      }
    }
    cout << o << endl;
    cin >> w >> h;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <cstring>

using namespace std;
using ll = long long;
using pint = pair<int, int>;
using Graph = vector<vector<int>>;

ll INF = 1LL << 60;

int W, H;

void dfs(Graph& field, int h, int w) {
	field[h][w] = 0;
	for (int dh = -1; dh <= 1; dh++) {
		for (int dw = -1; dw <= 1; dw++) {
			if (dh == 0 && dw == 0) continue;
			int nh = h + dh;
			int nw = w + dw;
			if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
			if (field[nh][nw] == 0) continue;
			dfs(field, nh, nw);
		}
	}
}

int main() {
	while (cin >> W >> H) {
		if (W == 0 && H == 0) break;
		Graph field(H, vector<int>(W));
		for (int h = 0; h < H; h++) {
			for (int w = 0; w < W; w++) cin >> field[h][w];
		}
		int count = 0;
		for (int h = 0; h < H; h++) {
			for (int w = 0; w < W; w++) {
				if (field[h][w] == 1) {
					dfs(field, h, w);
					count++;
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

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

int W,H;
int C[111][111];

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

bool used[111][111];
void solve(int x,int y){
  if( used[x][y] ) return;
  used[x][y] = true;
  for(int i=0;i<8;i++){
    int nx = x + dx[i], ny = y + dy[i];
    if( nx < 0 || ny < 0 || nx >= W || ny >= H ) continue;
    if( C[nx][ny] == 1 )
      solve( nx, ny );
  }
}

int main(){

  while(  cin >> W >> H && (W||H) ){
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	cin >> C[j][i];
      }
    }
    
    int res = 0;
    memset(used,0,sizeof(used));
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	if( !used[j][i] && C[j][i] == 1 )
	  {
	    res++;
	    solve(j,i);
	  }
      }
    }
    cout << res << endl;
  }


}

C++: #include <iostream>

using namespace std;
int field[50][50];
int w, h, ans;
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}, dy[8] = {-1, -1, -1, 0,  0, 1, 1, 1};

void check(int x, int y)
{
    if (field[x][y])
        field[x][y] = 0;
    else
        return;
        
    for (int i=0; i<8; i++) {
        int nx = x+dx[i], ny = y+dy[i];
        if (0<= nx && nx<w && 0<=ny&&ny<h) {
            check(nx, ny);
        }
    }
}

int main()
{
    while (cin>>w>>h, w||h) {
        for (int y=0; y<h; y++) {
            for (int x=0; x<w; x++) {
                int i; cin >> i;
                field[x][y] = i;
                }
           }
       
       ans = 0;
       for (int y=0; y<h; y++) {
          for (int x=0; x<w; x++) {
               if (field[x][y]) {
                       ans++;
                       check(x, y);
               }  
            }   
        }

        cout << ans << endl;
   }           
    
    return 0;
}

C++: #include <math.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define INF 1e9
#define MOD 1e9 + 7
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, a, n) for (int i = a; i < n; i++)
#define all(in) in.begin(), in.end()
int c[51][51];
int w, h;
void dfs(int x, int y) {
    c[x][y] = 0;
    for (int i = -1; i <= 1; i++) {
        for (int k = -1; k <= 1; k++) {
            int new_x = x + i, new_y = y + k;
            if (0 <= new_x && new_x < h && 0 <= new_y && new_y < w &&
                c[new_x][new_y] == 1) {
                dfs(new_x, new_y);
            }
        }
    }
}

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