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 <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <cstring>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for(int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int> >
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

#define MAX_V 1000000

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

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

  vector<int> ans;
  while(true) {
      int w, h;
      cin >> w >> h;
      if(w == 0 && h == 0) break;

      int c[h][w];
      bool reached[h][w];
      rep(i, 0, h) rep(j, 0, w) {
          cin >> c[i][j];
          reached[i][j] = false;
      }

      int rsl = 0;
      rep(y, 0, h) {
          rep(x, 0, w) {
              if(c[y][x] == 0) continue;
              if(reached[y][x]) continue;

              rsl++;
              stack<pair<int, int> > s;
              s.push(make_pair(x, y));
              reached[y][x] = true;
              while(!s.empty()) {
                  pair<int, int> p = s.top();
                  s.pop();
                  rep(i, 0, 8) {
                      int nx = p.first + dx[i], ny = p.second + dy[i];
                      if(!(0 <= nx && nx < w && 0 <= ny && ny < h)) continue;
                      if(c[ny][nx] == 0) continue;
                      if(reached[ny][nx]) continue;

                      reached[ny][nx] = true;
                      s.push(make_pair(nx, ny));
                  }
              }
          }
      }
      ans.push_back(rsl);
  }
  rep(i, 0, ans.size()) cout << ans[i] << endl;

}

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

int x,y;
const int mx[8]={-1,-1,-1,0,0,1,1,1};
const int my[8]={-1,0,1,-1,1,-1,0,1};

void dfs(vector<vector<int>> &c,int H,int W,int& h,int& w)
{
c[H][W] = 0;
for(int i=0;i<8;i++){y=H+my[i],x=W+mx[i];if(y<0||y>h-1||x<0||x>w-1)continue;if(!c[y][x])continue;dfs(c,y,x,h,w);}
}

int main()
{
int w,h,ans;
vector<vector<int>> c(50,vector<int>(50));
scanf("%d%d", &w, &h);
while(w!=0&&h!=0)
{
ans=0;
for(int i=0;i<h;i++)for(int j=0;j<w;j++)scanf("%d", &c[i][j]);
for(int i=0;i<h;i++)for(int j=0;j<w;j++){if(!c[i][j])continue;dfs(c,i,j,h,w);ans++;}
printf("%d\n",ans);
scanf("%d%d", &w, &h);
}
return 0;
}

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

using namespace std;

inline bool inRange(int a, int x, int b) {
    return a <= x && x < b;
}

void fill(vector<vector<int> > &v, int r, int c) {
    v[r][c] = 0;
    for(int dr = -1; dr <= 1; ++dr) {
        for(int dc = -1; dc <= 1; ++dc) {
            int nr = r + dr;
            int nc = c + dc;
            if(inRange(0, nr, v.size()) && inRange(0, nc, v[0].size()) && v[nr][nc]) {
                fill(v, nr, nc);
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(0);

    while(true) {
        int C, R;
        cin >> C >> R;
        if(!C && !R) break;

        vector<vector<int> > v(R, vector<int>(C));
        for(int i = 0; i < R; ++i) 
            for(int j = 0; j < C; ++j)
                cin >> v[i][j];

        int cnt = 0;
        for(int r = 0; r < R; ++r) {
            for(int c = 0; c < C; ++c) {
                if(v[r][c]) {
                    ++cnt;
                    fill(v, r, c);
                }
            }
        }

        cout << cnt << endl;
    }

    return 0;
}

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

static const int MAX_H = 50;
static const int MAX_W = 50;

int H, W;
int grid[MAX_H][MAX_W + 1];
bool reached[MAX_H][MAX_W];

#define OCTAS
#ifdef OCTAS
static const int dh[]{-1, -1, -1, 0, 0, +1, +1, +1};
static const int dw[]{-1, 0, +1, -1, +1, -1, 0, +1};
#else
static const int dh[]{-1, 0, 0, +1};
static const int dw[]{0, -1, +1, 0};
#endif

void initialize() {
//  memset(reached, 0, sizeof(reached));
  fill_n(reached[0], MAX_H * MAX_W, false);
}

void dfs(int h, int w) {
  reached[h][w] = true;

  for (int i{}; i < sizeof(dh) / sizeof(int); ++i) {
    int nh = h + dh[i];
    if (nh < 0 || nh >= H)
      continue;
    int nw = w + dw[i];
    if (nw < 0 || nw >= W)
      continue;
    if (grid[nh][nw] == 0 || reached[nh][nw])
      continue;
    dfs(nh, nw);
  }
}

int main() {
  while (true) {
    scanf("%d%d", &W, &H);
    if (H == 0 && W == 0)
      break;
    initialize();
    for (int h = 0; h < H; ++h) {
      for (int w = 0; w < W; ++w) {
        scanf("%d", &grid[h][w]);
      }
    }

    int ans{};
    for (int h = 0; h < H; ++h) {
      for (int w = 0; w < W; ++w) {
        if (grid[h][w] == 1 && !reached[h][w]) {
          dfs(h, w);
          ++ans;
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

C++: #include<bits//stdc++.h>
using namespace std;
int sum;
void solve(bool a[50][50],int h, int w, int i, int j){
	for(int z=-1;z<2;z++){
		for(int x=-1;x<2;x++){
			if(i+z>=0&&i+z<h&&j+x>=0&&x+z<w&&a[i+z][j+x]){
				a[i+z][j+x]=false;
				solve(a,h,w,i+z,j+x);
			}
		}
	}
}
int main(){
	int h,w;
	while(cin>>w>>h&&h!=0&&w!=0){
		bool a[50][50];
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				int x;
				cin>>x;
				if(x==0) a[i][j]=false;
				else a[i][j]=true;
			}
		}
		sum=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(a[i][j]){
					sum++;
					solve(a,h,w,i,j);
				}
			}
		}
		cout<<sum<<endl;
	}
}

C++: #include <iostream>
#include <stdlib.h>

using namespace std;

int d[8][2] = {
	{-1, -1},
	{0, -1},
	{1, -1},
	{-1, 0},
	{1, 0},
	{-1, 1},
	{0, 1},
	{1, 1}
};

void func(int **a, int s, int t, int i, int j)
{
	if(a[i][j] == 0){
		return ;
	}
	a[i][j] = 0;
	
	for(int k = 0; k < 8; k++){
		if((i+d[k][0] >= 0 && i+d[k][0] < t) && (j+d[k][1] >= 0 && j+d[k][1] < s)){
			func(a, s, t, i+d[k][0], j+d[k][1]);
		}
	}
	
	return ;
}

int main(int argc, char **argv)
{
	int s, t, **a, c, t2, s2;
	
	while(1){
		cin >> s >> t;
		if(s == 0 && t == 0){
			break;
		} else if(s == 1 || t == 1){
			t2 = t+1;
			s2 = s+1;
		} else {
			t2 = t;
			s2 = s;
		}
		a = (int **)calloc(t2, sizeof(int));
		for(int i = 0; i < t; i++){
			a[i] = (int *)calloc(s2, sizeof(int));
			for(int j = 0; j < s; j++){
				cin >> a[i][j];
			}
		}
		c = 0;
		
		for(int i = 0; i < t; i++){
			for(int j = 0; j < s; j++){
				if(a[i][j] == 1){
					c++;
					func(a, s, t, i, j);
				}
			}
		}
		cout << c << endl;
		
		for(int i = 0; i < s2; i++){
			free(a[i]);
		}
		free(a);
	}
	
	return 0;
}