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>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
int grid[100][100];
int H, W;

void dfs(int x, int y) {
  int mx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
  int my[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
  for (int i = 0; i < 8; i++) {
    int _x = x + mx[i];
    int _y = y + my[i];
    if (grid[_x][_y] == 1 && _x >= 0 && _y >= 0 && _x < W && _y < H) {
      grid[_x][_y] = 0;
      dfs(_x, _y);
    }
  }
}

signed main() {
  while (cin >> W >> H, W+H!=0) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> grid[j][i];
      }
    }
 
    int ans = 0; 
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (grid[j][i] == 1) {
          dfs(j,i);
          ans++;
        }
      }
    }

    cout << ans << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int m[50][50];
void del(int a,int b){
	m[a][b]=0;
	if(m[a-1][b]==1)del(a-1,b);
	if(m[a][b-1]==1)del(a,b-1);
	if(m[a+1][b]==1)del(a+1,b);
	if(m[a][b+1]==1)del(a,b+1);
	if(m[a-1][b-1]==1)del(a-1,b-1);
	if(m[a-1][b+1]==1)del(a-1,b+1);
	if(m[a+1][b-1]==1)del(a+1,b-1);
	if(m[a+1][b+1]==1)del(a+1,b+1);
}
int main(){
	int a,b,i,j,w,q;
	while(cin>>a>>b,a!=0&&b!=0){
		int c=0;
		for(i=0;i<b;i++)for(j=0;j<a;j++)cin>>m[i][j];
        for(i=0;i<b;i++)for(j=0;j<a;j++)if(m[i][j]==1){del(i,j);c++;}
        	cout<<c<<endl;
    }
}

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

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

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

  for(int i=-1;i<=1;i++){
    for(int j=-1;j<=1;j++){
      if(y+i<0 || x+j<0 || y+i>=h || x+j>=w)continue;
      if(g[y+i][x+j])dfs(y+i,x+j);
    }
  }
}

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

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

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

using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)

int w, h;
int C[50][50] = {};

void visit(int x, int y) {
  C[x][y] = 0;
  for (int ix = x - 1; ix <= x + 1; ix++) {
    for (int iy = y - 1; iy <= y + 1; iy++) {
      if (ix >= 0 && iy >= 0 && ix < w && iy < h && C[ix][iy] == 1) {
        visit(ix, iy);
      }
    }
  }
}

int main() {
  while (cin >> w >> h, w) {
    int ans = 0;
    C[50][50] = {};
    REP(j, h) {
      REP(i, w) { cin >> C[i][j]; }
    }
    REP(i, w) {
      REP(j, h) {
        if (C[i][j] == 1) {
          ans++;
          visit(i, j);
        }
      }
    }
    cout << ans << endl;
  }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int W, H;
// dfs
// https://qiita.com/drken/items/4a7869c5e304883f539b
void dfs(vector<vector<int>> &g, int h, int w) {
	g[h][w] = 0;
	for(int dh = -1; dh <= 1; dh++) {
		for(int dw = -1; dw <= 1; dw++) {
			int next_h = h + dh;
			int next_w = w + dw;
			if(next_h < 0 || next_h >= H || next_w < 0 || next_w >= W) continue;
			if(g[next_h][next_w] == 0) continue;
			dfs(g, next_h, next_w);
		}
	}
}

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

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){f(8)k(x+u[i],y+u[i+8]);d[x][y]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[0][i]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	//f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	//printf("%d\n",a);
	//main();
}

C++: #include <iostream>
using namespace std;

int field[64][64];
bool visited[64][64];
int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

void rec(int y, int x)
{
	if (visited[y][x] == false){
		return;
	}
	visited[y][x] = false;
	for (int i = 0; i < 8; i++){
		rec(y + dy[i], x + dx[i]);
	}
	return;
}

int main(void)
{
	int w, h;
	while (cin >> w >> h, w && h){
		for (int i = 0; i < 64; i++){
			for (int j = 0; j < 64; j++){
				field[i][j] = 0;
				visited[i][j] = false;
			}
		}
		for (int i = 1; i <= h; i++){
			for (int j = 1; j <= w; j++){
				cin >> field[i][j];
				if (field[i][j] == 1){
					visited[i][j] = true;
				}
			}
		}
		int ret = 0;
		for (int i = 0; i < 64; i++){
			for (int j = 0; j < 64; j++){
				if (visited[i][j] == true){
					rec(i, j);
					ret++;
				}
			}
		}
		cout << ret << endl;
	}
	
	return 0;
}

C++: #include<iostream>
using namespace std;

int main()
{
	while (true)
	{
		int w, h;
		int num = 0;
		cin >> w >> h;
		if (w == 0 && h == 0)break;

		int **c;
		c = new int*[h + 2];
		for (int i = 0; i < h + 2; i++)	c[i] = new int[w + 2]; 
		for (int i = 0; i < h + 2; i++)				  
		{
			for (int l = 0; l < w + 2; l++)	c[i][l] = 0;
		}

		for (int i = 1; i < h + 1; i++)
		{
			for (int l = 1; l < w + 1; l++)	cin >> (c[i])[l];
		}
		for (int i = 1; i < h + 1; i++)
		{
			for (int l = 1; l < w + 1; l++)
			{
				if (c[i][l] == 1)		     
				{
					if (c[i + 1][l - 1] == 0 && c[i + 1][l] == 0 && c[i + 1][l + 1] == 0 && c[i][l + 1] == 0)
					{
						num++; 
					}

					else if ((c[i + 1][l - 1] == 1 && c[i + 1][l] == 0) && (c[i + 1][l + 1] == 1 || c[i][l + 1] == 1))	   
					{
						c[i][l] = 0;
						c[i + 1][l] = 1; 
					}
					else
					{
						c[i][l] = 0; 
					}

				}
			}
		}
		cout << num << endl;
	}
	return 0;
}