Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

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

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) {
    queue<pair<int,int> > q;
    q.push(make_pair(r, c));
    v[r][c] = 0;
    while(!q.empty()) {
        const pair<int,int> cur = q.front();
        q.pop();
        for(int dr = -1; dr <= 1; ++dr) {
            for(int dc = -1; dc <= 1; ++dc) {
                int nr = cur.first + dr;
                int nc = cur.second + dc;
                if(inRange(0, nr, v.size()) && 
                   inRange(0, nc, v[0].size()) && v[nr][nc])
                {
                    v[nr][nc] = 0;
                    q.push_back(make_pair(nr, nc))
                }
            }
        }
    }
}

int main() {
    cin.tie(0);
    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>
 
using namespace std;
 
int c[64][64];
 
void paint(int y, int x) {
  if (y < 0 || y >= 50 || x < 0 || x >= 50) return;
  if (c[y][x] == 0) return;
  c[y][x] = 0;
  for (int dy = -1; dy <= 1; ++dy) {
    for (int dx = -1; dx <= 1; ++dx) {
      paint(y + dy, x + dx);
    }
  }
}
 
int main() {
  int H, W;
  while (cin >> W >> H, H) {
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> c[i][j];
      }
    }
    int res = 0;
    for (int y = 0; y < H; ++y) {
      for (int x = 0; x < W; ++x) {
        if (c[y][x] == 1) {
          ++res;
          paint(y, x);
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define rep3(i, s, n) for (int i = s; i > (int)(n); i--)
#define all(obj) obj.begin(), obj.end()
#define db(x) cerr << #x << ":" << x << " "
#define dbl(x) cerr << #x << ":" << x << "\n"
#define dbv(vec) cerr << #vec << ":"; for (auto e : vec) cerr << e << " "; cout << "\n"
#define dbvv(vv) cerr << #vv << ":\n"; for (auto vec : vv) { for (auto e : vec) cerr << e << " "; cerr << "\n"; }
#define YN(f) cout << (f ? "YES" : "NO") << "\n"
#define Yn(f) cout << (f ? "Yes" : "No") << "\n"
#define yn(f) cout << (f ? "yes" : "no") << "\n"
using ll = long long;
using vi = vector<int>; using vl = vector<ll>; using vs = vector<string>;
using vvi = vector<vi>; using vvl = vector<vl>;
using pii = pair<int, int>; using pll = pair<ll, ll>;
using vpii = vector<pii>; using vpll = vector<pll>;
using ti3 = tuple<int, int, int>; using tl3 = tuple<ll, ll, ll>;

vector<vector<bool>> c;
vector<vector<bool>> r;

void dfs(int i, int j) {
	r[i][j] = true;	
	vi di = { 1, 1, 0, -1, -1, -1, 0, 1 };
	vi dj = { 0, 1, 1, 1, 0, -1, -1, -1 };
	rep(k, 8) {
		int in = i + di[k], jn = j + dj[k];
		if (c[in][jn] and !r[in][jn]) dfs(in, jn);
	}
}

int main() {
	while (true) {
		int w, h; cin >> w >> h;
		if (w == 0 and h == 0) break;
		c.assign(h + 2, {}); rep(i, h + 2) c[i].assign(w + 2, false);
		rep2(i, 1, h + 1) {
			rep2(j, 1, w + 1) {
				int cij; cin >> cij;
				c[i][j] = cij == 1;
			}
		}

		// dbvv(c);
		r.assign(h + 2, {}); rep(i, h + 2) r[i].assign(w + 2, false);
		int ans = 0;		
		rep2(i, 1, h + 1) rep2(j, 1, w + 1) {
			if (c[i][j] and !r[i][j]) {
				dfs(i, j);
				ans++;
			}
		}
		// dbl(ans);
		cout << ans << endl;
	}
}

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

using namespace std;

struct Pos { int y, x; };

int main(void)
{
    int w, h;
    int map[52][52] = {{0}};
    int nx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
    int ny[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
    queue<Pos> poss;
    Pos now;
    int ans;
    while (true)
    {
        cin >> w >> h;
        if (!w && !h) break;
        ans = 0;
        for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> map[i][j];
        for (int i = 1; i <= h; i++)
        {
            for (int j = 1; j <= w; j++)
            {
                if (map[i][j])
                {
                    poss.push({i, j});
                    while (!poss.empty())
                    {
                        now = poss.front();
                        poss.pop();
                        if (!map[now.y][now.x]) continue;
                        else map[now.y][now.x] = 0;
                        for (int k = 0; k < 8; k++) if (map[now.y + ny[k]][now.x + nx[k]]) poss.push({now.y + ny[k], now.x + nx[k]});
                    }
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>

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

using namespace std;

int area[50][50], cnt = 0;
int w, h;

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

  for(int dx = -1 ; dx <= 1 ; dx++){
    for(int dy = -1 ; dy <= 1 ; dy++){
      int nx = x + dx, ny = y + dy;

      if(0 <= nx && nx < w && 0 <= ny && ny < h && area[nx][ny] == 1){
	dfs(nx, ny);
      }
    }
  }
}

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

    rep(j, h){
      rep(i, w){
	cin >> area[i][j];
      }
    }

    rep(i, w){
      rep(j, h){
	if(area[i][j] == 1){
	  dfs(i, j);
	  cnt++;
	}
      }
    }
    
    cout << cnt << '\n';
    cnt = 0;
  }
}

C++: #include <iostream>
#include <vector>
using namespace std;
int ans;
int w, h;
//?§???????
vector<int> vx = { -1, 0, 1, -1, 1, -1, 0, 1 };
vector<int> vy = { 1, 1, 1, 0, 0, -1, -1, -1 };
vector<vector<int>> map;
void Search(int i,int j){
	//??¨????????????????????§??????
	for (int m = 0; m < vx.size(); m++){
		int x = j + vx[m];
		int y = i + vy[m];
		if (x >= 0 && x < w &&
			y >= 0 && y < h){
			if (map[y][x] == 1){
				map[y][x] = map[i][j];
				Search(y,x);
			}
		}
	}
}

int main(){
	while (1){
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		ans = 1;
		map = vector<vector<int>>(h,vector<int>(w));
		//???????????????
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				cin >> map[i][j];
			}
		}

		//??¢?´¢
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (map[i][j] != 1) continue;
				ans++;
				Search(i,j);
			}
		}
		cout << --ans << endl;
	}
	return 0;
}