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 fill(int field[50][50], int x, int y, int w, int h){
    if(field[x][y] == 1){
        field[x][y] = 0;
        if(x+1<w) fill(field, x+1, y, w, h);
        if(y+1<h) fill(field, x, y+1, w, h);
        if(x-1>=0) fill(field, x-1, y, w, h);
        if(y-1>=0) fill(field, x, y-1, w, h);
        if(x+1<w && y+1<h) fill(field, x+1, y+1, w, h);
        if(x+1<w && y-1>=0) fill(field, x+1, y-1, w, h);
        if(x-1>=0 && y+1<h) fill(field, x-1, y+1, w, h);
        if(x-1>=0 && y-1>=0) fill(field, x-1, y-1, w, h);
        return 1;
    }
    return 0;
}

int main(){
    int w, h;
    int field[50][50];
    int result;
    cin >> w >> h;
    while(w || h){
        result = 0;
        for(int i=0;i<h;++i){
            for(int j=0;j<w;++j){
                cin >> field[j][i];
            }
        }
        for(int i=0;i<h;++i){
            for(int j=0;j<w;++j){
                result += fill(field, j, i, w, h);
            }
        }
        cout << result << endl;
        cin >> w >> h;
    }
}

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

using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

int c[200][200];
int vis[200][200];
int w,h;


void dfs(int ci, int cj){
    vis[ci][cj] = true;
    int di[] = {0,1,0,-1, 1,1,-1,-1};
    int dj[] = {1,0,-1,0, -1,1,-1,1};
    rep(d,8){
        int ni = ci+di[d];
        int nj = cj+dj[d];
        if(ni < 0 || nj < 0) continue;
        if(ni >= h || nj >= w) continue;
        if(vis[ni][nj]) continue;
        if(c[ni][nj]==0) continue;
        dfs(ni,nj);
    }
}

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

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int,int> P;

int W, H;
int map[50][50], used[50][50];

void walk(int x, int y){
    used[y][x] = 1;
    REP(i, 8){
        int nx = x+dx[i], ny = y+dy[i];
        if(0 <= nx && nx < W && 0 <= ny && ny < H && !used[ny][nx] && map[ny][nx] == 1){
            walk(nx, ny);
        }
    }
}

int main(){
    while(std::cin >> W >> H, W){
        REP(i, H){
            REP(j, W){
                std::cin >> map[i][j];
            }
        }

        REP(i, H){
            REP(j, W){
                used[i][j] = 0;
            }
        }

        int island_n = 0;
        REP(i, H){
            REP(j, W){
                if(map[i][j] == 1 && !used[i][j]){
                    walk(j, i);
                    island_n++;
                }
            }
        }

        std::cout << island_n << std::endl;
    }
}

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
				
				
void del_island(int i,int j,vector<vector<int>> &v){	
	v[i][j] = 0;
	if(v[i-1][j] == 1){
		del_island(i-1,j,v);
	}
	if(v[i+1][j] == 1){
		del_island(i+1,j,v);
	}
	if(v[i][j-1] == 1){
		del_island(i,j-1,v);
	}
	if(v[i][j+1] == 1){
		del_island(i,j+1,v);
	}
	if(v[i-1][j-1] == 1){
		del_island(i-1,j-1,v);
	}
	if(v[i-1][j+1] == 1){
		del_island(i-1,j+1,v);
	}
	if(v[i+1][j-1] == 1){
		del_island(i+1,j-1,v);
	}
	if(v[i+1][j+1] == 1){
		del_island(i+1,j+1,v);
	}

}


int count_island(int w, int h,vector<vector<int>> v){
	int count = 0;
	for(int i = 1 ; i <= h ;i++){
		for(int j = 1 ;j <= w ;j++){
			//cout << v[i][j] ;
			if(v[i][j] == 1){
				count++;
				//cout << "hello" <<endl;
				del_island(i,j,v);
			}
		}
	}
	return count;
}
	
int main(){ 
	int w,h;
	vector<vector<int>> v(60,vector<int>(60,0));
	int count;

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

		for(int i = 1;i <= h; i++){
			for(int j = 1; j <= w; j++){
				cin >> v[i][j];
			}
		}

		count = count_island(w,h,v);


		cout << count << endl;
	}
}

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

int w, h;
int sima[51][51];
int move_X[9] = {-1,-1,-1,0,0,0,1,1,1};
int move_Y[9] = {-1,0,1,-1,0,1,-1,0,1};

void dfs(int y, int x){
    
    // 現在の位置を海に変える
    sima[y][x] = 0;
    
    for(int i = 0; i < 9; i++){
        int next_X = x + move_X[i];
        int next_Y = y + move_Y[i];
        
        if(sima[next_Y][next_X] == 1 && next_X >= 0 && next_Y >= 0 && next_X < w && next_Y < h)
            dfs(next_Y, next_X);
    }
    
    return ;
    
}

int main(void){
    
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0) return 0;
    
        // 島と海の入力
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> sima[i][j];
            }
        }
        
        // 島の個数
        int ans = 0;
        
        // 島の個数を調べる
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(sima[i][j] == 1){
                    dfs(i, j);
                    ans++;
                }
            }
        }
        
        cout << ans << endl;
        
    }
    
    return 0;
    
}

C++: #include <iostream>
#include <queue>
using namespace std;
#define PIAR pair<int, int>

int main(){
    int w, h;
    while(cin >> w >> h, w != 0 && h != 0){
        int field[52][52] = {{0}};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                cin >> field[i][j];
            }
        }

        queue<PIAR> st;
        int island = 0;
        int dx[] = {-1,  0, 1, 0, -1,  1, 1, -1};
        int dy[] = {0 , -1, 0, 1, -1, -1, 1,  1};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                //島を探す
                if(field[i][j] == 1){
                    st.push(PIAR(i, j));
                    field[i][j] = 0;
                    while(!st.empty()){
                    	int x = st.front().first;
                    	int y = st.front().second;
                        st.pop();
                        for(int k = 0; k < 8; k++){
                            int nextX = x + dx[k];
                            int nextY = y + dy[k];
                            if(field[nextX][nextY] == 1){
                                st.push(PIAR(nextX, nextY));
                                field[nextX][nextY] = 0;
                            }
                        }
                    }
                    island++;
                }

            }
        }

        cout << island << endl;
    }
}