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>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
bool find1(vector< vector<int> >& C,vector< vector<int> >& check,int* findh,int* findw)
{
  for(int i = 0;i < C.size();++i)
    for(int j = 0;j < C[0].size();++j)
      if(C[i][j] == 1 && check[i][j] == 0){
	*findh = i;
	*findw = j;
	return true;
      }
  return false;
}
bool find2(vector< vector<int> >& C,vector< vector<int> >& check,int* findh,int* findw)
{
  if(C[*findh][*findw - 1] == 1 && check[*findh][*findw - 1] == 0){
    *findw -= 1;
    return true;
  }
  else if(C[*findh][*findw + 1] == 1 && check[*findh][*findw + 1] == 0){
    *findw += 1;
    return true;
  }
  else if(C[*findh + 1][*findw - 1] == 1 && check[*findh + 1][*findw - 1] == 0){
    *findh += 1;
    *findw -= 1;
    return true;
  }
  else if(C[*findh + 1][*findw] == 1 && check[*findh + 1][*findw] == 0){
    *findh += 1;
    return true;
  }
  else if(C[*findh + 1][*findw + 1] == 1 && check[*findh + 1][*findw + 1] == 0){
    *findh += 1;
    *findw += 1;
    return true;
  }
  else return false;
}
int main()
{
  int howmany = 0,w,h,findh,findw;
  stack< pair<int,int> > finish;
  queue<int> answer;
  while(cin >> h >> w && !(w == 0 && h == 0)){
    vector< vector<int> > C(h + 2,vector<int>(w + 2,0)),check(h + 2,vector<int>(w + 2,0)); 
    for(int i = 1;i <= h;++i)
      for(int j = 1;j <= w;++j)
	cin >> C[i][j];
    while(find1(C,check,&findh,&findw)){
      check[findh][findw] = 1;
      finish.push(make_pair(findh,findw));
      while(!finish.empty()){
	findh = finish.top().first;
	findw = finish.top().second;
	while(find2(C,check,&findh,&findw)){
	  finish.push(make_pair(findh,findw));
	  check[findh][findw] = 1;
	}
	finish.pop();
      }
      ++howmany;
    }
    answer.push(howmany);
    howmany = 0;
  }
  for(;!answer.empty();answer.pop())
    cout << answer.front() << endl;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

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

void dfs(int h, int w) {
	field[h][w] = 0;
	/* 8方向に探索 */
	for (int dh = -1; dh <= 1; dh++) {
		for (int dw = -1; dw <= 1; dw++) {
			int nh = h + dh;
			int nw = w + dw;

			if (nh < 0 || H <= nh || nw < 0 || W <= nw) 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)); // 0で初期化
		for (int h = 0; h < H; h++) {
			for (int w = 0; w < W; w++) {
				cin >> field[h][w];
			}
		}
		 
		/* すべての地点に対して探索を行う */
		int count = 0;
		rep(h, H) {
			rep(w, W) {
				if (field[h][w] == 0) continue;
				dfs(h, w);
				count++;
			}
		}
		cout << count << endl;
	}
}

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


int foo(int c[2][60][60], int y, int x){
	if(c[1][y][x] == 1) return 0;
	c[1][y][x] = 1;
	if(c[0][y][x] == 0) return 0;
	
	for(int i = -1; i <= 1; i++)
		for(int j = -1; j <= 1; j++)
			foo(c, y+i, x+j);
	
	return 1;
}

int main(){
	int h, w;
	
	while(1){
		int  c[2][60][60] = {}, count = 0;
		
		cin>>w>>h;
		
		if(w == 0 && h == 0) break;
		
		for(int i = 1; i <= h; i++)
			for(int j = 1; j <= w; j++)
				cin>>c[0][i][j];
		
		for(int i = 1; i <= h; i++)
			for(int j = 1; j <= w; j++)
				if(foo(c, i, j) == 1) count++;
				
		cout<<count<<endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int field[52][52];

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

  if (field[y][x] != 1)
    return;
  field[y][x] = 2;
  for (int i = 0; i < 8; ++i) {
    dfs(y+dy[i], x+dx[i]);
  }
}

int main()
{
  int w, h;
  while (scanf("%d%d", &w, &h), w|h) {
    memset(field, 0, sizeof(field));
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        scanf("%d", &field[i][j]);
      }
    }
    int count = 0;
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        if (field[i][j] == 1) {
          ++count;
          dfs(i, j);
        }
      }
    }
    printf("%d\n", count);
  }
}

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

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

int land[1000][1000]={-1};
int flag[1000][1000]={-1};
int dy[]={-1,-1,-1,0,0,1,1,1};
int dx[]={-1,0,1,-1,1,-1,0,1};
int cnt=0;

void isLand(int i,int j){
	REP(k,8){
		int newx=i+dx[k];
		int newy=j+dy[k];
		if(land[newx][newy]){
			if(flag[newx][newy]!=-1){
				flag[i][j]=flag[newx][newy];
				return;
			}
		}
	}
	cnt++;
	flag[i][j] = cnt;
}

signed main(){
	int w,h;
	while( w=read(),h=read(),w||h){
		cnt=0;
		
		REP(i,1000){
			REP(j,1000){
				land[i][j]=-1;
				flag[i][j]=-1;
			}
		}
		
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				land[i][j]=read();
			}
		}
		
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				if(land[i][j])isLand(i,j);
			}
		}
		
		cout << cnt << endl;
	}
	
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<numeric>
#include<vector>
#include<cstring>
#define foreach(t,p) for(t::iterator it=p.begin();it!=p.end();p++)
#define REP(i,n) for(int i=0;i<n;i++)
using namespace std;
#define MAX_W 50

bool island[MAX_W+3][MAX_W+3];

int solve(int x,int y)
{
	if(!island[x][y]) {return 0;}

	island[x][y]= false;
	solve(x-1,y-1);
	solve(x-1,y);
	solve(x-1,y+1);
	solve(x,y-1);
	solve(x,y+1);
	solve(x+1,y+1);
	solve(x+1,y);
	solve(x+1,y-1);
//	cout << x << " " << y << endl;
	return 1;
}


int main()
{
	int w,h;
	while(cin >> w >> h &&w && h)
	{
		REP(i,MAX_W+2)REP(j,MAX_W+2) island[i][j]=false;
		REP(i,h)REP(j,w){cin >> island[i+1][j+1];}
		int kosu = 0;
		REP(i,h)REP(j,w){kosu += solve(i+1,j+1);}
		cout << kosu << endl;
		
	}
	return 0;
}