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>
using namespace std;

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

void solve(int x,int y){
	if(map[y][x] == 1){
		map[y][x] = 0;
		for(int i=-1;i<2;i++){
			for(int j=-1;j<2;j++){
				if(i==0&&j==0){
					continue;
				}
				if(y+i >= 0 && y+i < h && x+i >= 0 && x+i <= w){
					solve(x+j,y+i);
				}
			}
		}
	}
}

int main(){
	while(true){
		int count=0;

		cin >> w >> h;
		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){
					solve(j,i);
					count++;
				}
			}
		}

		cout << count << endl;
	}
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#define EPS 1e-10
using namespace std;
typedef long long llong;
vector<vector<char> >v;
int cnt;
int w, h;

bool dfs(int x, int y) {

	if (x < 0 || h <= x)return false;
	if (y < 0 || w <= y)return false;

	if (v[x][y] == '1') {
		v[x][y] = '0';
		dfs(x - 1, y - 1);
		dfs(x - 1, y);
		dfs(x - 1, y + 1);
		dfs(x, y - 1);
		dfs(x, y + 1);
		dfs(x + 1, y - 1);
		dfs(x + 1, y);
		dfs(x + 1, y + 1);
		return true;
	}

	return false;

}


int main() {
	while (cin >> w >> h, h) {
		v = vector<vector<char> >(h, vector<char>(w));
		cnt = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
			cin >> v[i][j];

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

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define MOD 1000000007
#define INF 1e9
#define INFL 1e16
 
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
template<class T>using arr=vector<vector<T>>;
template<class T>void pr(T x){cout << x << endl;}
template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){cout << a[i] << " ";} cout << a[a.size()-1] << endl;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
 
arr<int> land;
int ans;
int h, w;

void dfs(int x, int y){
    land[x][y] = 0;
    FOR(i, -1, 2){
        FOR(j, -1, 2){
            if(i==0 && j==0) continue;
            if(x+i < 0 || x+i >= h || y+j < 0 || y+j >= w) continue;
            if(land[x+i][y+j]==1){
                dfs(x+i, y+j);
            }
        }
    }
}

int main()
{
    while(1){
        cin >> w >> h;
        if(w==0 && h==0) break;
        land.resize(h);
        rep(i, h) land[i].resize(w);

        rep(i, h) rep(j, w) cin >> land[i][j];
        ans = 0;
        rep(i, h) rep(j, w){
            if(land[i][j]==0) continue;
            ans++;
            dfs(i, j);
        }
        pr(ans);
    }
    return 0;}

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

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(int i = 0; i < (n); i++)
#define Rep(i, n) for(int i = 1; i < n; i++)

const int mod = 1000000007;
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vii = vector<pair<int, int>>;

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; }

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return (a * b) / GCD(a, b); }
int round_int(int a, int b) { return (a + (b - 1)) / b; }

// 素因数分解; 返り値は(素数, 乗数)のvector配列
vector<pair<ll, int>> factorize(ll n) {
	vector<pair<ll, int>> res;
	for (ll i = 2; i * i <= n; ++i) {
		if (n % i) continue;
		res.emplace_back(i, 0);
		while (n % i == 0) {
			n /= i;
			res.back().second++;
		}
	}
	if (n != 1) res.emplace_back(n, 1);
	return res;
}

constexpr array<int, 9> dx = { 0, 1, 0, -1, -1, 1, 1, -1, 0 };
constexpr array<int, 9> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0 };
constexpr auto MAX = 100000;

// ──────────────────────────────────────────────────────────────────

// UnionFind 解説：https://youtu.be/TdR816rqc3s?t=8424

int w, h, t[50][50], cnt = 0;
bool check[50][50];

void dfs(ii pos) {
	int y = pos.first;
	int x = pos.second;

	rep(i, 8) {
		int nx = x + dx[i], ny = y + dy[i];

		if (0 <= nx && nx < w &&
			0 <= ny && ny < h &&
			check[ny][nx] == false &&
			t[ny][nx] == 1) {
			check[ny][nx] = true;
			dfs({ ny,nx });
		}
	}
}

int main() {
	vi ans;
	while (true) {
		cin >> w >> h;

		if (!w && !h) break;

		cnt = 0;
		rep(i, 50) rep(j, 50) check[i][j] = false;
		rep(i, h) rep(j, w) cin >> t[i][j];

		rep(i, h) {
			rep(j, w) {
				if (t[i][j] == 1 && check[i][j] == false) {
					check[i][j] = true;
					dfs({ i, j });
					cnt++;
				}
			}
		}
		ans.push_back(cnt);
	}
	rep(i, ans.size()) cout << ans[i] << endl;
}

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


int H,W;
vector<vector<int>> field;

void dfs(int h, int w){
	field[h][w] = 0;
	for(int dh=-1;dh<=1;dh++){
		for(int dw=-1;dw<=1;dw++){
			int nh=h+dh, nw=w+dw;
			if(nh<0||nw<0||H-1<nh||W-1<nw) continue;
			if(field[nh][nw] == 0) continue;
			
			dfs(nh, nw);
		}
	}	
}

int main()
{
	while(1){
		cin>>W>>H;
		if(W==0&&H==0) break;
		field.assign(H, vector<int>(W, 0));
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>field[i][j];
			}
		}
		
		int cnt = 0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(field[i][j] == 0) continue;
				dfs(i, j);
				cnt++;
			}
		}
		cout<<cnt<<endl;
	}
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define MAX 50

int c[MAX][MAX];

void dfs(int w, int h, int x, int y);
int main(int argc, char const *argv[]){
    int w, h, ans;

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

    return 0;
}

void dfs(int w, int h, int x, int y){
    int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
    int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
    c[x][y] = 0;

    for (int i = 0; i < 8; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];

        if (0 <= nx && nx < w && 0 <= ny && ny < h && c[nx][ny] == 1){
            dfs(w, h, nx, ny);
        }
    }
    return;
}