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 <vector>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
int group[55][55];
int parent(int n){
	int i=n/55,j=n%55;
	if(group[i][j]==-1)	return -1;
	return group[i][j]=(group[i][j]==n?group[i][j]:parent(group[i][j]));
}
void merge(int s,int t){
	int si=s/55,sj=s%55;
	s=parent(group[si][sj]);
	int ti=t/55,tj=t%55;
	t=parent(group[ti][tj]);
	group[t/55][t%55]=s;
}
bool isInside(int i,int j,int w,int h){
	if(i<0||h<=i)	return false;
	if(j<0||w<=j)	return false;
	return true;
}
const int dx[]={1,0,-1};
const int dy[]={1,0,-1};
int main(){
	while(true){
		int w,h;	cin>>w>>h;
		if(w==0)	break;
		for(int i=0;i<h;i++)	for(int j=0;j<w;j++)	group[i][j]=-1;
		for(int i=0;i<h;i++)	for(int j=0;j<w;j++){
			int c;	cin>>c;
			if(c==1)	group[i][j]=55*i+j;
		}
		for(int i=0;i<h;i++)	for(int j=0;j<w;j++){
			if(group[i][j]>=0){
				for(int x=0;x<3;x++)	for(int y=0;y<3;y++){
					int ni=i+dy[y],nj=j+dx[x];
					if(!isInside(ni,nj,w,h))	continue;
					if(group[ni][nj]>=0)	merge(55*i+j,55*ni+nj);
				}
			}
		}
		set<int> ret;
		for(int i=0;i<h;i++)	for(int j=0;j<w;j++)	ret.insert(parent(group[i][j]));
		ret.erase(-1);
		cout<<ret.size()<<endl;
	}
	return 0;
}

C++: #include <utility>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;
int cnt;
int t[3]={-1,0,1};
    int map[55][55];
void dfs(int h,int w){
    if(map[h][w]==1){
        map[h][w]=0;
        for(int i=0;i<3;i++){for(int j=0;j<3;j++)dfs(h+t[i],w+t[j]);}
       }
}
int main()
{
    int w,h;

    while(cin>>w>>h,w+h){cnt=0;
        for(int i=0;i<55;i++){for(int j=0;j<55;j++)map[i][j]=0;}
        for(int i=0;i<h;i++){for(int j=0;j<w;j++)scanf("%d",&map[i+1][j+1]);}
        for(int i=0;i<53;i++){for(int j=0;j<53;j++){
            if(map[i][j]==1)cnt++;
            dfs(i,j);}}
        cout<<cnt<<endl;}
    return 0;
}

C++: #include <bits/stdc++.h>
#ifdef _DEBUG
#include "../../../debug_print.hpp"
#else
#define debug(...)
#endif
#define sort_(a) sort(a.begin(), a.end())
#define rsort(a) sort(a.rbegin(), a.rend())
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; 0 <= i; i--)
#define for_(i, start, end) for (int i = start; i < end; i++)
#define rfor(i, start, end) for (int i = start - 1; 0 <= i; i--)
#define all(a) a.begin(), a.end()
using ll = long long;
using ld = long double;
using namespace std;
constexpr int INF32       = 1'050'000'000;
constexpr long long INF64 = 4'000'000'000'000'000'000;
constexpr int MOD7        = 1'000'000'007;
constexpr int MOD53       = 998'244'353;
constexpr double PI       = 3.14159265358979323846;
template <class T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
void print() { std::cout << '\n'; }
template <class H, class... T> void print(H &&head, T &&... args) {
    std::cout << head;
    sizeof...(args) == 0 ? std::cout << "" : std::cout << ' ';
    print(std::forward<T>(args)...);
}
template <class T> void print(std::vector<T> &v) {
    for (int i = 0; i < v.size(); i++) {
        std::cout << v[i];
        i == v.size() - 1 ? std::cout << '\n' : std::cout << ' ';
    }
}
template <class T> void print(std::vector<std::vector<T>> &v) {
    for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j < v[i].size(); j++) {
            std::cout << v[i][j];
            j == v[i].size() - 1 ? std::cout << '\n' : std::cout << ' ';
        }
    }
}
void scan() {}
template <class H, class... T> void scan(H &&head, T &&... args) {
    std::cin >> head;
    scan(std::forward<T>(args)...);
}
template <class T> void scan(std::vector<T> &v) {
    for (auto &&i : v) {
        scan(i);
    }
}

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

    while (true) {
        int w, h;
        scan(w, h);
        if (w == 0 && h == 0)
            break;
        vector<vector<int>> mp(h, vector<int>(w));
        scan(mp);
        int ans        = 0;
        vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
        vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};
        auto exist     = [](int H, int W, int nx, int ny) -> bool {
            if (nx < 0 || W <= nx || ny < 0 || H <= ny)
                return false;
            else
                return true;
        };
        auto dfs = [&](auto f, int y, int x) -> void {
            mp[y][x] = 0;
            rep(i, dx.size()) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (exist(h, w, nx, ny) && mp[ny][nx] == 1) {
                    f(f, ny, nx);
                }
            }
            return;
        };
        rep(y, h) {
            rep(x, w) {
                if (mp[y][x] == 1) {
                    dfs(dfs, y, x);
                    ans++;
                }
            }
        }
        print(ans);
    }

    return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;
 
int c[64][64];
 
void paint(int y, int x) {
  if (y < 0 || y >= 50 || x < 0 || x >= 50) return;
  if (c[y][x] == 0) return;
  c[y][x] = 0;
  for (int dy = -1; dy <= 1; ++dy) {
    for (int dx = -1; dx <= 1; ++dx) {
      paint(y + dy, x + dx);
    }
  }
}
 
int main() {
  int H, W;
  while (cin >> W >> H, H) {
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> c[i][j];
      }
    }
    int res = 0;
    for (int y = 0; y < H; ++y) {
      for (int x = 0; x < W; ++x) {
        if (c[y][x] == 1) {
          ++res;
          paint(y, x);
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}