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>
using namespace std;

int myMap[51][51];
int w, h;
int ans;

bool safe(int x, int y){
    return (0 <= x && 0 <= y) && (x < h && y < w);
}

void del(int x, int y){
    myMap[x][y] = 0;
    if (safe(x+1, y) && myMap[x+1][y]) del(x+1, y);
    if (safe(x+1, y+1) && myMap[x+1][y+1]) del(x+1, y+1);
    if (safe(x+1, y-1) && myMap[x+1][y-1]) del(x+1, y-1);
    if (safe(x, y+1) && myMap[x][y+1]) del(x, y+1);
    if (safe(x, y-1) && myMap[x][y-1]) del(x, y-1);
    if (safe(x-1, y-1) && myMap[x-1][y-1]) del(x-1, y-1);
    if (safe(x-1, y+1) && myMap[x-1][y+1]) del(x-1, y+1);
    if (safe(x-1, y) && myMap[x-1][y]) del(x-1, y);
    return;
}

int main(){

    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 >> myMap[i][j];
	    }
	}

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

	cout << ans << endl;
    }
}

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

void ume(int y, int x, vector<vector<int>> &field) {
    int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
    int dy[] = {0, 1, -1, 1, -1, 0, 1, -1};

    for (int d = 0; d < 8; d++) {
        if (field[y + dy[d]][x + dx[d]] == 1) {
            field[y + dy[d]][x + dx[d]] = 0;
            ume(y + dy[d], x + dx[d], field);
        }
    }
}

int solve(int w, int h) {
    vector<vector<int>> field(h + 2, vector<int>(w + 2, 0));
    for (int i = 0; i < h + 2; i++) {
        for (int j = 0; j < w + 2; j++) {
            if (i == 0 || i == h + 1 || j == 0 || j == w + 1)
                field[i][j] = -1;
            else
                cin >> field[i][j];
        }
    }
    // for (int i = 0; i < h + 2; i++) {
    //     for (int j = 0; j < w + 2; j++) {
    //         // if (i == 0 || i == h + 1 || j == 0 || j == w + 1)
    //         //     field[i][j] = -1;
    //         // else
    //         //     cin >> field[i][j];
    //         cout << field[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    // queue<pair<int,int>> que;
    int island = 0;
    for (int i = 0; i < h + 2; i++) {
        for (int j = 0; j < w + 2; j++) {
            if (field[i][j] == 1) {
                field[i][j] = 0;
                ume(i, j, field);
                island++;
            }
        }
    }
    return island;
}

int main(void) {
    int w, h;
    vector<int> ans;
    while (1) {
        cin >> w >> h;
        if (!w && !h) break;
        ans.push_back(solve(w, h));
    }
    for (int a : ans) cout << a << endl;
}

C++: #include<bits/stdc++.h>
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//clock_t start=clock();clock_t end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int> pii;
typedef pair<pii,int> ppii;
typedef pair<int,pii> pipi;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppll;
typedef pair<ll,pll> plpl;
typedef tuple<ll,ll,ll> tl;
typedef pair<double,double> pdd;
ll mod=1000000007;
ll mod2=998244353;
ll mod3=1000003;
ll mod4=998244853;
ll mod5=1000000009;
ll inf=1LL<<60;
double pi=3.141592653589793238462643383279L;
double eps=1e-9;
#define rep(i,m,n) for(ll i=m;i<n;i++)
#define rrep(i,n,m) for(ll i=n;i>=m;i--)
#define srep(itr,st) for(auto itr=st.begin();itr!=st.end();itr++)
#define mrep(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++)
#define Max(a,b) a=max(a,b)
#define Min(a,b) a=min(a,b)
int dh[4]={1,-1,0,0};
int dw[4]={0,0,1,-1};
int ddh[8]={-1,-1,-1,0,0,1,1,1};
int ddw[8]={-1,0,1,-1,1,-1,0,1};
ll gcd(ll a,ll b){
    if(a<b)swap(a,b);
    if(b==0)return a;
    if(a%b==0)return b;
    return gcd(b,a%b);
}
ll Pow(ll n,ll k){
    ll ret=1;
    ll now=n;
    while(k>0){
        if(k&1)ret*=now;
        now*=now;
        k/=2;
    }
    return ret;
}
ll beki(ll n,ll k,ll md){
  ll ret=1;
  ll now=n;
  while(k>0){
    if(k%2==1){
      ret*=now;
      ret%=md;
    }
    now*=now;
    now%=md;
    k/=2;
  }
  return ret;
}
ll gyaku(ll n,ll md){
  return beki(n,md-2,md);
}
ll popcount(ll n){
    ll ret=0;
    ll u=n;
    while(u>0){
        ret+=u%2;
        u/=2;
        
    }
    return ret;
}

int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    for(;;){
        int h,w;cin>>w>>h;
        if(h==0&&w==0)return 0;
        int a[h][w];
        rep(i,0,h)rep(j,0,w)cin>>a[i][j];
        bool used[h][w];
        memset(used,false,sizeof used);
        int ans=0;
        rep(i,0,h){
            rep(j,0,w){
                //cout<<i<<" "<<j<<" "<<used[i][j]<<endl;
                if(used[i][j]==true)continue;
                if(a[i][j]==0)continue;
                queue<pii> q;
                ans++;
                q.push({i,j});
                used[i][j]=true;
                while(q.size()>0){
                    pii p=q.front();q.pop();
                    int nh=p.first,nw=p.second;
                    
                    rep(k,0,8){
                        int nnh=nh+ddh[k],nnw=nw+ddw[k];
                        //if(i==0&&j==0)cout<<nnh<<" "<<nnw<<endl;
                        if(nnh<0||nnh>=h||nnw<0||nnw>=w)continue;
                        if(a[nnh][nnw]==0)continue;
                        if(used[nnh][nnw])continue;
                        
                        q.push({nnh,nnw});
                        used[nnh][nnw]=true;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
}