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;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(a)     (a).begin(),(a).end()
#define VI         vector<int>
#define MOD 1000000007

typedef long long int ll;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

const ll INF=(ll)1e19;
//const int INF=(1<<30);
int h,w;
int m[50][50];

int dx[] = {0,1,-1,0,1,-1,1,-1};
int dy[] = {1,0,0,-1,1,-1,-1,1};
bool visited[50][50];

int dfs(int x, int y, int d){
  if(m[x][y]==0)return d;
  if(visited[x][y])return d;
  visited[x][y]=true;

  REP(i,8){
    int nx = x + dx[i];
    int ny = y + dy[i];

    if(nx < 0 || nx >= h || ny <0 || ny >= w)continue;
    if(m[nx][ny]==0)continue;
    if(visited[nx][ny])continue;

    dfs(nx, ny, d);
  }

  return d+1;
}

int main(){
  while(true){

    cin >> w >> h;
    if(h+w==0)break;
    REP(i,h)REP(j,w)cin >> m[i][j];
    REP(i,h)REP(j,w)visited[i][j]=false;

    int d = 0;
    REP(i,h)REP(j,w){
      d = dfs(i,j,d);
    }
    cout << d << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
using Int = long long;
Int const INF = 1e10;

int w,h; int fie[50][50];
void dfs(int y,int x){
    fie[y][x]=0;
    for (int i = -1; i <= 1; i++){
        for (int j = -1; j <= 1; j++){
            int ny=y+i,nx=x+j;
            if(ny>=0&&ny<h&&nx>=0&&nx<w&&fie[ny][nx]==1)dfs(ny,nx);
        }
    }
}

int main(){
    cin.tie(0); ios::sync_with_stdio(false);

    while(true){
        cin>>w>>h; if(w+h==0)break;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++) cin>>fie[i][j];
        }
        int ans=0;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                if(fie[i][j]==1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<'\n';
    }
}

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


int islands[50][100];
int w,h;

void dfs(int y,int x){

  islands[y][x] = 0;

  int dx[8] = {1,0,-1,0,1,1,-1,-1};
  int dy[8] = {0,1,0,-1,1,-1,1,-1};

  for(int i = 0;i < 8;i++){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(0 <= nx && nx < w && 0 <= ny && ny < h && islands[ny][nx] == 1)dfs(ny,nx);
  }

  return ;
}


int main(){

  vector<int> v;

  while(scanf("%d %d",&w,&h),w != 0 && h != 0){

    int ans = 0;

    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        scanf("%d",&islands[i][j]);
      }
    }

    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        if(islands[i][j] == 1){
          dfs(i,j);
          ans++;
        }
      }
    }


    v.push_back(ans);
  }

  for(int i = 0;i < v.size();i++){
    printf("%d\n",v[i]);
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define MAX 50

int c[MAX][MAX];

void dfs(int w, int h, int x, int y);
int main(int argc, char const *argv[]){
    int w, h, ans;

    while (cin>>w>>h, w || h){
        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){
                    dfs(w, h, j, i);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

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

    for (int i = 0; i < 8; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];

        if (0 <= nx && nx < w && 0 <= ny && ny < h && c[ny][nx] == 1){
            dfs(w, h, nx, ny);
        }
    }
    return;
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<bitset>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
using namespace std;

void erase_island (int ocean[][50], int h, int w, int i, int j) {
    ocean[i][j] = 0;
    for(int dy = -1; dy <= 1; dy++) {
        for(int dx = -1; dx <= 1; dx++) {
            if (i + dy >= 0 && i + dy < h && j + dx >= 0 && j + dx < w) {
                if(ocean[i+dy][j+dx] == 1)  erase_island(ocean, h, w, i+dy, j+dx);
            }
        }
    }
}

int main() {

    int w, h;
    while(cin >> w >> h && (w || h)) {
        int ocean[h][50];
        for(int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> ocean[i][j];
            }
        }
        int answer = 0;
        for(int i = 0; i < h; i++) {
            for(int j = 0; j < w; j++) {
                if (ocean[i][j] == 1) {
                    erase_island(ocean, h, w, i, j);
                    answer += 1;
                }
            }
        }
        cout << answer << endl;
    }
     
    return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

int tizu[52][52];

void check(int i, int j) {
	tizu[i][j] = 0;
	for (int k = -1; k <= 1; k++) {
		for (int l = -1; l <= 1; l++) {
			if (tizu[i+k][j+l] == 1) {
				check(i+k,j+l);
			}
		}
	}
	return;
}

int main() {
	int w, h;
	while (cin >> w >> h) {
		if (w == 0 && h == 0) {
			break;
		}
		for (int i = 0; i < 52; i++) {
			for (int j = 0; j < 52; j++) {
				tizu[i][j] = 0;
			}
		}
		int counter = 0;
		for (int i = 1; i <= h; i++) {
			for (int j = 1; j <= w; j++) {
				cin >> tizu[i][j];
			}
		}
		for (int i = 1; i <= h; i++) {
			for (int j = 1; j <= w; j++) {
				if (tizu[i][j] == 1) {
					counter++;
					check(i,j);
				}
			}
		}
		cout << counter << endl;
	}
	return 0;
}

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

// 入力
int H, W;
vector<vector<int> > field;

// 探索
void dfs(int h, int w) {
    field[h][w] = 0;

    // 八方向を探索
    for (int dh = -1; dh <= 1; ++dh) {
        for (int dw = -1; dw <= 1; ++dw) {
            int nh = h + dh, nw = w + dw;
            
            // 場外アウトしたり、0 だったりはスルー
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) 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));
        for (int h = 0; h < H; ++h) for (int w = 0; w < W; ++w) cin >> field[h][w];

        // 探索開始
        int count = 0;
        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                if (field[h][w] == 0) continue; // 残島じゃなかったらスルー
                dfs(h, w);
                ++count;
            }
        }
        cout << count << endl;
    }
}