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 <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;

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

void dfs(VVI &table, int i, int j) {
    table[i][j] = 0;
    for (int loop = 0; loop < 8; loop++) {
        int new_i = i + dy[loop];
        int new_j = j + dx[loop];
        if (table[new_i][new_j] == 1) {
            dfs(table, new_i, new_j);
        }
    }
    return;
}

int main(void) {
    while(true) {
        int w;
        int h;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }
        VVI table(h + 2, VI(w + 2, 0));
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                cin >> table[i][j];
            }
        }
        int number = 0;
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                if (table[i][j] == 1) {
                    dfs(table, i, j);
                    number++;
                }
            }
        }
        cout << number << endl;
    }
    return 0;
}

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

int main(){
	int w, h;
	while( cin >> w >> h && w && h ){
		int field[52][52];
		stack< pair<int, int> > st;
		for( int i = 1; i <= h; i++ ){
			for( int j = 1; j <= w; j++ ){
				cin >> field[i][j];
			}
		}
		int sum = 0;
		for( int i = 1; i <= h; i++ ){
			for( int j = 1; j <= w; j++ ){
				if( field[i][j] == 1 ){
					sum++;
					field[i][j] = 0;
					st.push( pair<int, int>(i, j) );
					while( !st.empty() ){
						int x0 = st.top().second;
						int y0 = st.top().first;
						st.pop();
						for( int k = 0; k < 8; k++ ){
							int x = x0 + dx[k];
							int y = y0 + dy[k];
							if( field[y][x] == 1 ){
								st.push( pair<int, int>(y, x) );
								field[y][x] = 0;
							}
						}
					}
				}
			}
		}
		cout << sum << endl;
	}
}

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

#define pb         push_back

const int INF = 1000000000;
const int MOD = 1000000007;

int ans;
int ma[100][100];
int w, h;

void rec(int y, int x);

int main(void) {
	while(1){
		ans = 0;
		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][j] = t;
			}
		}

		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				if(ma[i][j] == 1){
					ans++;
					rec(i, j);
				}
			}
		}
		printf("%d\n", ans);

	}
	
	return 0;
}

void rec(int y, int x){
	for(int i=-1; i<=1; i++){
		for(int j=-1; j<=1; j++){
			if(i == 0 && j == 0) continue;

			int ny = y+i;
			int nx = x+j;
			if(ny >= 0 && ny < h && nx >= 0 && nx < w && ma[ny][nx] == 1){
				ma[ny][nx] = 0;
				rec(ny, nx);
			}
		}
	}
}

C++: #ifdef BUILT_LOCAL

#include "niklib.h"

#endif

#include <iostream>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <cstdio>
#include <map>
#include <set>

using namespace std;

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

void rec(int y, int x) {
    if (y < 0 || x < 0 || y >= h || x >= w) return;
    if (c[y][x] == 0)
        return;
    c[y][x] = 0;
    for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
            rec(y + i, x + j);
        }
    }
}

int main() {
    while (1) {
        scanf("%d %d", &w, &h);
        if (w == 0 && h == 0) break;
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++) {
                scanf("%d", &c[i][j]);
            }
        int r = 0;
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++) {
                if (c[i][j] == 0) continue;
                r++;
                rec(i, j);
            }
        printf("%d\n", r);
    }
}

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

bool valid(int x,int y,int W,int H)
{
    return (x>=0&&x<W)&&(y>=0&&y<H);
}
int main()
{
    int W,H,i,j,k,x,y;
    queue<int> Q;
    cin >> H >> W;
    while(W && H)
    {
        int num=0;
        int A[W][H];
        for(i=0;i<W;i++)
            for(j=0;j<H;j++)
                cin >> A[i][j];
        for(i=0;i<W;i++)
            for(j=0;j<H;j++)
            {
                if(A[i][j]==1)
                {
                    Q.push(i);
                    Q.push(j);
                    A[i][j]=0;
                    num++;
                    while(!Q.empty())
                    {
                        x=Q.front();
                        Q.pop();
                        y=Q.front();
                        Q.pop();
                        for(k=0;k<8;k++)
                        {
                            if(valid(x+dx[k],y+dy[k],W,H))
                            {
                                if(A[x+dx[k]][y+dy[k]]==1)
                                {
                                    Q.push(x+dx[k]);
                                    Q.push(y+dy[k]);
                                    A[x+dx[k]][y+dy[k]]=0;
                                }
                            }
                        }
                    }
                }
            }
        cout << num << endl;
        cin >> H >> W;
    }
    return 0;
}

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

void	dfs(int w, int h, int grid[50][50], bool seen[50][50],
			int dx[], int dy[], int x, int y)
{
	seen[y][x] = true;
	int	nx, ny;
	for (int i = 0; i < 8; ++i) {
		nx = x + dx[i];
		ny = y + dy[i];
		if (nx < 0 || w <= nx || ny < 0 || h <= ny)
			continue;
		if (grid[ny][nx] == 0)
			continue;
		if (seen[ny][nx])
			continue;
		dfs(w, h, grid, seen, dx, dy, nx, ny);
	}
}

int	main()
{
	int		w, h;
	int		grid[50][50];
	bool	seen[50][50];
	int		dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
	int		dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
	while (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 >> grid[i][j];
				seen[i][j] = false;
			}
		}
		int	num_island = 0;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				if (grid[i][j] == 0)
					continue;
				if (seen[i][j])
					continue;
				++num_island;
				dfs(w, h, grid, seen, dx, dy, j, i);
			}
		}
		cout << num_island << endl;
	}
}