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;
int w, h;
vector<int> ans;
char c[55][55];

void dfs(int x, int y) {
    c[x][y] = '0';
    for (int dh = -1; dh <= 1; dh++) {
        for (int dw = -1; dw <= 1; dw++) {
            int nh = x + dh, nw = y + dw;
            if (0 <= nh && 0 <= nw && nh < h && nw < w && c[nh][nw] == '1') dfs(nh, nw);
        }
    }
    return;
}

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 cnt = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (c[i][j] == '1') { 
                    dfs(i, j);
                    cnt++;
                }
            }
        }
        ans.push_back(cnt);
    }
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << endl;
    }
}

C++: #include <iostream>
using namespace std;
int main(){
	int w,h,ct,num2;
	while(cin>>w>>h){
		ct=0;
		if(w==0&&h==0) break;
		int c[w+2][h+2];
		for(int i=0;i<w+2;i++){
			for(int j=0;j<h+2;j++){
				c[i][j]=0;
			}
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>c[j+1][i+1];
			}
		}
		for(int i=0;i<w+2;i++){
			for(int j=0;j<h+2;j++){
				if(c[i][j]==1){
					c[i][j]=2;
					do{
						num2=0;
						for(int ii=1;ii<w+1;ii++){
							for(int jj=1;jj<h+1;jj++){
								if(c[ii][jj]==2&&c[ii-1][jj-1]==1){
									c[ii-1][jj-1]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii-1][jj]==1){
									c[ii-1][jj]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii-1][jj+1]==1){
									c[ii-1][jj+1]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii][jj-1]==1){
									c[ii][jj-1]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii][jj+1]==1){
									c[ii][jj+1]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii+1][jj-1]==1){
									c[ii+1][jj-1]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii+1][jj]==1){
									c[ii+1][jj]=2;
									num2++;
								}
								if(c[ii][jj]==2&&c[ii+1][jj+1]==1){
									c[ii+1][jj+1]=2;
									num2++;
								}
							}
						}
					} while(num2>0);
					ct++;
					for(int ii=0;ii<w+2;ii++){
						for(int jj=0;jj<h+2;jj++){
							if(c[ii][jj]==2){
								c[ii][jj]=0;
							}
						}
					}
				}
			}
		}
		cout<<ct<<endl;		
	}
    return 0;
}

C++: #include <iostream>

using namespace std;

int w, h, cnt;
int island[50][50];
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};

bool check(int nx, int ny){

  if(nx < 0 || w <= ny || ny < 0 || h < ny)return false;
  else return true;

}

void dfs(int sy, int sx){

  island[sy][sx] = 0;

  for(int i = 0 ; i < 8 ; i++){
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    if(!check(nx, ny)) continue;
    if(island[ny][nx]) dfs(ny, nx);
  }
}

int main(void){

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

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

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

C++: #include <iostream>
using namespace std;
void kazoeruyo(int p,int P,int box[10][10]){
  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<iostream>
using namespace std;
void b(int ,int );
int a[100][100]={};
 
int main(){
  while(1){
    int count=0,w,h;
    cin>>w>>h;
    if(w+h==0)break;
     
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++)
    cin>>a[i][j];
 
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++)
    if(a[i][j]==1){
      b(j,i);
      count++;
    }
    cout<<count<<endl;  
  }
  return 0;
}
 
  void b(int x,int y){
    a[y][x]=0;
    int x1,y1;
    int x2[8]={-1,0,1,1,1,0,-1,-1};
    int y2[8]={1,1,1,0,-1,-1,-1,0};
    for(int k=0;k<8;k++){
    x1=x+x2[k];
    y1=y+y2[k];
    if(a[y1][x1]==1)
      b(x1,y1);
    }
  }