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;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int w,h;
int nx,ny;
void dfs(vector<vector<int>> &G,int a,int b){
    G.at(a).at(b)=0;
    
    for(int k=-1;k<=1;k++){
        for(int l=-1;l<=1;l++){
            nx=a+k;
            ny=b+l;
        if(0<=nx&& nx<h && 0<=ny&& ny<w && G.at(nx).at(ny)==1) dfs(G,nx,ny);
        }
    }
    return ;
}



int main(){
    int w,h;
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                dfs(G,i,j);
                count++;
            }
        }
    }
cout<<count<<endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
signed main(){
  int w,h;
  while(cin>>w>>h,w||h){
    int c[h][w];
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	cin>>c[i][j];
    int ans=0;
    int ax[]={-1,0,1,-1,1,-1,0,1};
    int ay[]={-1,-1,-1,0,0,1,1,1};
    int used[h][w];
    memset(used,0,sizeof(used));
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(used[i][j]) continue;
	if(!c[i][j]) continue;
	ans++;
	queue<P> q;
	q.push(P(i,j));
	while(!q.empty()){
	  P p=q.front();q.pop();
	  int y=p.first,x=p.second;
	  //cout<<y<<" "<<x<<endl;
	  if(used[y][x]) continue;
	  if(!c[y][x]) continue;
	  used[y][x]=1;
	  for(int k=0;k<8;k++){
	    int ny=y+ay[k],nx=x+ax[k];
	    if(ny<0||ny>=h||nx<0||nx>=w) continue;
	    if(used[ny][nx]) continue;
	    if(!c[ny][nx]) continue;
	    q.push(P(ny,nx));
	  }
	}
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

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

#define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
//constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

struct Edge {
    ll to, rev;
    long double cap;
    Edge(ll _to, long double _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag,
              long double revCap) {
    G[from].push_back(Edge(to, cap, (ll)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void solve() {
    int w, h;
    while(cin >> w >> h) {
        if(w == 0 and h == 0)
            break;
        Matrix c(h, Array(w));

        REP(i, h) {
            REP(j, w) {
                cin >> c[i][j];
            }
        }

        Matrix used(h, Array(w, false));

        auto dfs = [&](auto &&self, int i, int j) -> ll {
            used[i][j] = true;
            ll ret = 1;
            for(int ni = max(i - 1, 0); ni <= min(i + 1, h - 1); ni++) {
                for(int nj = max(j - 1, 0); nj <= min(j + 1, w - 1); nj++) {
                    if(used[ni][nj])
                        continue;
                    if(c[ni][nj])
                        ret += self(self, ni, nj) + 1;
                }
            }
            return ret;
        };

        ll ans = 0;
        REP(i, h) {
            REP(j, w) {
                if(!c[i][j] or used[i][j])
                    continue;
                dfs(dfs, i, j);
                ans++;
            }
        }
        cout << ans << en;
    }
}

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

    solve();

    /*
    ll t;
    cin >> t;
    REP(i, t) { solve();}
    */

    return 0;
}

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

const int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int w, h;
int c[50][50];

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

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

C++: #include<bits/stdc++.h>
using namespace std;
#define rp(i,j) for(long long i=0;i<j;i++)
#define rep(i,j,k) for(long long i=j;i<k;i++)
#define mod 1000000007
#define NIL -1
#define MAX 50
const int INFTY=(1<<21);
typedef long long ll;
int H,W;
int c[MAX][MAX];
int checked[MAX][MAX]={0};
void dfs(int x,int y){
    if(x<0||x>=H||y<0||y>=W||c[x][y]==0)return;
    if(checked[x][y]==1)return;
    checked[x][y]=1;
    rep(i,-1,2){
        rep(j,-1,2){
            dfs(x+i,y+j);
        }
    }
}
int main(){
    while(1){
        int ans=0;
        cin>>W>>H;
        if(H==0&&W==0)break;
        else {
            rp(i,H){
                rp(j,W){
                    cin>>c[i][j];
                    checked[i][j]=0;
                }
            }
            rp(i,H){
                rp(j,W){
                    if(c[i][j]==1&&checked[i][j]==0){
                        dfs(i,j);
                        ans++;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
}