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<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

bool ok=true;
int c[60][60];
int w,h;
bool seen[60][60];
int dx[8]={1,-1,0,0,1,-1,1,-1};
int dy[8]={1,-1,1,-1,0,0,-1,1};

void dfs(int v,int u){
	if(!seen[v][u]){
		seen[v][u]=true;
		for(int i=0;i<8;i++){
			if(v+dx[i]>=0&&v+dx[i]<h&&u+dy[i]>=0&&u+dy[i]<w&&c[v+dx[i]][u+dy[i]]==1)dfs(v+dx[i],u+dy[i]);
		}
	}
	return;
}

int main(){
	while(ok){
		cin>>w>>h;
		if(w==0||h==0){
			ok=false;
			return 0;
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>c[i][j];
			}
		}
		for(int i=0;i<60;i++){
			for(int j=0;j<60;j++)seen[i][j]=false;
		}
		int ans=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(c[i][j]==1&&!seen[i][j]){
					ans++;
					dfs(i,j);
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
       
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
       

template <typename T>
class Grid {
  struct Pos {
    int i, j;
    Pos(int i, int j) : i(i), j(j) {}
  };

  int H, W;
  vector<vector<T>> G;

  vector<int> di8 = {1, 1, 0, -1, -1, -1, 0, 1},
              dj8 = {0, 1, 1, 1, 0, -1, -1, -1};
  vector<int> di4 = {1, 0, -1, 0}, dj4 = {0, 1, 0, -1};

  vector<Pos> neighbor(int i, int j, const vector<int>& dis,
                       const vector<int>& djs) const {
    vector<Pos> res;
    for (int k = 0; k < dis.size(); ++k) {
      if (in(i + dis[k], j + djs[k])) res.emplace_back(i + dis[k], j + djs[k]);
    }
    return res;
  }

 public:
  static bool in(int H, int W, int i, int j) {
    return 0 <= i && i < H && 0 <= j && j < W;
  }

  Grid(int H, int W) : H(H), W(W) { G.resize(H, vector<T>(W)); }

  vector<T>& operator[](int i) { return G[i]; }

  bool in(int i, int j) const { return (0 <= i && i < H && 0 <= j && j < W); }

  vector<Pos> neighbor8(int i, int j) const { return neighbor(i, j, di8, dj8); }

  vector<Pos> neighbor4(int i, int j) const { return neighbor(i, j, di4, dj4); }

  void scanFrom(istream& strm) { for (int i = 0; i < (int)(H); ++i) for (int j = 0; j < (int)(W); ++j) strm >> G[i][j]; }

  void outputTo(ostream& strm) {
    for (int i = 0; i < (int)(H); ++i) {
      for (int j = 0; j < (int)(W); ++j) cout << G[i][j];
      strm << endl;
    }
  }
};

template <typename T>
inline istream& operator>>(istream& strm, Grid<T>& g) {
  g.scanFrom(strm);
  return strm;
}

template <typename T>
inline ostream& operator<<(ostream& strm, Grid<T>& g) {
  g.outputTo(strm);
  return strm;
}

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

    function<void(int, int)> dfs = [&](int i, int j) {
      grid[i][j] = 0;
      for (auto& p : grid.neighbor8(i, j)) {
        if (grid[p.i][p.j]) dfs(p.i, p.j);
      };
    };

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

  return 0;
}

C++: #include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <utility>
#include <set>

class union_find {
  mutable std::vector<intmax_t> c;

public:
  union_find(size_t n): c(n, -1) {}

  size_t find(size_t v) const {
    if (c[v] < 0) return v;
    return (c[v] = find(c[v]));
  }

  bool unite(size_t u, size_t v) {
    u = find(u);
    v = find(v);
    if (u == v) return false;
    if (-c[u] > -c[v]) std::swap(u, v);
    c[v] += c[u];
    c[u] = v;
    return true;
  }

  bool connected(size_t u, size_t v) const { return (find(u) == find(v)); }

  size_t size() const { return c.size(); }
  size_t size(size_t v) const {
    return -c[find(v)];
  }
};

constexpr size_t m1 = -1;
constexpr size_t di[] = {m1, m1, m1, 0, 0, 1, 1, 1};
constexpr size_t dj[] = {m1, 0, 1, m1, 1, m1, 0, 1};

int testcase_ends() {
  size_t h, w;
  scanf("%zu %zu", &w, &h);
  if (h == 0 && w == 0) return 1;

  std::vector<std::vector<int>> c(h, std::vector<int>(w));
  for (auto& ci: c)
    for (auto& cij: ci) scanf("%d", &cij);

  union_find uf(h*w);
  for (size_t i = 0; i < h; ++i)
    for (size_t j = 0; j < w; ++j) {
      if (c[i][j] == 0) continue;
      for (size_t k = 0; k < 8; ++k) {
        size_t ni = i + di[k];
        size_t nj = j + dj[k];
        if (!(ni < h && nj < w)) continue;
        if (c[ni][nj] == 0) continue;
        uf.unite(i*w+j, ni*w+nj);
      }
    }

  std::set<size_t> r;
  for (size_t i = 0; i < h; ++i)
    for (size_t j = 0; j < w; ++j)
      if (c[i][j] == 1)
        r.insert(uf.find(i*w+j));

  printf("%zu\n", r.size());
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>

    using namespace std;

int map[50][50], width, height;
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}, dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

bool rangeCheck(int row, int col)
{
    if (row >= 0 && row <= height - 1 && col >= 0 && col <= width - 1)
        return true;
    else
    {
        return false;
    }
}

void erase(int row, int col)
{
    map[row][col] = 0;

    for (int i = 0; i < 8; i++)
    {
        if (rangeCheck(row + dy[i], col + dx[i]) == true && map[row + dy[i]][col + dx[i]] == 1)
            erase(row + dy[i], col + dx[i]);
    }
}

int main()
{

    int count;

    while (true)
    {
        scanf("%d %d", &width, &height);
        if (width == 0 && height == 0)
            break;

        for (int i = 0; i < height; i++)
        {
            for (int k = 0; k < width; k++)
            {
                scanf("%d", &map[i][k]);
            }
        }

        count = 0;

        for (int row = 0; row < height; row++)
        {
            for (int col = 0; col < width; col++)
            {
                if (map[row][col] == 1)
                {
                    count++;
                    erase(row, col);
                }
            }
        }

        printf("%d\n", count);
    }

    return 0;
}