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 <cmath>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <tuple>
#include <set>
#include <stack>
#include <queue>
#define REP(i, n) for(LL i = 0;i < n;i++)
#define REPR(i, n) for(LL i = n;i >= 0;i--)
#define FOR(i, m, n) for(LL i = m;i < n;i++)
#define FORR(i, m, n) for(LL i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define out(x, y) cout << x << " " << y << endl

#define INF 999999999
#define MOD 1000000007
using namespace std;
typedef long long LL;
//typedef LL int;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> PP;
typedef pair<LL, LP> LPP;
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

int main(){
    int w, h;
    while(cin >> w >> h, w + h){
        int c[51][51];
        REP(i, h){
            REP(j, w){
            cin >> c[i][j];
            }
        }

        int ans = 0;

        REP(i, h){
            REP(j, w){
                if(c[i][j] == 0) continue;
                else if(c[i][j] == 1){
                    queue<int> que;
                    que.push(i); que.push(j);
                    while(!que.empty()){
                        int x, y;
                        y = que.front(); que.pop();
                        x = que.front(); que.pop();
                        
                        c[y][x] = 0;
                        
                        //??????
                        if(y-1 >= 0 && y-1 < h && x-1 >= 0 && x-1 < w && c[y-1][x-1] == 1){
                            que.push(y-1); que.push(x-1);
                        }
                        //???
                        if(y-1 >= 0 && y-1 < h && x >= 0 && x < w && c[y-1][x] == 1){
                            que.push(y-1); que.push(x);
                        }
                        //??????
                        if(y-1 >= 0 && y-1 < h && x+1 >= 0 && x+1 < w && c[y-1][x+1] == 1){
                            que.push(y-1); que.push(x+1);
                        }
                        //???
                        if(y >= 0 && y < h && x-1 >= 0 && x-1 < w && c[y][x-1] == 1){
                            que.push(y); que.push(x-1);
                        }
                        //???
                        if(y >= 0 && y < h && x+1 >= 0 && x+1 < w && c[y][x+1] == 1){
                            que.push(y); que.push(x+1);
                        }
                        //??????
                        if(y+1 >= 0 && y+1 < h && x-1 >= 0 && x-1 < w && c[y+1][x-1] == 1){
                            que.push(y+1); que.push(x-1);
                        }
                        //???
                        if(y+1 >= 0 && y+1 < h && x >= 0 && x < w && c[y+1][x] == 1){
                            que.push(y+1); que.push(x);
                        }
                        //??????
                        if(y+1 >= 0 && y+1 < h && x+1 >= 0 && x+1 < w && c[y+1][x+1] == 1){
                            que.push(y+1); que.push(x+1);
                        }
                    }
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int H,W,k[100][100],n[100][100],color=1,i,j,l,m,o,r;
int main(){while(true){memset(n,0,sizeof(n));r=0;cin>>H>>W;if(H==0){break;}for(i=0;i<H;i++){for(j=0;j<W;j++){cin>>k[i][j];}}
for(i=0;i<H;i++){for(j=0;j<W;j++){if(r==1){color++;}r=0;if(k[i][j]==1 && n[i][j]==0){n[i][j]=color;for(l=0;l<100;l++){
	for(m=0;m<H;m++){for(o=0;o<W;o++){if(n[m][o]==color){
		for(int p=-1;p<=1;p++){for(int q=-1;q<=1;q++){if(m-p>=0 && m-p<H){if(o-q>=0 && o-q<W){n[m][o]=color;r=1;}}}}
	}}}
}}}}
cout<<color<<endl;}
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define MAX_W 50;
#define MAX_H 50;
ll N;
int W, H;
vector<ll> A;
vector<vector<int>> fields;
vector<vector<bool>> seen;

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

ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return b/gcd(a,b)*b;}

void dfs(int x, int y) {
  seen[y][x] = true;

  for (int i = 0; i < 8; i++) {
    if (x + dx[i] < 0 || x + dx[i] >= W || y + dy[i] < 0 || y + dy[i] >= H) continue;
    if (seen[y+dy[i]][x+dx[i]]) continue;
    if (fields[y + dy[i]][x + dx[i]]) dfs(x+dx[i], y+dy[i]);
  }
}

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

  // Start Loop
  while(true){
    cin >> W >> H;
    if (!(W || H)) break;
    fields.resize(H);
    seen.resize(H);
    rep(j, H) {
      fields[j].resize(W);
      seen[j].resize(W, false);
      rep(i, W) {
        cin >> fields[j][i]; 
      }
    }
    ll count = 0; 
    rep(j, H) {
      rep(i, W) {
        if (fields[j][i] && !seen[j][i]) {
          dfs(i, j);
          count++;
        }
      }
    }
    cout << count << endl;
    fields.clear();
    seen.clear();
  }
  // End Loop
  return 0;
}

C++: #include<iostream>
#include<vector>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;
void saiki(vector<vector<bool> >& in,int x,int y,int n,int m){
  in[x][y]=0;
  //cout<<x<<" "<<y<<endl;
  loop(i,-1,2){
    loop(j,-1,2){
      if(x+i>=0&&x+i<n&&y+j>=0&&y+j<m&&in[x+i][y+j])saiki(in,x+i,y+j,n,m);
    }
  }
}
int main(){
  int n,m;
  while(cin>>m>>n,n||m){
    int co=0;
    vector<vector<bool> >in(n);
    rep(i,n){
      rep(j,m){
	bool tmp;
	cin>>tmp;
	in[i].push_back(tmp);
      }
    }
    rep(i,n){
      rep(j,m){
	if(!in[i][j])continue;
	saiki(in,i,j,n,m);
	co++;
      }
    }
    cout<<co<<endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

void er(int c[52][52],int i,int j){
    c[i][j]--;
    if(c[i+1][j-1])
        er(c,i+1,j-1);
    else if(c[i][j+1])
        er(c,i,j+1);
    else if(c[i+1][j])
        er(c,i+1,j);
    else if(c[i+1][j+1])
        er(c,i+1,j+1);
}

int main(void){
    int w,h;
    int i,j;
    int a;
    int c[52][52];
    while(true){
        cin >> w >> h;
        if(w == 0){
            break;
        }
        for(i = 0;i < 52;i++){
            for(j = 0;j < 52;j++){
                c[i][j] = 0;
            }
        }
        for(i = 1;i <= w;i++){
            for(j = 1;j <= h;j++){
                cin >> c[i][j];
            }
        }
        a = 0;
        for(i = 1;i <= w;i++){
            for(j = 1;j <= h;j++){
                if(c[i][j]){
                    er(c,i,j);
                    a++;
                }
            }
        }
        cout << a << endl;
    }
}