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;
void kazoeruyo(int p,int P,int box[50][50]){
  box[p][P]=0;
    if(box[p][P+1]==1) {
      box[p][P+1]=0;
      P=P+1; 
      kazoeruyo(p,P,box);
    }
    if(box[p][P-1]==1){
      box[p][P-1]=0;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P]==1){ 
      box[p+1][P]=0;
      p=p+1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P]==1){
      box[p-1][P]=0;
      p=p-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P-1]==1){
      box[p-1][P-1]=0;
      p=p-1;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P+1]==1){
      box[p-1][P+1]=0;
      p=p-1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P+1]==1){
      box[p+1][P+1]=0;
      p=p+1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P-1]==1){
      box[p+1][P-1]=0;
      p=p+1;
      P=P-1;
      kazoeruyo(p,P,box);
    }	
}
int main(){
  int w,h,i,I,p,P,o,t,T,y,Y;
  int box[10][10];
  while(1){
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	cin>>box[i][I];
      }
    }
    o=0;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) {
	  p=i;
	  P=I;
	  kazoeruyo(p,P,box);
	  box[p][P]=1;
	}
      }
    }
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) o++;
      }
    }
    cout<<o<<endl;
    o=0;
  }
  return 0;
}

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

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

void dfs(int y, int x)
{
  int dx[]={1,-1,0,0};
  int dy[]={0,0,1,-1};

  maps[y][x]=0;

  for(int i=0;i < 4;i++){
    for(int j=0;j < 4;j++){
      int xx = x + dx[j];
      int yy = y + dy[i];
      if(maps[yy][xx] == 1) dfs(yy,xx);
    }
  }
}
int main(){
  while(cin >> w >> h && w)
    {
      for(int i=0;i < h;i++){
	for(int j=0;j < w;j++){
	  cin >> maps[i][j];
	}
      }
      int island=0;

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

C++: #include<iostream>
#include<vector>
#include<queue>
#include<utility>

using namespace std;

pair<int,int> finish(vector<vector<int>> a,int h,int w){
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(a[i][j]==1){return make_pair(i,j);}
    }
  }
  return make_pair(h,w);
}

//沈めてく
void idlands(int H,int W){
  vector<vector<int>> d(H,vector<int>(W,0));
  queue< pair<int,int> > x;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin >> d[i][j];
    }
  }
  int l=1;
  int c=0;
  while(finish(d,H,W)!=make_pair(H,W)){
    x.push(finish(d,H,W));
    c+=1;l=x.size();d[x.front().first][x.front().second]=0;
    while(l!=0){
      for(int i=0;i<l;i++){
        int h,w;h=x.front().first;w=x.front().second;
        if(0<h and d[h-1][w]==1){
          x.push(make_pair(h-1,w));d[h-1][w]=0;}
        if(h<H-1 and d[h+1][w]==1){
          x.push(make_pair(h+1,w));d[h+1][w]=0;}
        if(0<w and d[h][w-1]==1){
          x.push(make_pair(h,w-1));d[h][w-1]=0;}
        if(w<W-1 and d[h][w+1]==1){
          x.push(make_pair(h,w+1));d[h][w+1]=0;}
        if(0<h and w<W-1 and d[h-1][w+1]==1){
          x.push(make_pair(h-1,w+1));d[h-1][w+1]=0;}
        if(h<H-1 and 0<w and d[h+1][w-1]==1){
          x.push(make_pair(h+1,w-1));d[h+1][w-1]=0;}
        if(0<h and 0<w and d[h-1][w-1]==1){
          x.push(make_pair(h-1,w-1));d[h-1][w-1]=0;}
        if(h<H-1 and w<W-1 and d[h+1][w+1]==1){
          x.push(make_pair(h+1,w+1));d[h+1][w+1]=0;}
        x.pop();
      }
      l=x.size();
    }
  }
  cout << c << endl;
}

int main(){
  int W,H;
  while(cin >> W >> H and W!=0){
    idlands(H,W);
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;

vector<vector<int>> c(51,vector<int>(51,0));
int w,h;

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

int main(){
	while (1) {
		cin>>w>>h;
		if (w == 0 && h == 0){
			break;
		}
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				cin >> c[i][j];
			}	
		}
		int ans = 0;

	// for(int i = 0; i < h; i++) {
	// 	for(int j = 0; j < w; j++) {
	// 		cout << c[i][j];
	// 	}
	// 	cout << endl;
	// }
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if (c[i][j] == 1){
					search(i,j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: 
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <ctime>
#include <string>
#include <map>
#include <stack>
#include <queue>

using namespace std;

int w, h;
const int MAX = 50;
int c[MAX][MAX];


void check(int x, int y){
	c[x][y] = 0;
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            if(x + dx >= 0 && x + dx <= h  && y + dy >= 0 && y + dy <= w && c[x + dx][y + dy] == 1){
            	check(x + dx, y + dy);
            } 
        }
    }
	
}

int main(void){

	for(int i = 0; i < MAX ; i++){
		for(int j = 0; j < MAX; j++){
			c[i][j] = 0;
		}
	}


	while((cin >> w) != 0 && (cin >> h) != 0){
		int ans = 0;
		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				cin >> c[i][j];
			}
		}

		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				if(c[i][j] == 1){
					check(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	
    return 0;
}