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<algorithm>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

vector<int> dx{1, 0, -1, 0, 1, -1, 1, -1};
vector<int> dy{0, 1, 0, -1, -1, 1, 1, -1};

vector<vector<int>> g;
int w, h;

vector<vector<bool>> seen;
void dfs(int x, int y){
  seen[y][x] = true;
  for(int i = 0; i < 8; ++i){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny)continue;
    else if(!g[ny][nx])continue;
    else if(seen[ny][nx])continue;
    dfs(nx, ny);
  }
}

int main(){
  while(true){
    cin >> w >> h;
    if(w == 0 && h == 0)return 0;
    g = vector<vector<int>>(h, vector<int>(w));
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
        cin >> g[i][j];
      }
    }

    seen = vector<vector<bool>>(h, vector<bool>(w, false));
    int cnt = 0;
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
        if(seen[i][j])continue;
        if(!g[i][j])continue;
        dfs(j, i);
        ++cnt;
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,b) FOR(i,0,b)
#define INF 1e9

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

void search(int x, int y){
  if(c[y][x] == 1 && x >=0 && x < w && y >=0 && y < h){
    c[y][x] = i_num;
    REP(i,3) REP(j,3){
      if(i != 0 || j != 0 || x-1 >= 0 || x+1 < w || y-1 >= 0 || y+1 < h)
      search(x+dx[i],y+dy[j]);
    }
  }
}

void solve(int X, int Y){
  i_num = 2;
  REP(i,20) REP(j,20) c[i][j] = 0;
  //0:ocean, 1:land, 2~:island_number

  REP(i,Y){
    REP(j,X){
      cin>>c[i][j];
    }
  }

  REP(i,Y){
    REP(j,X){
      if(c[i][j] == 1){
        search(j,i);
        i_num++;
      }
    }
  }

  cout<<i_num-2<<endl;
  /*REP(i,20){
     REP(j,20) cout<<c[i][j];
     cout<<endl;
  }*/
}

int main(){

  while(1) {
    cin>>w>>h;
    if(w == 0 && h == 0) break;
    solve(w,h);
  }

  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vector<int>>;

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

vb seen;

void dfs(const Graph &G, int v) {
    seen[v] = true;
    for(auto next_v : G[v]) {
        if (seen[next_v]) continue;
        dfs(G, next_v);
    }
}

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

    int w, h;
    while(cin >> w >> h, w*h) {
        int map[h+1][w+1];
        REP(j, w+1) {
            map[0][j] = 0;
        }
        REP(i, h) {
            map[i+1][0] = 0;
            REP(j, w) {
                cin >> map[i+1][j+1];
            }
        }
        
        Graph G(w*h);
        FOR(i, 1, h+1) {
            FOR(j, 1, w+1) {
                if (map[i][j] == 1) {
                    int p1 = (i-1)*w+(j-1);
                    int p2;
                    if (map[i][j-1] == 1) {
                        p2 = (i-1)*w+(j-2);
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                    if (map[i-1][j-1] == 1) {
                        p2 = (i-2)*w+(j-2);
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                    if (map[i-1][j] == 1) {
                        p2 = (i-2)*w+(j-1);
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                    if (map[i-1][j+1] == 1) {
                        p2 = (i-2)*w+j;
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                }
            }
        }

        seen.assign(w*h, false);
        int cnt = 0;
        REP(i, w*h) {
            if (seen[i]) continue;
            if (map[i/w+1][i%w+1] == 0) {
                seen[i] = true;
                continue;
            }
            cnt++;
            dfs(G, i);
        }
        cout << cnt << "\n";
    }
}

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

typedef std::pair<int, int> P;
int vx[] = { 0, 1, 0, -1, 1, 1, -1, -1 }, vy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
int w, h;
int map[51][51];

void bfs(int x, int y){

	map[y][x] = 0;

	std::queue<P>que;
	que.push(P(x, y));

	while (!que.empty()){

		P p = que.front();
		que.pop();

		for (int i = 0; i < 8; i++){
			int nx = p.first + vx[i], ny = p.second + vy[i];

			if (0 <= nx&&nx < w && 0 <= ny&&ny < h&&map[ny][nx] != 0){
				map[ny][nx] = 0;
				que.push(P(nx, ny));
			}
		}
	}

	return;
}

int main(){

	while (std::cin >> w >> h, w || h){

		int cnt = 0;

		memset(map, 0, sizeof(map));

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				std::cin >> map[i][j];
			}
		}

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (map[i][j] != 0){
					bfs(j, i);
					cnt++;
				}
			}
		}

		std::cout << cnt << std::endl;
	}

	return 0;
}

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

int H, W;
int field[51][51];

//動的計画法
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;
            int nw = w + dw;

            //場外アウトまたは０ならば、それ以降（再帰的に探索する部分）は実行せずループの最初に戻る
            if(nh < 0 || nh > H || nw < 0 || nw > W || field[nh][nw] == 0) continue;

            //再帰的に探索（探索しているマスを基準にしてその八方向にあるマスを探索していく）
            dfs(nh, nw);
        }
    }
}

int main(void){
    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 >> field[i][j];
            }
        }

        //探索開始
        int c = 0; //島の個数
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(field[i][j] == 0) continue; //島の一部でないならば、それ以降スルー
                dfs(i, j); //そのマスが陸であるならば、その八方向を動的計画法で探索していく
                c++;
            }
        }
        cout << c << endl;
    }
}