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>
#include <iomanip>
#include <math.h>
//#include <unistd.h>
//#include <stdio.h>
#define rep(i,x) for(ll i = 0;i<x;i++)
#define all(a) (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using namespace std;
using dou = double;
const ll inf = 2147483647;
const ll INF = 1LL << 60;
const ll mod = 1000000007;
const ld pi = 3.14159265358;
typedef pair<ll,ll> P;
using graph = vector<vector<ll>>;
template<class T> inline bool chmax(T& a, T b){if(a < b){a = b; return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a > b){a = b; return true;}return false;}
ll gcd(ll a,ll b){
    if(a < b)swap(a,b);
    if(a % b == 0)return b;
    else return gcd(b,a%b);
}
//素数判定O(sqrt(N))
bool isp(ll n){
    bool res = true;
    if(n == 1)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}

ll fac[510000], finv[510000], inv[510000];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < 510000; i++){
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}

// 二項係数計算
ll COM(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
//nのm乗をMODで割ったあまりO(logN)
ll modpow(ll n,ll m,ll MOD){
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}

//printf("%.10f\n", N);
/*  vector<ll> vec = {1,2,5,6,8,9,10};
    cout << binary_search(all(vec),5) << endl; -> true*/
/*
    vector<ll> vec = {1,2,5,6,8,9,10};
    auto n = upper_bound(all(vec),4);
    cout << *n << endl; -> 5*/

//    cout << fixed << setprecision(15);

ll H,W;
const ll dx[] = {1,1,1,0,0,-1,-1,-1};
const ll dy[] = {1,0,-1,1,-1,0,1,-1};
char shima[55][55];
void dfs(ll x,ll y){
    shima[x][y] = '0';
    rep(i,8){
        ll hx = x + dx[i],hy = y + dy[i];
        if(hx < H && hy < W && hx >= 0 && hy >= 0 && shima[hx][hy] == '1'){
            dfs(hx,hy);
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(true){
        cin >> W >> H;
        if(H == 0 && W == 0)break;/*
        rep(i,55){
            rep(j,55){
                shima[i][j] = '0';
            }
        }*/
        rep(i,H){
            rep(j,W){
                cin >> shima[i][j];
            }
        }
        ll ans = 0;
        rep(i,H){
            rep(j,W){
                if(shima[i][j] == '1'){
                    dfs(i,j);
                    ans++;/*
                    rep(i,H){
                        rep(j,W){
                            cout << shima[i][j] << " ";
                        }
                        cout << "\n";
                    }*/
                }
            }
        }
        cout << ans << "\n";
    }
}

C++: #include <bits/stdc++.h>
#define ALL(a)  (a).begin(),(a).end()
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
#define MOD 1000000007

int w, h;
vvi c(60, vi(60));
vector<vector<bool>> flag(60, vector<bool>(60));

void dfs(int i, int j){
  flag.at(i).at(j) = false;

  vector<int> dx = {0, 0, 1, -1, 1, -1, -1, 1};
  vector<int> dy = {1, -1, 0, 0, 1, 1, -1, -1};
  for(int k = 0; k < 8; k++){
    int y = i + dy.at(k);
    int x = j + dx.at(k);
    if(x < 0 || x >= w || y < 0 || y >= h) continue;
    if(c.at(y).at(x) && flag.at(y).at(x)) dfs(y, x);
  }
}

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

  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      cin >> c.at(i).at(j);
      flag.at(i).at(j) = true;
    }
  }
  int result = 0;
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      if(c.at(i).at(j) && flag.at(i).at(j)){
        dfs(i, j);
        result++;
      }
    }
  }
  cout << result << endl;
}
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dx[8] = {-1, 0, 1, -1, 0, -1, 0, 1};

void dfs(VVI &table, int i, int j) {
    table[i][j] = 0;
    for (int loop = 0; loop < 8; loop++) {
        int new_i = i + dy[loop];
        int new_j = j + dx[loop];
        if (table[new_i][new_j] == 1) {
            dfs(table, new_i, new_j);
        }
    }
    return;
}

int main(void) {
    while(true) {
        int w;
        int h;
        cin >> w >> h;
        if (h == 0) {
            break;
        }
        VVI table(h + 2, VI(w + 2, 0));
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                cin >> table[i][j];
            }
        }
        int number = 0;
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                if (table[i][j] == 1) {
                    dfs(table, i, j);
                    number++;
                }
            }
        }
        cout << number << endl;
    }
    return 0;
}

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

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

int c[50][50];
int w, h;

void dfs(int x, int y)
{
  if (x < 0 || y < 0 || w == x || h == y) return;
  if (c[x][y] == 0) return;
  c[x][y] = 0;
  for (int i = 0; i < 8; i++){
    dfs(x + dx[i], y + dy[i]);
  }
}

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

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