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

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

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

int nextInt() {int a; cin >> a; return a;}
char nextChar() {char a; cin >> a; return a;}
double nextDouble() {double a; cin >> a; return a;}
string nextString() {string a; cin >> a; return a;}

void inputVector(vector<int> &v, int &n) {rep(i,n){v.push_back(nextInt());}}
void inputVector(vector<double> &v, int &n) {rep(i,n){v.push_back(nextDouble());}}
void inputVector(vector<string> &v, int &n) {rep(i,n){v.push_back(nextString());}}

void paint(vector<string> &board, int y, int x) {
	queue<int> q;
	q.push(y * 100 + x);
	board[y][x] = '0';
	while (!q.empty()) {
		int now = q.front(); q.pop();

		int nowX = now % 100;
		int nowY = now / 100;

		for (int ay = -1; ay <= 1; ay++) {
			for (int ax = -1; ax <= 1; ax++) {
				int nx = nowX + ax;
				int ny = nowY + ay;
				if (nx < 0 || nx >= board[0].size() || ny < 0 || ny >= board.size()) continue;
				if (board[ny][nx] == '0') continue;

				q.push(ny * 100 + nx);
				board[ny][nx] = '0';
			}
		}
	}
}

signed main() {
	while (true) {
		int w, h;
		cin >> w >> h;

		if (w == 0) break;

		vector<string> board;
		rep(y, h) {
			string tmp = "";
			rep(x, w) {
				tmp += nextChar();
			}
			board.push_back(tmp);
		}

		int ret = 0;
		rep(y, h) {
			rep(x, w) {
				if (board[y][x] == '1') {
					paint(board, y, x);
					ret++;
				}
			}
		}

		cout << ret << endl;
	}

    return 0;
}

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

#define REP(i,a,n) for(int i = a ; i < n  ;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

#define MAX 100

int w, h;
int t[MAX][MAX];

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

bool destroy(int x, int y){
  if(t[y][x] == 0) return false;
  t[y][x] = 0;
  rep(i, 8){
    int nx = x + dx[i], ny = y + dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
    destroy(nx, ny);
  }
  return true;
}

void solve(){
  int res = 0;
  rep(i,h) rep(j,w) if(destroy(j,i)) res++;
  cout << res << endl;
}
int main(){
  while(cin >> w >> h){
    if( w==0 && h ==0) break;
    rep(i, h) rep(j, w) cin >> t[i][j];
    solve();
  }
}

C++: /**
 * @Author: yujirotakahashi
 * @Date:   2020-05-17T20:21:41+09:00
 * @Last modified by:   yujirotakahashi
 * @Last modified time: 2020-05-23T00:13:28+09:00
 */

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

/*******  All Required define Pre-Processors and typedef Constants *******/
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define rep(i,n) for(int i=0; i<(n); ++i)
#define rep1(i,n) for(int i=1; i<=(n); ++i)
#define Dcout(a) cout << setprecision(20) << a << endl
#define MP make_pair
#define PB push_back
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
//const ll INF = (1LL << 60);
//const double pi=acos(-1.0);
using ll = long long;
using PAIR = pair<int, int>;
#define debug cout << "line : " << __LINE__ << " debug" << endl
/****** Template of some basic operations *****/
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
/**************************************/

/******** User-defined Function *******/
using Graph = vector<vector<int>>;
int w,h;
int c[55][55];
bool seen[55][55];
int dx[8] = {-1,0,1,-1,1,-1,0,1};
int dy[8] = {-1,-1,-1,0,0,1,1,1};
void dfs(int y,int x){
    seen[y][x] = true;

    rep(i,8){
        int nx = x + dx[i], ny = y + dy[i];
        if(nx<0 || nx>=w || ny<0 || ny>=h) continue;
        if(seen[ny][nx] == true) continue;
        if(c[ny][nx] == 0) continue;

        dfs(ny,nx);
    }

    return;
}
/**************************************/

/********** Main()  function **********/
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(cin >> w >> h){
      if(w==0 && h==0) break;

      rep(i,h)rep(j,w) cin >> c[i][j];
      memset(seen, false, sizeof(seen));
      int cnt = 0;
      rep(i,h)rep(j,w){
          if(seen[i][j]==false && c[i][j] == 1){
              dfs(i,j);
              cnt++;
          }
      }

      cout << cnt << endl;
  }

  return 0;
}
/********  Main() Ends Here *************/

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

#define rep(i, n) for (int i=0; i<(n); ++i)
using ll = long long;
using P = pair<int, int>;

const int MAX = 53;
int W, H;
int field[MAX][MAX];

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


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

  rep(i,8) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || H <= nx) continue;
    if (ny < 0 || W <= ny) continue;
    if (field[nx][ny] == 0) continue;
    dfs(nx, ny);
  }
}

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

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

int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int w, h;

int field[51][51];
bool seem[51][51];

void dfs(int x, int y)
{
	field[y][x] = 0;
	for (int i = 0; i < 8; ++i)
	{
		int xx = x + dx8[i];
		int yy = y + dy8[i];

		if (xx < 0 || xx >= w || yy < 0 || yy >= h) continue;
		
		if (field[yy][xx] == 0) continue;

		dfs(xx, yy);
	}
}

int main(int argc, char **argv)
{
	while (true)
	{
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		memset(field, 0, sizeof(field));
		for (int y = 0; y < h; ++y)
			for (int x = 0; x < w; ++x)
				cin >> field[y][x];

		int c{0};
		memset(seem, 0, sizeof(seem));
		for (int y = 0; y < h; ++y)
		{
			for (int x = 0; x < w; ++x)
			{
				if (field[y][x] == 0) continue;
				dfs(x, y);
				++c;
			}
		}
		std::cout << c << std::endl;
	}
}