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>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
#define WHITE 0
#define GRAY 1
typedef long long LL;
const int MAXN = 60;
int w,h,ans, mapp[MAXN][MAXN], color[MAXN][MAXN];
vector<int> v_ans;

void dfs_visit(int u,int k) {

	color[u][k] = GRAY;//参照したことを示す


	if(mapp[u][k + 1] == 1 && color[u][k + 1] == WHITE) {//右
		dfs_visit(u,k + 1);
	}

	if(mapp[u + 1][k + 1] == 1 && color[u + 1][k + 1] == WHITE) {//右下
		dfs_visit(u + 1,k + 1);
	}

	if(mapp[u + 1][k] == 1 && color[u + 1][k] == WHITE) {//下
		dfs_visit(u + 1,k);
	}

    if(k > 0){
        if(mapp[u + 1][k - 1] == 1 && color[u + 1][k - 1] == WHITE) {// 左下
            dfs_visit(u + 1,k - 1);
        }
    }

    if(k > 0){
        if(mapp[u][k - 1] == 1 && color[u][k - 1] == WHITE) {// 左
            dfs_visit(u,k - 1);
        }
    }

    if(u > 0){
        if(mapp[u - 1][k] == 1 && color[u - 1][k] == WHITE) {// 上
            dfs_visit(u - 1,k);
        }
    }

    if(u > 0){
        if(mapp[u - 1][k + 1] == 1 && color[u - 1][k + 1] == WHITE) {// 右上
            dfs_visit(u - 1,k + 1);
        }
    }

    if(u > 0 && k > 0){
        if(mapp[u - 1][k - 1] == 1 && color[u - 1][k - 1] == WHITE) {// 左上
            dfs_visit(u - 1,k - 1);
        }
    }


}

void dfs() {
    int ans = 0;
	for(int i = 0; i < h; i++) {
        for(int j = 0; j < w; j++){
            if(mapp[i][j] == 1 && color[i][j] == WHITE ) {//未探訪の島なら探索

                dfs_visit(i,j);
                ans += 1;
            }
        }

	}

	v_ans.push_back(ans);

}

int main() {

    while(1){
        memset(mapp, 0, sizeof(mapp));//木情報をリセット
        memset(color, 0, sizeof(color));//探訪情報をリセット
        cin >> w >> h;
        if(w == 0) break;
        for(int i = 0; i < h; i++){

            for(int j = 0; j < w; j++){
                cin >> mapp[i][j];
            }

        }
        dfs();
    }
    rep(i,v_ans.size()) cout <<v_ans[i] <<endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<ll>;
using VV = vector<VI>;
using VS = vector<string>;

// tourist set
template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << '\n'; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// tourist set end

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; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,b) FOR(i, 0, b)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<'\n'
#define p2(s, t) cout << (s) << " " << (t) << '\n'
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << '\n'
#define p_yes() p("YES")
#define p_no() p("NO")
#define SZ(x) ((int)(x).size())
#define SORT(A) sort(ALL(A))
#define RSORT(A) sort(ALL(A), greater<ll>())

void no(){p_no(); exit(0);}
void yes(){p_yes(); exit(0);}

const ll mod = 1e9 + 7;
const ll inf = 1e18;
const double PI = acos(-1);


using PII = pair<ll, ll>;
#define MP make_pair
int dx[8] = {-1, 0, 1,  0, -1, 1, -1,  1};
int dy[8] = { 0, 1, 0, -1, -1, 1,  1, -1};


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

    // input
    ll w,h;

    while(cin>>w>>h){
      if(w==0) return 0;

      VV G(h);
      rep(i, h){
        rep(j, w){
          ll a; cin >> a;
          G[i].push_back(a);
        }
      }

      ll cnt = 0;
      rep(i, h){
        rep(j, w){
          if(G[i][j]==1){
            G[i][j]=0;
            cnt++;
            // bfs
            queue<PII> que;
            que.push(MP(i,j));
            while(!que.empty()){
              auto pa = que.front(); que.pop();
              rep(k, 8){
                ll ty = pa.first + dy[k];
                ll tx = pa.second+ dx[k];
                if(0<=ty && ty<h && 0<=tx && tx<w && G[ty][tx]==1){
                  G[ty][tx] = 0;
                  que.push(MP(ty, tx));
                }
              }
            }
          }
        }
      }
      p(cnt);
    }
    
    return 0;
}

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

using namespace std;

struct Point{
	int x, y;
};

const Point DIR[] = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
//ななめ移動ができる

int w,h;
int map[50][50];//1:陸 0:海
bool visited[50][50];

bool inField(Point pos){
	return 0<=pos.x && pos.x<w && 0<=pos.y && pos.y<h;
}

//同じ島のvisitedを埋める
void dfs(Point pos){
	visited[pos.y][pos.x] = true;
	for(int i=0;i<8;i++){
		Point next={pos.x + DIR[i].x, pos.y + DIR[i].y};
		if(inField(next)==true){
			if(visited[next.y][next.x] == false && map[next.y][next.x] == 1){
				//未訪問かつ、陸
				dfs(next);
			}
		}
	}
}

int main(){
	while(true){
		cin >> w >> h;
		if(w==0){
			break;
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> map[i][j];
				visited[i][j] = false;
			}
		}
		int cnt = 0;//島の数
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(visited[i][j] == false && map[i][j] == 1){
					Point pos = {j,i};
					dfs(pos);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}