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 g[52][52];
void solve(int x,int y){
  g[x][y]=0;
  for(int i=-1;i<=1;++i){
    for(int j=-1;j<=1;++j){
      if(g[x+i][y+j]) solve(x+i,y+j);
    }
  }
}
int main(){
  int w,h,cnt;
  while(cin>>w>>h&&w&&h){
    cnt=0;
    for(int i=0;i<=h+1;++i){
      for(int j=0;j<=w+1;++j){
        if(i==0||j==0||i==h+1||j==w+1) g[i][j]=0;
        else cin>>g[i][j];
      }
    }
    for(int i=1;i<=h;++i){
      for(int j=1;j<=w;++j){
        if(g[i][j]){
          solve(i,j);
          ++cnt;
        }
      }
    }
    cout<<cnt<<endl;
  }
  return 0;
}

C++: #include <iostream>

using namespace std;

int w, h;
int c[51][51];

bool valid(int i, int j) {
    return (0 <= i && i < h && 0 <= j && j < w);
}

void go(int y, int x) {
    if (c[y][x] == 0) return ;
    c[y][x] = 0;

    for (int i=y-1; i<=y+1; ++i) {
        for (int j=x-1; j<=x+1; ++j) {
            if (valid(i, j)) go(i, j);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);

    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) {
                if (c[i][j]) {
                    go(i, j);
                    ans ++;
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e7+9
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int w, h;
int c[100][100];
int ans;
void dfs(int y, int x){
	c[y][x] = 0;
	for (int i = -1; i <= 1; i++){
		for (int j = -1; j <= 1; j++){
			int ny = y + i, nx = x + j;//??????8???????????§?¨?
			if (ny >= 0 && ny < h&&nx >= 0 && nx < w){//?????????????????????
				//???????????£???????????¶???????????????????????°
				if (c[ny][nx] == 1)dfs(ny, nx);
			}
		}
	}
}

int main(){
	while (cin >> w >> h&&w + h){
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++) cin >> c[i][j];
		ans = 0;
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (c[i][j] == 1){//??¶??????????????????
					solve(i, j);//???????????£???????????¶????????¶???
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
}
}

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

void DFS(const Graph &G,int u,int v,vector<vector<bool>> &seen,vector<int> y, vector<int> x,int h,int w){//深さ優先探索。
//引数は順にグラフ、始点(注目点)、探索済みかどうか、y変位、x変位、y方向のグリッドサイズ、x方向のグリッドサイズ
  
  seen[u][v]=true;

  rep(k,8){
    int yd=u+y[k],xd=v+x[k];
    if(yd<0||h<=yd||xd<0||w<=xd) continue;//グラフの外側はスルー
    if(seen[yd][xd]) continue;//もう探索した場所はスルー
    if(G[yd][xd]==0) continue;//陸地でないならスルー
    DFS(G,yd,xd,seen,y,x,h,w);
  }
  return ;
}
 
int main() {

  while(1){

  int w,h;  cin >> w >> h ;

  if(w==0&&h==0) break;

  Graph G(h,vector<int> (w));//Grid

  rep(i,h){
    rep(j,w){
    cin >> G[i][j];
    }
  }//入力終了

  vector<vector<bool>> seen(h,vector<bool>(w,false));//探索済み
  int ans=0;//島の数

  vector<int> x={1,-1,0,0,1,1,-1,-1};
  vector<int> y={0,0,1,-1,-1,1,1,-1};

  rep(i,h){
    rep(j,w){
      if(seen[i][j]) continue;
      if(G[i][j]==0) continue;
      DFS(G,i,j,seen,y,x,h,w);

      ans++;//DFS一つ終わるたびに島+1
    }
  }
    cout << ans << endl;
  
  }
  return 0;
}

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[100][100]={0};
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){
    if(land[i][j]==1){
    	land[i][j] = 0;
		REP(k,8){
			int newx=j+dx[k];
			int newy=i+dy[k];
			isLand(newy,newx);
		}
    }
}
???
signed main(){
	int w,h;
	while( w=read(),h=read(),w||h){
		cnt=0;
		
		REP(i,100){
			REP(j,100){
				land[i][j]=0;
			}
		}
		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]==1){
					cnt++;
					isLand(i,j);
				}
			}
		}
		
		cout << cnt << endl;
	}
	
}

C++: #include <iostream>
#include <vector>
#include <cstring>
using namespace std;
vector<vector<int> > field;

// 深さ優先探索
bool seen[60][60];
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[9] = {0, 1, 0, -1, 1, -1, 1, -1};
int w,h;

void dfs(int current_h, int current_w){
    seen[current_h][current_w] = true;

    for(int dir=0; dir < 8; dir++){
        int next_w = current_w + dx[dir];
        int next_h = current_h + dy[dir];

        if(next_w < 0) continue;
        if(next_h < 0) continue;
        if(next_w >= w) continue;
        if(next_h >= h) continue;
        
        if(field[next_h][next_w] == 1 && !seen[next_h][next_w]){
            dfs(next_h, next_w);
        }
    }
}

int main(){
    while(true) {
        cin >> w >> h;
        memset(seen, 0, sizeof(seen));
        if( w==0 && h==0 ){
            break;
        }

        field.assign(h, vector<int>(w, 0));
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cin >> field[i][j];
            }
        }

        int count = 0;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(field[i][j] ==1 && !seen[i][j] ){
                    dfs(i, j);
                    count ++;
                }
            }
        }

        cout << count << endl;
    }
}