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;

const int N = 55;
int C[N][N];
bool seen[N][N];

int h, w;

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

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

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

    while(cin >> w >> h){
        if(h == 0 && w == 0){
            break;
        }

        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> C[i][j];
                seen[i][j] = false;
            }
        }

        int ans = 0;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(C[i][j] == 0 || seen[i][j]) continue;
                ans++;
                dfs(i, j);
            }
        }

        cout << ans << endl;
    }

    return 0;
}

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

#define ll long long int
#define rep(i,x,y) for(int i=x;i<y;i++)
#define rel(i,x,y) for(int i=x-1;i>=y;i--)
#define all(x) x.begin(),x.end()
#define NMAX 50

using namespace std;
bool MAP[NMAX][NMAX];
bool alr[NMAX][NMAX];

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

void DFS(int y,int x){
	alr[y][x] = true;
	rep(i,0,9){
		if(0<=y+dy[i/3] && y+dy[i/3]<h && 0<=x+dx[i%3] && x+dx[i%3]<w && MAP[y+dy[i/3]][x+dx[i%3]] && !alr[y+dy[i/3]][x+dx[i%3]]){
			DFS(y+dy[i/3],x+dx[i%3]);
		}
	}
}

int main(){
	while(true){
	int count=0;
	cin >> w >> h;
	if(w == 0 && h == 0){ break; }
	rep(i,0,h){
		rep(j,0,w){
			alr[i][j] = false;
			cin >> MAP[i][j];
		}
	}
	rep(i,0,h){
		rep(j,0,w){
			if(MAP[i][j] && !alr[i][j]){
				DFS(i,j);
				count++;
			}
		}
	}
	cout << count << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<ll,ll> Pi;
const int INF=(ll)(1LL<<31)-1;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
//int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int w,h;
int ls[55][55];
int alr[55][55];

void dfs(int a,int b){
	alr[a][b]=1;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(i!=0||j!=0){
				if(a+i>=0&&a+i<h&&b+j>=0&&b+j<w){
					if(alr[a+i][b+j]==0&&ls[a+i][b+j]==1){
						dfs(a+i,b+j);
					}
				}
			}
		}
	}
	return;
}

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

C++: #include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(),v.end()
#define _GLIBCXX_DEBUG

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vd = vector<double>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vvd = vector<vd>;
using vvvi = vector<vvi>;
using vvvll = vector<vvll>;
using vvvd = vector<vvd>;

const double pi=acos(-1);
const ll MOD=1e9+7;
const ll INF=(1LL<<60);

int w,h;
int graph[51][51];

void dfs(int x,int y,int x1,int y1){
    graph[x][y]=0;
    vi move={-1,0,1};
    for (int i:move){
        for (int j:move){
            if (i==0 && j==0)continue;
            else if (x+i<=0 || x+i>h || y+j<=0 || y+j>w)continue;
            else if (x+i==x1 && y+j==y1)continue;
            else if (graph[x+i][y+j]==0)continue;
            else dfs(x+i,y+j,x,y);
        }
    }
}

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

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0; i<(n); i++)
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; }

void dfs(int i, int j, vector<vector<int>> &map){
    if(map.at(i).at(j)==0) return;
    map.at(i).at(j)=0;
    vector<int> x={-1,0,1,-1,1,-1,0,1},y={-1,-1,-1,0,0,1,1,1};
    rep(k,8){
        dfs(i+x.at(k),j+y.at(k),map);
    }
}

int main(){
    while(true){
        int w,h; cin>>w>>h;
        int res=0;
        if(w==0 && h==0) break;
        vector<vector<int>> map(h+2,vector<int>(w+2,0));
        rep(i,h){
            rep(j,w){
                cin>>map.at(i+1).at(j+1);
            }
        }
        rep(i,h+2){
            rep(j,w+2){
                if(map.at(i).at(j)==1){
                    dfs(i,j,map);
                    res++;
                }
            }
        }
        cout<<res<<endl;
    }
}