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;

int H, W;
vector<vector<int>> field;

void dfs(int h, int w) {
  field[h][w] = 0;

  for (int dh = -1; dh <= 1; dh++) {
    for (int dw = -1; dw <= 1; dw++) {
      int nh = h + dh, nw = w + dw;

      if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
      if (field[nh][nw] == 0) continue;

      dfs(nh, nw);
    }
  }
}

int main() {
  while (cin >> W >> H) {
    if (H == 0 || W == 0) break;
    field.assign(H, vector<int>(W, 0));
    for (int h = 0; h < H; h++) {
      for (int w = 0; w < W; w++) {
        cin >> field[h][w];
      }
    }
    int count = 0;
    for (int h = 0; h < H; h++) {
      for (int w = 0; w < W; w++) {
        if (field[h][w] == 0) continue;
        dfs(h, w);
        count++;
      }
    }

    cout << count << endl;
  }
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)
//#define INF (1LL<<59)

vector<vector<int>> v;

int w,h;
void dfs(int y, int x, vector<vector<bool>> &used){
    for(int dy=-1;dy<=1;dy++){
        for(int dx=-1;dx<=1;dx++){
            if(dy==0 && dx==0)continue;
            int ddy = y+dy, ddx = x+dx;
            if(ddx<0 || ddy<0 || ddx>=w || ddy>=h)continue;
            if(v[ddy][ddx]!=1 || used[ddy][ddx]==true)continue;
            used[ddy][ddx] = true;
            dfs(ddy,ddx,used);
        }
    }
}

int main(){
    while(cin>>w>>h&&(w||h)){
        
        v.clear();
        v.resize(h,vector<int>(w));
        rep(i,h)rep(j,w)cin>>v[i][j];
        
        int ans = 0;
        vector<vector<bool>> used(h,vector<bool>(w,false));
        rep(i,h){
            rep(j,w){
                if(used[i][j]==false && v[i][j]==1){
                    used[i][j] = true;
                    dfs(i,j,used);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
}

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

constexpr int MAX_H = 55;
constexpr int MAX_W = 55;

int H, W;
int grid[MAX_H][MAX_W];
bool reached[MAX_H][MAX_W];

int dh[8] = {-1, -1, -1,  0,  0,  1,  1,  1};
int dw[8] = {-1,  0,  1, -1,  1, -1,  0,  1};

void dfs(int h, int w) {
  reached[h][w] = true;
  for (int i{}; i < 8; ++i) {
    int nh = h + dh[i];
    int nw = w + dw[i];
    if (nh < 0 || nh >= H || nw < 0 || nw >= W ||
        reached[nh][nw] || grid[nh][nw] == 0)
      continue;
    dfs(nh, nw);
  }
}

int main() {
  while (true) {
    cin >> H >> W;
    if (H == 0 && W == 0)
      break;
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        reached[h][w] = false;
        cin >> grid[h][w];
      }
    }

    int island{};
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        if (grid[h][w] == 0 || reached[h][w])
          continue;
        dfs(h, w);
        ++island;
      }
    }

    cout << island << endl;
  }

  return 0;
}

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

int myMap[50][50];
int w, h;
int ans;

bool safe(int x, int y){
    return (0 <= x && 0 <= y) && (x < w && y < h);
}

void del(int x, int y){
    myMap[x][y] = 0;
    if (safe(x+1, y) && myMap[x+1][y]) del(x+1, y);
    if (safe(x+1, y+1) && myMap[x+1][y+1]) del(x+1, y+1);
    if (safe(x+1, y-1) && myMap[x+1][y-1]) del(x+1, y-1);
    if (safe(x, y+1) && myMap[x][y+1]) del(x, y+1);
    if (safe(x, y-1) && myMap[x][y-1]) del(x, y-1);
    if (safe(x-1, y-1) && myMap[x-1][y-1]) del(x-1, y-1);
    if (safe(x-1, y+1) && myMap[x-1][y+1]) del(x-1, y+1);
    if (safe(x-1, y) && myMap[x-1][y]) del(x-1, y);
    return;
}

int main(){

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

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

	ans = 0;	
	for (int i = 0; i < w; i++){
	    for (int j = 0; j < h; j++){
		if (myMap[i][j] == 1) {
		    ans++;
		    del(i, j);
		}
	    }
	}

	cout << ans << endl;
    }
    
}

C++: #include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>

using namespace std;

#define INF 999999999

int map[52][52];
bool ch[52][52];
int ans;

void dfs(int map[52][52], bool c[52][52], int x, int y){
	//cout << x << " " << y << " " << ans << endl;
	c[x][y] = 1;
	if(map[x-1][y]==1 && c[x-1][y]==0) dfs(map,c,x-1,y);
	if(map[x-1][y-1]==1 && c[x-1][y-1]==0) dfs(map,c,x-1,y-1);
	if(map[x-1][y+1]==1 && c[x-1][y+1]==0) dfs(map,c,x-1,y+1);
	if(map[x+1][y]==1 && c[x+1][y]==0) dfs(map,c,x+1,y);
	if(map[x-1][y-1]==1 && c[x-1][y-1]==0) dfs(map,c,x-1,y-1);
	if(map[x-1][y+1]==1 && c[x-1][y+1]==0) dfs(map,c,x-1,y+1);
	if(map[x][y-1]==1 && c[x][y-1]==0) dfs(map,c,x,y-1);
	if(map[x][y+1]==1 && c[x][y+1]==0) dfs(map,c,x,y+1);
}

int main(){
	int i, j, w, h;
	while(1){
		cin >> w >> h;
		if(!w) break;
		for(i=0;i<=h+1;i++){
			for(j=0;j<=w+1;j++){
				if(i==0||i==h+1||j==0||j==w+1)map[i][j]=0;
				else cin >> map[i][j];
				ch[i][j] = 0;
			}
		}
		ans = 0;
		for(i=1;i<=h;i++){
			for(j=1;j<=w;j++){
				if(map[i][j]&&!ch[i][j]){
					dfs(map,ch,i,j);
					ans++;
				}
			}
		}
		
		cout << ans << endl;
	}
	return 0;
}

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

void dfs(int px,int py){
  G[px][py] = 0;
  for(int i = 0 ; i < 8 ; i++ ){
    int nx = px + dx[i],ny = py + dy[i];
    if(0 <= nx && nx < w && 0 <= ny && ny < h && G[nx][ny] == 1){
      dfs(nx,ny);
    }
  }
}

int main(){
  while( cin >> w >> h,w+h){

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

    int cnt = 0;
    for(int i = 0 ; i < h ; i++){
      for(int j = 0 ; j < w ; j++){
	if(G[j][i] == 1){
	  dfs(j,i);
	  cnt++;
	}
      }
    }
    cout << cnt << endl;
  }
  return 0;
}