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;
typedef vector<vector<int> > VVI;
typedef vector<vector<bool> > VVB;

int w, h, cnt;

void labeling(VVI& map, VVB& visited, int x, int y) {
    visited[x][y] = true;
    for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
            if (x+i < 0 || h <= x+i || y+j < 0 || w <= y+j)
                continue;
            if (!visited[x+i][y+j] && map[x+i][y+j])
                labeling(map, visited, x+i, y+j);
        }
    }
}

int main(int argc, char *argv[])
{
    while (cin >> w >> h, w | h) {
        VVI map(h, vector<int>(w));
        VVB visited(h, vector<bool>(w, false));
        cnt = 0;
        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 (!visited[i][j] && map[i][j]) {
                    cnt++;
                    labeling(map, visited, i, j);
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,1,-1};

bool dfs(int sx,int sy,vector<vector<bool> >&c){
if(c[sx][sy]==false) return false;
c[sx][sy]=false;
for(int i=0;i<8;i++){
int nx=sx+dx[i];
int ny=sy+dy[i];
bool flag=dfs(nx,ny,c);
}
return true;
}

int main(void){
int h,w,temp;
while(1){
cin>>w>>h;
if(w==0) break;
vector<vector<bool> > c(w+2,vector<bool>(h+2,false)) ;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
cin>>temp;
if(temp==1) c[i][j]=true;
}
}
int t=0;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
bool flag=dfs(i,j,c);
if(flag) t++;
}
}
cout<<t<<endl;
}
return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define sint(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<endl
#define sset(s) cout<<#s<<":";each(i,s)cout <<" "<<i;cout<<endl

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 52;
const int dx[] = {1,1,1,0,0,-1,-1,-1};
const int dy[] = {1,0,-1,1,-1,1,0,-1};

int flag[MAX_N][MAX_N];
int w,h;

void dfs(int u,int v)
{
    flag[u][v] = 0;
    rep(i,8){
        int nu = u + dx[i],nv = v + dy[i];
        if(0 <= nu && nu < h && 0 <= nv && nv < w && flag[nu][nv]){
            dfs(nu,nv);
        }
    }
    return;
}

int main()
{
    while(1){
        cin >> w >> h;
        if(w == 0){
            break;
        }
        rep(i,h){
            rep(j,w){
                cin >> flag[i][j];
            }
        }
        int ans = 0;
        rep(i,h){
            rep(j,w){
                if(flag[i][j] != 0){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout << ans << "\n";
    }
}

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

void input(int **, int, int);
int howManyIslands(int **, int, int);
void howBigIslands(int **, bool **, int, int, int, int);

int main()
{
	int **island, w, h;
	while(true){
		cin >> w >> h;
		if(w==h && h==0) break;
		island = new int*[w];
		for(int i=0; i<w; i++)
			island[i] = new int[h];
		input(island, w, h);
		cout << howManyIslands(island, w, h) << endl;
		for(int i=0; i<w; i++)
			delete[] island[i];
		delete[] island;
	}
	return 0;
}

void input(int **island, int w, int h)
{
	char buf[60];
	cin.clear();cin.ignore();
	for(int i=0; i<h; i++){
		cin.getline(buf, sizeof(buf));
		int j=0, k=0;
		while(buf[j] != '\0'){
			if(buf[j] != ' '){
				switch(buf[j]){
				case '0':
					island[k][i] = 0;
					k++;
					break;
				case '1':
					island[k][i] = 1;
					k++;
					break;
				}
				if(w==k) break;
			}
			j++;
		}
	}
}

int howManyIslands(int **island, int w, int h)
{
	bool **check;
	int ans = 0;
	check = new bool*[w];
	for(int i=0; i<w; i++){
		check[i] = new bool[h];
		for(int j=0; j<h; j++)
			check[i][j] = false;
	}
	for(int i=0; i<w; i++){
		for(int j=0; j<h; j++){
			if(check[i][j])continue;
			if(island[i][j]){
				ans++;
				howBigIslands(island, check, i, j, w, h);
			} else {
				check[i][j] = true;
			}
		}
	}
	for(int i=0; i<w; i++)
		delete[] check[i];
	delete[] check;

	return ans;
}

void howBigIslands(int **island, bool **check, int x, int y, int w, int h)
{
	if(check[x][y]) return;
	enum __DIR{ UPPER_LEFT, UP, UPPER_RIGHT, LEFT, RIGHT, LOWER_LEFT,
		UNDER, LOWER_RIGHT, DIR_END};
	int periphe[DIR_END][2]={
			{-1, -1}, {0, -1}, {1, -1},
			{-1, 0}, {1, 0},
			{-1, 1}, {0, 1}, {1, 1}
	};
	check[x][y] = true;
	for(int i=0; i<DIR_END; i++){
		if(x+periphe[i][0] < 0 || w-1 < x+periphe[i][0] ||
			y+periphe[i][1] < 0 || h-1 < y+periphe[i][1]) continue;
		if(island[x+periphe[i][0]][y+periphe[i][1]]){
			if(!check[x+periphe[i][0]][y+periphe[i][1]])
				howBigIslands(island, check,
				x+periphe[i][0], y+periphe[i][1], w, h);
		}
	}
}

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

void dfs(vector<vector<int>>& m, int r, int c)
{
    int rows = m.size();
    int cols = m[0].size();

    m[r][c] = 0;
    int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
    int dy[] = {0, -1, -1, -1, 0, 1, 1, 1};
    for (int i = 0; i < 8; i++) {
        int nr = r + dy[i];
        int nc = c + dx[i];
        if (0 <= nr && nr < rows && 0 <= nc && nc < cols && m[nr][nc] == 1) {
            dfs(m, nr, nc);
        }
    }
}

int main()
{
    int cols, rows;
    while (true) {
        cin >> cols >> rows;
        if (cols == 0 && rows == 0) return 0;
        vector<vector<int>> m(rows, vector<int>(cols));
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                cin >> m[i][j];
            }
        }
        int ans = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (m[i][j] == 1) {
                    dfs(m, i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){d[x][y]=0;f(8)k(x+u[i],y+u[i+8]);}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[0][i]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	printf("%d\n",a);
	main();
}