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 dx[8]={0,1,1,1,0,-1,-1,-1}, dy[8]={-1,-1,0,1,1,1,0,-1};

void dfs(int v[][55], int x, int y){
    v[y][x] = 0;
    for(int i=0;i<8;++i)
        if(v[y+dy[i]][x+dx[i]] == 1)
            dfs(v, x+dx[i], y+dy[i]);
}

int main(){
    int w, h;
    while(cin >> w >> h, w != 0){
        int v[55][55]={0};
        for(int i=0;i<h;++i)
            for(int j=0;j<w;++j)
                cin >> v[i][j];
        int ans = 0;
        for(int i=0;i<h;++i)
            for(int j=0;j<w;++j)
                if(v[i][j] == 1){
                    ans++;
                    dfs(v, j, i);
                }
        cout << ans << endl;
    }

    return 0;
}

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

void add_label(map<int,int> &tbl, int a, int b){
	if( a == b ) return;
	if( a > b ) tbl[a] = b;
	else tbl[b] = a;
}

int main(void){
	int w, h;
	while( cin>>w>>h, w||h ) {
		vector<vector<int> > field( h );
		vector<vector<int> > labeled( h );
		for( int i=0; i<h; ++i ) {
			field[i] = vector<int>( w );
			labeled[i] = vector<int>( w );
			for( int j=0; j<w; ++j ) {
				cin>>field[i][j];
				labeled[i][j] = 0;
			}
		}
		int label = 1;
		map<int, int> tbl;
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				if( field[i][j] ){
					bool f = false;
					if( i ) {
						if( j && labeled[i-1][j-1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j-1];
							f = true;
						}
						if( labeled[i-1][j] ) {
							if(!f) labeled[i][j] = labeled[i-1][j];
							else add_label( tbl, labeled[i][j], labeled[i-1][j] );
							f = true;
						}
						if( j < w - 1 && labeled[i-1][j+1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j+1];
							else add_label( tbl, labeled[i][j], labeled[i-1][j+1] );
							f = true;
						}
						if( j && labeled[i][j-1] ) {
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j], labeled[i][j-1] );
							f = true;
						}
						if(!f) labeled[i][j] = ++label;
					} else {
						if( j && labeled[i][j-1] ){
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j], labeled[i][j-1] );
							f = true;
						}
						if(!f) labeled[i][j] = ++label;
					}
				}
			}
		}
		for( auto x : tbl ) {
			int a = x.first;
			int b = x.second;
			while( tbl.find(b) != tbl.end() ) {
				b = tbl.find(b)->second;
			}
			for( int i=0; i<h; ++i ) {
				for( int j=0; j<w; ++j ) {
					if( labeled[i][j] == a ) labeled[i][j] = b;
				}
			}
		}
		
		/*
		for( int i=0; i<h; ++i ) {
			cerr<<"!";
			for( int j=0; j<w; ++j ) {
				cerr<<labeled[i][j];
			}
			cerr<<endl;
		}*/
		
		set<int> nums;
		nums.insert(0);
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				nums.insert( labeled[i][j] );
			}
		}
		cout<<nums.size()-1<<endl;
	}
	return 0;
}

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

#define ul unsigned long long
#define ll long long

ll td[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};

void dfs(int p[50][50],ll w,ll h,ll cx,ll cy){
  if (cx < 0 || cx >= w || cy < 0 || cy >= h)
    return;
  //cout << cx << "," << cy <<":"<<(int)p[cy][cx]<< endl;
  if (p[cy][cx] == 0)
    return;
  p[cy][cx]=0;
  ll nx,ny;
  for (ll i = 0; i < 8; i++)
  {
    nx = cx + td[i][0];
    ny = cy + td[i][1];
    dfs(p,w,h,nx,ny);
  }
}

int main()
{
  ll w,h;
  int p[50][50];
  ul ct = 0;

  cin >> w >> h;
  while (w | h)
  {
    ct=0;
    for (ll i = 0; i < h; i++)
    {
      for (ll j = 0; j < w; j++)
      {
        cin >> p[i][j];
      }
    }

    for (ll i = 0; i < h; i++)
    {
      for (ll j = 0; j < w; j++)
      {
        if (p[i][j])
          ct++;
        dfs(p, w, h, j, i);
      }
    }
    cout << ct << endl;
    cin >> w >> h;
  }

  return 0;
}

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

const int dx[3]={-1,0,1};

int w,h,ans;
queue<pair<int,int>> que;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> w >> h,w){
        vector<vector<int>> m(h,vector<int>(w));
        for (int i=0;i<h;++i)
            for (int j=0;j<w;++j)
                cin >> m[i][j];
        vector<vector<int>> visited(h,vector<int>(w,0));
        ans=0;
        for (int i=0;i<h;++i){
            for (int j=0;j<w;++j){
                if (visited[i][j]||!m[i][j]) continue;
                ++ans;
                visited[i][j]=1; que.emplace(i,j);
                while(!que.empty()){
                    pair<int,int> p=que.front(); que.pop();
                    for (int k=0;k<3;++k){
                        for (int l=0;l<3;++l){
                            int nh=p.first+dx[k],nw=p.second+dx[l];
                            if (nh<0||h<=nh||nw<0||w<=nw) continue;
                            if (visited[nh][nw]||!m[nh][nw]) continue;
                            visited[nh][nw]=1; que.emplace(nh,nw);
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int w, h;
int m[50][50];
int visited[50][50] = {0};

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

int main() {
  while(true) {
    rep(i, h) {
      rep(j, w) {
        visited[i][j] = 0;
      }
    }
    cin >> w >> h;
    if(w == 0 && h == 0) break;
    rep(i, h) {
      rep(j, w) {
        cin >> m[i][j];
      }
    }
    int count = 0;
    for(int i = 0; i < h; i++) {
      for(int j = 0; j < w; j++) {
        if(m[i][j] == 1 && visited[i][j] == 0) {
          count++;
          dfs(i, j);
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

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

int H, W;
bool grid[55][55];

void island(int h, int w) {
  grid[h][w] = 0;

  for(int i = -1; i <= 1; ++i) {
    for(int j = -1; j <= 1; ++j) {
      if(i == 0 && j == 0) continue;
      int nh = h + i;
      int nw = w + j;
      if(0 <= nh && nh < H && 0 <= nw && nw < W && grid[nh][nw]) island(nh, nw);
    }
  }
}

int main(){
  while(true) {
    cin >> W >> H;
    if(W == 0 && H == 0) break;

    for(int i=0; i < H; ++i) 
      for(int j=0; j < W; ++j) cin >> grid[i][j];

    int ans = 0;
    for(int i=0; i < H; ++i) {
      for(int j=0; j < W; ++j) {
        if(grid[i][j]) {
          island(i,j);
          ++ans;
        }
      }
    }
    cout << ans << '\n';
  }

  return 0;
}