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<algorithm>
#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000
int MOD=1000000007;
int w,h;
int tansaku[51][51];
int c[51][51];
int a[3]={1,-1,0};
void dfs(int x,int y){
  if(tansaku[x][y]||x<0||x>=h||y<0||y>=w||c[x][y]==0){return;}
  tansaku[x][y]=1;
  rep(i,3){
    rep(j,3){
          dfs(x+a[i],y+a[j]);
      }
    }
  }
signed main() {

while(true){
  cin>>w>>h;
  int ans=0;
if(w==0&&h==0){break;}
rep(i,51){
  rep(j,51){
    tansaku[i][j]=0;
  }
}
rep(i,h){rep(j,w)cin>>c[i][j];}
rep(j,h){
  rep(i,w){
    if((!tansaku[j][i])&&(c[j][i]==1)){dfs(j,i);ans++;}
  }
}
cout<<ans<<endl;
}
}

C++: #include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "YES" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "NO" << endl;  \
    }
#define IFYes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    }
#define IFyes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "no" << endl;  \
    }
#define DEBUG_OUTPUT_ARRAY(XXX, ONE)                            \
    for (int i = 0; i < (ONE); i++)                             \
    {                                                           \
        cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
    }
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO)                             \
    for (int i = 0; i < (ONE); i++)                                    \
    {                                                                  \
        cout << "<<< i = " << i << " >>>" << endl;                     \
        for (int j = 0; j < (TWO); j++)                                \
        {                                                              \
            cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
        }                                                              \
    }
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
    for (int i = 0; i < (ONE); i++)            \
    {                                          \
        cout << i << "  ";                     \
        for (int j = 0; j < (TWO); j++)        \
        {                                      \
            cout << XXX[i][j] << " ";          \
        }                                      \
        cout << endl;                          \
    }

typedef pair<long long int, long long int> pll;


const long long int mod = 1000000007;
const long long int INF = 99999999999999999;

long long int w,h,ch[100][100];

void dfs(long long int hhh,long long int www){
	ch[hhh][www]=0;
	if(ch[hhh][www+1]==1){dfs(hhh,www+1);}
	if(ch[hhh][www-1]==1){dfs(hhh,www-1);}
	if(ch[hhh+1][www]==1){dfs(hhh+1,www);}
	if(ch[hhh-1][www]==1){dfs(hhh-1,www);}
	
	if(ch[hhh+1][www+1]==1){dfs(hhh+1,www+1);}
	if(ch[hhh-1][www-1]==1){dfs(hhh-1,www-1);}
	if(ch[hhh+1][www-1]==1){dfs(hhh+1,www-1);}
	if(ch[hhh-1][www+1]==1){dfs(hhh-1,www+1);}
}

int main()
{
    cout << fixed << setprecision(18);
	cin>>w>>h;
	while(w!=0&&h!=0){
		long long int res=0;
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				cin>>ch[i][j];
			}
		}
		for(int i=0;i<=h;i++){
			ch[i][0]=0;
			ch[i][w+1]=0;
		}
		for(int i=0;i<=w;i++){
			ch[0][i]=0;
			ch[h+1][i]=0;
		}
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				if(ch[i][j]==1){
					res++;
					dfs(i,j);
				}
			}
		}
		cout<<res<<endl;
		cin>>w>>h;

	}
}

C++: #include<iostream>
using namespace std;
int ans;
int map[100][100];
void dfs(int x,int y,int w,int h);
int dx[8] = {1,1,0,-1,-1,-1,0,1},dy[8] = {0,1,1,1,0,-1,-1,-1};

int main(){
  int w,h;
  while(cin>>w>>h){
    ans=0;
    if(w == 0 && h == 0)break;
    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){
	  map[i][j] = 0;
	  ans++;
	  dfs(j,i,w,h);
	}
      }
    // cout<<"map"<<endl;
    // for(int a=0;a<h;a++){
    //   for(int b=0;b<w;b++) cout<<map[a][b];
    //   cout<<endl;
    // }
    //cout<<endl;
    cout<<ans<<endl;
  }
  return 0;
}

void dfs(int x,int y,int w,int h){
  //cout<<"LK+LKJ"<<endl;
  int nx,ny;
  for(int i = 0;i<8;i++){
    nx = x+dx[i]; ny = y+dy[i];
    if(nx>=0 && nx<w && ny>=0 && ny<h && map[ny][nx] == 1){
      map[ny][nx] = 0;
      dfs(nx,ny,w,h);
    }
  }
}

C++: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
const double PI=3.14159265358979323846;
const in MOD = 1000000007;
const in INF=1<<30;
using P=pair<int,int>;
vector<int> dx={0,1,-1,0,1,-1,1,-1};
vector<int> dy={1,0,0,-1,1,1,-1,-1};
vector<vector<int>> a(0);
int h,w;

void dfs(int x,int y){
    a[x][y]=0;
    rep(i,8){
        int nx=x+dx[i],ny=y+dy[i];
        if(nx>=h||nx<0||ny>=w||ny<0) continue;
        if(a[nx][ny]==0) continue;
        dfs(nx,ny);
    }
}

int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cout << fixed << setprecision(10);
    cin>>w>>h;
    vector<int> ans(0);
    while(h+w>0){
        int cnt=0;
        a=vector<vector<int>>(h,vector<int>(w));
        rep(i,h)rep(j,w) cin>>a[i][j];
        rep(i,h)rep(j,w){
            if(a[i][j]==1){dfs(i,j);cnt ++;}
        }
        cin>>w>>h;
        ans.emplace_back(cnt);
    }
    for(auto e:ans) cout<<e<<endl;
}