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 (ll i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)
using namespace std;

using ll = long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const int INF = 1001001001;
const long long INFLL = LLONG_MAX / 3;
const double eps = (1e-9);

int dh[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dw[] = {-1, 0, 1, -1, 1, -1, 0, 1};

void dfs(const vector<vector<int>>& c, vector<vector<bool>>& visited, int h, int w) {
  visited[h][w] = true;
  rep(i, 8) {
    int nh = h + dh[i];
    int nw = w + dw[i];
    if (nh < 0 || nh >= c.size() || nw < 0 || nw >= c[0].size()) continue;
    if (visited[nh][nw]) continue;
    if (c[nh][nw] == 0) continue;
    dfs(c, visited, nh, nw);
  }
}

bool solve() {
  int w, h; cin >> w >> h;
  if (w == 0 && h == 0) return false;
  vector<vector<int>> c(h, vector<int>(w));
  rep(i, h) rep(j, w) cin >> c[i][j];
  vector<vector<bool>> visited(h, vector<bool>(w, false));

  int ans = 0;
  rep(i, h) rep(j, w) {
    if (visited[i][j]) continue;
    if (c[i][j] == 0) continue;
    dfs(c, visited, i, j);
    ans++;
  }
  cout << ans << endl;
  return true;
}

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

  while (solve()) {}

  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int dp[50][50];
int main(){
	int w,h;
	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>>dp[i][j];
	int count=0;
	while(true){
		bool t=true;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(dp[i][j]!=0)
					t=false;
			}
		}
		if(t==true)
			break;
		int x=0,y=0;
		while(true){
			x=0;
			while(true){
				if(dp[y][x]==1){
					t=true;
					break;
				}
				x++;
				if(x==w)
					break;
			}
			if(t==true)
				break;
			y++;
		}
		for(int f=max(0,y-1);f<=min(h-1,y+1);f++){
			for(int g=max(0,x-1);g<=min(w-1,x+1);g++){
				if(dp[f][g]==1)
				    dp[f][g]=2;
			}
		}
		dp[y][x]=0;
		while(true){
			t=true;
			for(int i=0;i<h;i++){
				for(int j=0;j<w;j++){
					if(dp[i][j]==2){
						for(int f=max(0,i-1);f<=min(h-1,i+1);f++){
			                for(int g=max(0,j-1);g<=min(w-1,j+1);g++){
								if(dp[f][g]==1)
				                    dp[f][g]=2;
			                }
						}
						dp[i][j]=0;
						t=false;
					}
				}
			}
			if(t==true)
				break;
		}
		count++;
	}
	cout<<count<<endl;
	}
	return 0;
}

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

int MAP(vector<vector<int>> &m, int i, int j){
	int w = m[0].size();
	int h = m.size();

	if(!(0 <= i && i < h && 0 <= j && j < w)){
		return 0;
	}

	if(m[i][j] == 0){
		return 0;
	}
	else if(m[i][j] = 1){
		m[i][j] = 0;
		MAP(m, i - 1, j - 1);
		MAP(m, i, j - 1);
		MAP(m, i + 1, j - 1);
		MAP(m, i - 1, j);
		MAP(m, i + 1, j);
		MAP(m, i - 1, j + 1);
		MAP(m, i, j + 1);
		MAP(m, i + 1, j + 1);
	}
	return 1;
}

int main(){
	int w, h;

	while(true){
		int count = 0;
		cin >> w >> h;

		if(w == 0 && h == 0){
			break;
		}

		vector<vector<int> > map(h, vector<int>(w, 0));

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

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				count += MAP(map, i , j);
			}
		}

		cout << count << endl;
	}
}

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

using namespace std;
using vec = vector<int>;
using mat = vector<vec>;

void init(vec & parent)
{
	for (int i = 0; i < parent.size(); ++i)
	{
		parent[i] = i;
	}
}

int find(vec & parent, int x)
{
	if (parent[x] == x)return x;
	return parent[x] = find(parent, parent[x]);
}

void unite(vec & parent, int x, int y)
{
	x = find(parent, x);
	y = find(parent, y);
	if (x == y)return;
	parent[y] = x;
}

int change(int y, int x, int W)
{
	return y*W + x;
}


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

		if (w == 0 && h == 0)return 0;

		mat c(h, vec(w));
		vec parent(h*w);

		init(parent);

		for (int y = 0; y < h; ++y)
		{
			for (int x = 0; x < w; ++x)
			{
				cin >> c[y][x];

				if (x != 0 && c[y][x] == c[y][x - 1])
				{
					unite(parent, change(y, x, w), change(y, x - 1, w));
				}
				if (y != 0 && c[y][x] == c[y - 1][x])
				{
					unite(parent, change(y, x, w), change(y - 1, x, w));
				}
				if (x != 0 && y != 0 && c[y][x] == c[y - 1][x - 1])
				{
					unite(parent, change(y, x, w), change(y - 1, x - 1, w));
				}
				if (x != w - 1 && y != 0 && c[y][x] == c[y - 1][x + 1])
				{
					unite(parent, change(y, x, w), change(y - 1, x + 1, w));
				}
			}
		}

		int ans = 0;
		for (int y = 0; y < h; ++y)
		{
			for (int x = 0; x < w; ++x)
			{
				int i = change(y, x, w);
				ans += parent[i] == i && c[y][x] == 1;
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
//入力
int H,W;
vector<vector<int>> field;

void dfs(int h,int w){
  field[h][w] = 0;
  // 八方向を探索
  for(int dh=-1;dh<=1;dh++){
    for(int dw=-1;dw<=1;dw++){
      int nh = h+dh,nw=w+dw;
      //場外アウトしたり、0だったりはスルー
      if(nh<0||nh>=H||nw<0||nw>=W) continue;
      if(field[nh][nw]==0) continue;
      dfs(nh,nw);//再帰的に探索
    }
  }
}

int main(){
  //入力受け取り
  while(cin >> W >> H){
    if(H==0||W==0) break;
    field.assign(H,vector<int>(W,0));
    for(int h=0;h<H;h++)for(int w=0;w<W;w++) cin >> field[h][w];
    int count = 0;
    for(int h=0;h<H;h++){
      for(int w=0;w<W;w++){
        if(field[h][w]==0) continue;//残島じゃなかったらスルー
        dfs(h,w);
        ++count;
      }
    }
    cout << count << endl;
  }
}