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 <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

const ll INF = 1LL << 50;

const ll mod = 1e9 + 7;

int main() {
  ll w, h;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;
    vvll mp(h, vll(w, -1));
    vvll mk(h, vll(w, -1));
    rep(i, h) rep(j, w) cin >> mp[i][j];

    ll ans = 0;
    queue<P> que;
    bool pushcond;
    rep(i, h) {
      rep(j, w) {
        pushcond = mk[i][j] == -1 && mp[i][j] == 1;
        if (pushcond) {
          ans++;
          mk[i][j] = ans;
          que.emplace(i, j);
        }
        while (!que.empty()) {
          ll x, y;
          tie(y, x) = que.front();
          que.pop();
          mk[i][j] = ans;
          rep(k, 9) {
            ll nx = x + dx[k];
            ll ny = y + dy[k];
            bool insidecond = 0 <= nx && nx < w && 0 <= ny && ny < h;
            if (!insidecond) continue;
            pushcond = mk[ny][nx] == -1 && mp[ny][nx] == 1;
            if (pushcond) {
              mk[ny][nx] = ans;
              que.emplace(ny, nx);
            }
          }  // k
        }    // while
      }      // j
    }        // i

    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <tuple>
#include <sstream>
#include <set>

struct Node
{
  int data;
  Node* parent;
  int rank;
  Node() : data(-1), parent(this), rank(0){}
  Node& operator=(int data){ this->data = data; return *this; }
  Node* Find(){
    if (this == this->parent){
      return this;
    } else {
      auto root = this->parent->Find();
      this->parent = root;
      return root;
    }
  }
  static void Union(Node& lhs, Node& rhs){
    Node* lhs_root = lhs.Find();
    Node* rhs_root = rhs.Find();
    if (lhs_root->rank > rhs_root->rank){
      rhs_root->parent = lhs_root;
    } else if (rhs_root->rank > lhs_root->rank){
      lhs_root->parent = rhs_root;
    } else if (rhs_root != lhs_root){
      lhs_root->parent = rhs_root;
      rhs_root->rank += 1;
    }
    return;
  }
};

bool is_not_space(int c){ return c != ' '; }
std::string& ltrim(std::string& str){
  str.erase(str.begin(), std::find_if(str.begin(), str.end(), is_not_space));
  return str;
}
std::string& rtrim(std::string& str){
  str.erase(std::find_if(str.rbegin(), str.rend(), is_not_space).base(), str.end());
  return str;
}
std::string trim(std::string& str){ return ltrim(rtrim(str)); }
std::vector<std::pair<int, int>> const delta = {{-1,1}, {0,1}, {1,1}, {1,0}};

int main(void)
{
  std::string buff;
  while(true){
    std::getline(std::cin, buff);
    int w, h;
    {
      std::stringstream ss(buff);
      ss >> w;
      ss >> h;
    }
    if(w == 0 && h == 0){ break; }
    std::vector<Node> V((w+2)*(h+2));
    auto to_index = [=](int i, int j) -> size_t { return i*(w+2) + j; };
    std::vector<std::vector<int>> C(h+2, std::vector<int>(w+2, 0));
    for(int i=1; i<=h; ++i){
      std::getline(std::cin, buff);
      std::stringstream ss(buff);
      for(int j=1; j<=w; ++j){
        ss >> C[i][j];
        if(C[i][j] == 1){ V[to_index(i,j)].data = to_index(i,j); }
      }
    }
    for(int i=1; i<=h; ++i){
      for(int j=1; j<=w; ++j){
        if(C[i][j] == 0){ continue; }
        for(auto& d: delta){
          int di = d.first, dj = d.second;
          if(C[i+di][j+dj] == 1){ Node::Union(V[to_index(i,j)], V[to_index(i+di, j+dj)]); }
        }
      }
    }
    std::set<int> cluster;
    for(int i=1; i<=h; ++i){
      for(int j=1; j<=w; ++j){
        cluster.insert(V[to_index(i,j)].Find()->data);
      }
    }
    cluster.erase(-1);
    std::cout << cluster.size() << std::endl;

  }

  return 0;
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int box[50][50];
int main(){
  int w,h,i,I,a;
  while(1){
    a=0;
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<=h+1;i++){
      for(I=0;I<=w+1;I++){
	box[i][I]==0;
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	cin>>box[i][I];
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	if(box[i][I]==1){
	  dfs(i,I);
	  a++;
	}
      }
    }
    cout<<a<<endl;
  }
  return 0;
}
  void dfs(int i,int I){
  if(box[i][I]==0) return;
  box[i][I]=0;
  dfs(i,I+1);
  dfs(i+1,I+1);
  dfs(i+1,I);
  dfs(i+1,I-1);
  dfs(i,I-1);
  dfs(i-1,I-1);
  dfs(i-1,I);
  dfs(i-1,I+1);
  }

C++: #include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define p(S) cout<<S<<endl
using namespace std;

int chizu[51][51];

void labeling(int y,int x,int w,int h){
	chizu[y][x]=0;
	REP(dy,-1,2){
		REP(dx,-1,2){
			if(0 <= y+dy && y+dy < h && 0 <= x+dx && x+dx < w){
				if(chizu[y+dy][x+dx]){
					labeling(y+dy,x+dx,w,h);
				}
			}
		}
	}
}

int main(){
	int w,h;
	while(cin>>w>>h&&w&&h){

		int count=0;
		REP(i,0,h){
			REP(j,0,w){
				cin>>chizu[i][j];
			}
		}
		REP(i,0,h){
			REP(j,0,w){
				if(chizu[i][j]){
					count++;
					labeling(i,j,w,h);
				}
			}
		}

		p(count);
	}
	return 0;
}

C++: # include <iostream>

const int MAX_W = 50;
const int MAX_H = 50;

int w, h;

int c[MAX_H][MAX_W];

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

void dfs(int x, int y) {
	c[y][x] = 0;
	for (int i = 0; i < 8; ++i) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx<0 || ny<0 || nx>w || ny>h)
			continue;
		if (c[ny][nx])
			dfs(nx, ny);
	}
}

int solve() {
	int sum = 0;
	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			if (c[i][j] == 1) {
				dfs(j, i);
				++sum;
			}
		}
	}
	return sum;
}

int main() {
	while (1) {
		std::cin >> w >> h;
		if ((w + h) == 0)
			break;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				std::cin >> c[i][j];
		}
		std::cout << solve() << std::endl;
	}
	return 0;
}