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

int w, h, ans, tmp;
vector<vector<int>> c(60, vector<int>(60,0));

void ume(int x, int y){
	if(c[x][y]==0) return;
	c[x][y]=0;
	ume(x-1,y);
	ume(x+1,y);
	ume(x-1,y-1);
	ume(x,y-1);
	ume(x+1,y-1);
	ume(x-1,y+1);
	ume(x,y+1);
	ume(x+1,y+1);
	return;
}

bool hantei(int x, int y){
	if(c[x][y]==1){
		ume(x,y);
		return 1;
	}
	else return 0;
}

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

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <cstdio>
#include <string.h>
#include <cstring>
using namespace std;

int w,h;
int land[50][50];
int isLands;
int d1[] = {0,0,-1,1,-1,1,-1,1};
int d2[] = {-1,1,0,0,1,1,-1,-1};

void dfs(int i, int j,int h, int w){
    
    if(i >= h || j >= w) return;
    if(i < 0 || j < 0) return;
    if(land[i][j] == 0) return;
    if(land[i][j] == 1) {
        land[i][j] = 0;
        for(int k = 0; k < 8; k++){
            dfs(i + d1[k], j + d2[k], h , w);
        }
    }

}

int main() {
    while(true){
        cin >> w >> h;
        if(w == 0 || h == 0) break;
        isLands = 0;
        for(int i = 0;i < h; i++){
            for(int j = 0; j< w; j++){
                cin >> land[i][j];
            }
        }
        for(int i = 0;i < h; i++){
            for(int j = 0; j< w; j++){
                if(land[i][j] == 1){
                    isLands++;
                    dfs(i,j,h,w);
                }
            }
        }
        cout << isLands << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>

using namespace std;

int res, land[1080][1080];
int xs[] = {1, 1, 0, -1, -1, -1, 0, 1};
int ys[] = {0, 1, 1, 1, 0, -1, -1, -1};
void dfs(int x, int y){
	land[x][y] = 0;
	for(int i = 0;i < 8;i++){
		int tx = x + xs[i];
		int ty = y + ys[i];
		if(land[tx][ty])dfs(tx, ty);
	}
}

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

C++: #include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9+7;
using Graph = vector<vector<ll>>;
using pll = pair<ll, ll>;
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;}

// 0 false, 1 true 
// string to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

ll w, h; 
vector<pll> direction = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
//vector<vector<ll>> vec(h, vector<ll>(w));
//vector<vector<bool>> seen(h, vector<bool>(w));

void dfs(vector<vector<ll>> &vec, vector<vector<bool>> &seen, ll y, ll x){
    seen[y][x] = true;

    /*
    cout << "---" << endl;
    rep(i, h){
        rep(j, w){
            cout << vec[i][j] << " ";
        }
        br;
    }
    cout << "---" << endl;
    */

    if(vec[y][x] == 1){
        for(ll i = 0; i < direction.size(); i++){
            ll next_y = y + direction[i].first;
            ll next_x = x + direction[i].second;

            //場外なら終わり
            if(next_y < 0 || next_y >= h){
                continue;
            }
            if(next_x < 0 || next_x >= w){
                continue;
            }

            if(seen[next_y][next_x] == false && vec[next_y][next_x] == 1){
                dfs(vec, seen, next_y, next_x);
            }
        }
    }
}

int main() {
    std::cout << std::fixed << std::setprecision(15);

    vector<ll> res;

    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0){
            break;
        }
        vector<vector<ll>> vec(h, vector<ll>(w));
        vector<vector<bool>> seen(h, vector<bool>(w));
        vec.resize(h, vector<ll>(w, 0));
        seen.resize(h, vector<bool>(w, false));
        
        rep(i, h){
            rep(j, w){
                cin >> vec[i][j];
            }
        }

        ll ans = 0;
        rep(i, h){
            rep(j, w){
                if(vec[i][j] == 1 && seen[i][j] == false){
                    ans++;
                    dfs(vec, seen, i, j);
                }
            }
        }
        res.push_back(ans);

    }

    rep(i, res.size()){
        cout << res[i] << endl;
    }

}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int> P;

int di[] = { 1, 1, 1, 0, 0,-1,-1,-1};
int dj[] = { 1, 0,-1, 1,-1, 1, 0,-1};


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

    int w,h;
    while(cin >> w >> h){
        if(!w && !h) break;

        int island[h][w];
        rep(i,h) rep(j,w) cin >> island[i][j];

        stack<P> stack;

        int ans = 0;
        rep(i,h) rep(j,w) {
            if(island[i][j] == 0) continue;
            ans++;
            stack.emplace(i,j);
            while(!stack.empty()){
                P p = stack.top(); stack.pop();
                int i = p.first, j = p.second;
                island[i][j] = 0;
                rep(dir,8){
                    int ni = i + di[dir];
                    int nj = j + dj[dir];
                    if(ni<0||ni>=h||nj<0||nj>=w) continue;
                    if(island[ni][nj] == 0) continue;
                    stack.emplace(ni,nj);
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}