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 <cassert>
#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cstdint>
#include <cmath>
#include <complex>
#include <chrono>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#include <random>
#include <memory>
#include <utility>
#include <limits>

/* template start */
 
#define rep(i, a, b) for (long long i = (a); (i) < (b); (i)++)
#define all(i) i.begin(), i.end()
#define debug(...) std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

void debug_out(){std::cerr<<std::endl;}

template<typename Head,typename... Tail>
void debug_out(Head h,Tail... t){
  std::cerr<<" "<<h<<":";
  debug_out(t...);
}
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (std::size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
 
template<typename T1,typename T2>
inline bool chmax(T1& a,T2 b){return a<b && (a=b,true);}
 
template<typename T1,typename T2>
inline bool chmin(T1& a,T2 b){return a>b && (a=b,true);}
 
long long pow_mod(long long a, long long b, long long mod=-1) {
  if(b==0)return 1;
  if ((a == 0)||(mod!=-1&&(a+mod)%mod==0))return 0;
  long long x = 1;
  while (b > 0) {
    if (b & 1)x = (mod!=-1)?(x * a) % mod:x*a;
    a = (mod!=-1)?(a * a) % mod:a*a;
    b >>= 1;
  }
  return x;
}

template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};

/* template end */

using ll = long long;
using P=std::pair<long long,long long>;

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

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

  ll w,h;

  while(std::cin>>w>>h,w||h){
    std::vector<std::vector<ll>> grid(h,std::vector<ll>(w));
    rep(i,0,h)rep(j,0,w)std::cin>>grid[i][j];

    auto dfs=[&](auto f,ll x,ll y)->void{
      grid[x][y]=2;
      rep(i,0,8){
        if(0<=x+dx[i]&&x+dx[i]<h&&0<=y+dy[i]&&y+dy[i]<w&&grid[x+dx[i]][y+dy[i]]==1)f(f,x+dx[i],y+dy[i]);
      }
    };

    ll ans=0;

    rep(i,0,h){
      rep(j,0,w){
        if(grid[i][j]==1){
          ans++;
          dfs(dfs,i,j);
        }
      }
    }
    std::cout<<ans<<"\n";
  }

  return 0;
}

C++: #include <utility>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int cnt;
int t[3]={-1,0,1};
    int map[55][55];
void dfs(int h,int w){
    if(map[h][w]==1){
        map[h][w]=0;
        for(int i=0;i<3;i++){for(int j=0;j<3;j++)dfs(h+t[i],w+t[j]);}
       }
}
int main()
{
    int w,h;

    while(cin>>w>>h,w+h){cnt=0;
        for(int i=0;i<55;i++){for(int j=0;j<55;j++)map[i][j]=0;}
        for(int i=0;i<h;i++){for(int j=0;j<w;j++)scanf("%d",&map[i+1][j+1]);}
        for(int i=0;i<53;i++){for(int j=0;j<53;j++){
            if(map[i][j]==1)cnt++;
            dfs(i,j);}}
        cout<<cnt<<endl;}
    return 0;
}

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

void proc(vector<int> &maps,int p,int w,int h){
	if(maps[p]==1) maps[p]=2;
	else return;
	int x=p%w;
	int y=p/w;
	for(int i=-1;i<=1;++i){
		for(int j=-1;j<=1;++j){
			int nx=x+j;
			int ny=y+i;
			if(nx>=0&&ny>=0&&nx<w&&ny<h){
				proc(maps,nx+ny*w,w,h);
			}
		}
	}
}

int main(void){
	int w,h;
	while(cin>>w>>h,w,h){
		vector<int> chizu(w*h);
		for(int i=0;i<h;++i){
			for(int j=0,a;j<w;++j){
				cin>>a;
				chizu[i*h+j]=a;
			}
		}
		int count = 0;
		for(int i=0;i<w*h;++i){
			if(chizu[i]==1){
				proc(chizu,i,w,h);
				++count;
			}
		}
		cout<<count<<endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
struct Point
{
	int x;
	int y;
	Point(int x = 0, int y = 0) :x(x), y(y) {}
};
const int MAX = 50;
int map[MAX][MAX];
bool book[MAX][MAX];
void depthFSearch(int h, int w, Point start)
{
	int next[9][2] = { { -1,-1 },{ -1,0 },{ -1,1 },{ 0,-1 },{ 0,0 },{ 0,1 },{ 1,-1 },{ 1,0 },{ 1,1 } };
	for (int k = 0; k <= 8; k++)
	{
		int nx = start.x + next[k][0];
		int ny = start.y + next[k][1];
		if (nx < 0 || nx >= h || ny < 0 || ny >= w || !map[nx][ny])
			continue;
		if (!book[nx][ny] && map[nx][ny])
		{
			book[nx][ny] = true;
			Point p = { nx,ny };
			depthFSearch(h, w, p);
		}
	}
	return;
}
int main()
{
	while (true)
	{
		int w, h;
		cin >> w >> h;
		if (!w && !h)
			break;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				cin >> map[i][j];
				book[i][j] = false;
			}
		int sum = 0;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				if (!book[i][j] && map[i][j])
				{
					book[i][j] = true;
					Point start(i, j);
					depthFSearch(h, w, start);
					sum++;
				}
			}
		cout << sum << endl;
	}
}