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;

#define pb         push_back

const int INF = 100000000;

int w, h;
vector<int> ma[60];

void rec(int y, int x){
	int ny, nx;
	if(ma[y][x] != 1){
		return;
	}
	ma[y][x] = 0;
	for(int i=-1; i<=1; i++){
		for(int j=-1; j<=1; j++){
			if(i == 0 && j == 0) continue;
			nx =  x + i;
			ny =  y + j;
			if(ny >= 0 && ny < h && nx >= 0 && nx < w){
				rec(ny, nx);
			}
		}
	}
}
int main(void) {
	while(1){
		for(int i=0; i<60; i++){
			ma[i] = vector<int>();
		}
		cin >> w >> h;
		if(w == 0 && h == 0) break;

		for(int i = 0; i < h; i++){
			for(int j=0; j<w; j++){
				int t;
				cin >> t;
				ma[i].pb(t);
			}
		}

		int ans = 0;
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				if(ma[i][j] == 1){
					ans++;
					rec(i, j);
				}
			}
		}
		/*
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				cout << ma[i][j] << ' ';
			}
			cout << endl;
		}
		*/
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <map>
#include <queue>
#include <vector>

#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define REP(i,n) FOR(i,0,n)

using namespace std;
vector <vector<int> > seamap, visit;

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

void bfs(int x, int y){
    queue <pair <int, int> > Q;
    visit[x][y] = 1;
    Q.push(pair<int,int>(x, y));
    while(!Q.empty()){
        pair<int,int> cur = Q.front();
        Q.pop();
        REP(i,8){
            nx = cur.first + dx[i];
            ny = cur.second + dy[i];
            if(seamap[nx][ny] && !visit[nx][ny]){
                visit[nx][ny] = 1;
                Q.push(pair<int,int>(nx, ny));
            }
        }
    }
}

int main(){
    int w,h;
    while(cin >> w >> h && !(w == 0 && h == 0)){
        int island = 0;
        seamap.resize(w + 2);
        REP(i,w + 2){
            seamap[i] = vector<int>(h + 2, 0);
        }
        visit = seamap;
        FOR(y,1,h + 1){
            FOR(x,1,w + 1){
                cin >> seamap[x][y];
            }
        }
       FOR(x,1,w + 1){
            FOR(y,1,h + 1){
                if(seamap[x][y] && !visit[x][y]){
                    island++;
                    bfs(x,y);
                }
            }
        }
        cout << island << endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using IslandsMap = vector<vector<int>>;
using CheckMap = vector<vector<bool>>;

int direction_x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int direction_y[8] = {1, 1, 0, -1, -1, -1, 0, 1};

int h,w;
IslandsMap islands_map;
CheckMap check_map;

void dfs(int x, int y) {
    int dx, dy;
    check_map[y][x] = true;
    rep(i, 8) {
        dx = x + direction_x[i];
        dy = y + direction_y[i];
        if(dx >= 0 && dx < w && dy >= 0 && dy < h &&
           check_map[dy][dx] == false && islands_map[dy][dx] == 1) {
            dfs(dx, dy);
        }
    }
}

int countIslands() {
    int ans = 0;
    rep(y, h) {
        rep(x, w) {
            if(check_map[y][x] == false && islands_map[y][x] == 1) {
                dfs(x, y);
                ans++;
            }
        }
    }
    return ans;
}

int main() {
    // 制約見て！！！デカかったらlong longにすること！
    int c;
    while(true) {
        cin >> w >> h;
        if(w == 0 && h == 0)
            break;

        islands_map.resize(h);
        rep(i, h) {
            islands_map[i].resize(w);
            rep(j, w) {
                cin >> c;
                islands_map[i][j] = c;
            }
        }

        check_map.resize(h);
        rep(i, h) {
            check_map[i].resize(w);
            check_map[i].assign(w,false);
        }
        cout << countIslands() << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main(void) {
	int w,h;
	while(cin >> w >> h && w && h) {
		vector< vector<int> > data(w+2,vector<int>(h+2));
		vector< vector<bool> > check(w+2,vector<bool>(h+2,false));
		queue<int> sx,sy;
		for (int i = 0; i < h + 2; ++i) {
			for (int j = 0; j < w + 2; ++j) {
				if (i == 0 || i == h+1 || j == 0 || j == w+1) {
					data[i][j] = 0;
				}
				else {
					cin >> data[i][j];
					if (data[i][j] == 1) {
						sx.push(j);
						sy.push(i);
					}
				}
			}
		}

		int nx,ny;
		int count = 0;
		queue<int> fx,fy;
		while(!sx.empty()) {
			nx = sx.front();
			ny = sy.front();
			sx.pop();
			sy.pop();
			if (!check[ny][nx]) {
				check[ny][nx] = true;
				fx.push(nx);
				fy.push(ny);
				++count;
			}
			while (!fx.empty()) {
				nx = fx.front();
				ny = fy.front();
				fx.pop();
				fy.pop();
				if (!check[ny+1][nx] && data[ny+1][nx] == 1) {
					check[ny+1][nx] = true;
					fx.push(nx);
					fy.push(ny+1);
				}
				if (!check[ny-1][nx] && data[ny-1][nx] == 1) {
					check[ny-1][nx] = true;
					fx.push(nx);
					fy.push(ny-1);
				}
				if (!check[ny+1][nx+1] && data[ny+1][nx+1] == 1) {
					check[ny+1][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny+1);
				}
				if (!check[ny+1][nx-1] && data[ny+1][nx-1] == 1) {
					check[ny+1][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny+1);
				}
				if (!check[ny-1][nx-1] && data[ny-1][nx-1] == 1) {
					check[ny-1][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny-1);
				}
				if (!check[ny-1][nx+1] && data[ny-1][nx+1] == 1) {
					check[ny-1][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny-1);
				}
				if (!check[ny][nx+1] && data[ny][nx+1] == 1) {
					check[ny][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny);
				}
				if (!check[ny][nx-1] && data[ny][nx-1] == 1) {
					check[ny][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}