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;
using ll = long long;
constexpr ll MOD = 1000000007;
constexpr ll INF = 1ll<<60;

ll field[60][60];
bool f[60][60];
ll W, H;

void dfs(ll h, ll w)
{
	if (f[h][w]) return;
	f[h][w] = true;
	for (ll i = -1; i <= 1; ++i)
		for (ll j = -1; j <= 1; ++j)
		{
			ll nw = w + i;
			ll nh = h + j;
			if (nw < 0 || nw > W || nh < 0 || nh > H) continue;
			if (field[nh][nw])
				dfs(nh, nw);
		}
}

int main(int argc, char **argv)
{
	while (cin >> W >> H, true)
	{
		if (W == 0 && H == 0) break;
		memset(f, 0, sizeof f);
		memset(field, 0, sizeof field);

		vector<pair<ll, ll>> search;
		search.reserve(50*50);
		for (ll h = 0; h < H; ++h)
			for (ll w = 0; w < W; ++w)
			{
				cin >> field[h][w];
				if (field[h][w] == 1)
				{
					search.push_back(pair<ll, ll>(h, w));
					// std::cout << "(h, w) : " << h << ", " << w << std::endl;
				}
			}
		
		ll res{0};
		for (ll i = 0; i < (ll)search.size(); ++i)
		{
			if (!f[search[i].first][search[i].second])
			{
				// std::cout << "(h, w) : " << search[i].first << ", " << search[i].second << std::endl;
				dfs(search[i].first, search[i].second);
				++res;
			}
		}
		std::cout << res << std::endl;

	}
}

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

int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,1,-1};
bool seen[50][50];
int G[50][50];

void dfs(int x,int y,int h,int w){
    seen[x][y]=true;

    rep(i,8){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<0||nx>h-1||ny<0||ny>w-1)continue;
        if(seen[nx][ny])continue;
        if(G[nx][ny]==0)continue;
        dfs(nx,ny,h,w);
    }
}

int main(){
    while(1){
        int h,w;
        int ans=0;
        cin>>w>>h;
        if(w==0&&h==0)break;
        rep(i,h)rep(j,w)cin>>G[i][j];
        rep(i,50)rep(j,50)seen[i][j]=false;
        rep(i,h)rep(j,w){
            if(seen[i][j]==false&&G[i][j]==1){
                ans++;
                dfs(i,j,h,w);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

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

int islands=0;
int w,h;
int field[49][49];
void bfs(int, int);

int main(){

  while(cin>>w>>h&&w!=0&&h!=0){

    for(int i=0;i<50;i++){
      for(int j=0;j<50;j++){
        field[i][j] = 0;
      }
    }

    for(int j=0;j<h;j++){
      for(int i=0;i<w;i++){
        cin >> field[i][j];
//        cout << field[i][j] << " ";
      }
  //    cout << endl;
    }
   // cout << endl;
    int ans=0; 

    for(int i=0;i<w;i++){
      for(int j=0;j<h;j++){
        if(field[i][j]==1){
          field[i][j]=0;
          bfs(i,j);
          ans++;
        }
      }
    }
    /*   if(w==5&&h==4){
         for(int i=0;i<w;i++){
         for(int j=0;j<h;j++){
         cout << field[i][j] << " ";
         }
         cout << endl;
         }
         }*/
    cout << ans << endl;
  }

  return 0;
}

void bfs(int x, int y){
  if(x-1>=0&&field[x-1][y]==1){
    field[x-1][y]=0;
    bfs(x-1,y);
  }
  if(x+1<w&&field[x+1][y]==1){
    field[x+1][y]=0;
    bfs(x+1,y);
  }
  if(y-1>=0&&field[x][y-1]==1){
    field[x][y-1]=0;
    bfs(x,y-1);
  }
  if(y+1<h&&field[x][y+1]==1){
    field[x][y+1]=0;
    bfs(x, y+1);
  }
  if(x-1>=0&&y-1>=0&&field[x-1][y-1]==1){
    field[x-1][y-1]=0;
    bfs(x-1,y-1);
  }
  if(x+1<w&&y-1>=0&&field[x+1][y-1]==1){
    field[x+1][y-1]=0;
    bfs(x+1,y-1);
  }
  if(x-1>=0&&y+1<h&&field[x-1][y+1]==1){
    field[x-1][y+1]=0;
    bfs(x-1,y+1);
  }
  if(x+1<w&&y+1<h&&field[x+1][y+1]==1){
    field[x+1][y+1]=0;
    bfs(x+1,y+1);
  }
  return;
}

C++: #include <bits/stdc++.h>

#define ll long long

using namespace std;

int w,h;

int f[100][100]={};
int cf[100][100] = {};



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",&f[i][j]);
            }
        }
        int ans = 0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                
                if(f[i][j] == 1 && cf[i][j]==0){
                    ans++;
                }
                for(int k=-1;k<2;k++){
                    if(i+k<0 || h <= i+k)continue;
                    for(int t=-1;t<2;t++){
                        if(j+t<0 || w <= j+t)continue;
                        if(f[i][j]==1&&f[i+k][j+t]==1 && cf[i+k][j+t]==0){
                            cf[i+k][j+t]=1;
                        }
                    }
                }
            }
        }
        cout << ans << 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;
				
				
vector<vector<int>> del_island(int i,int j,vector<vector<int>> v){	
	v[i][j] = 0;
	vector<vector<int>> answer = v;
	if(v[i-1][j] == 1){
		answer = del_island(i-1,j,answer);
	}
	if(v[i+1][j] == 1){
		answer = del_island(i+1,j,answer);
	}
	if(v[i][j-1] == 1){
		answer = del_island(i,j-1,answer);
	}
	if(v[i][j+1] == 1){
		answer = del_island(i,j+1,answer);
	}
	if(v[i-1][j-1] == 1){
		answer = del_island(i-1,j-1,answer);
	}
	if(v[i-1][j+1] == 1){
		answer = del_island(i-1,j+1,answer);
	}
	if(v[i+1][j-1] == 1){
		answer = del_island(i+1,j-1,answer);
	}
	if(v[i+1][j+1] == 1){
		answer = del_island(i+1,j+1,answer);
	}
	return answer;
}


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;
				v = 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 = 0; i < 60;i++){
			for( int j = 0; j < 60;j++){
				v[i][j] = 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;
	}
}