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 <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <limits>
#include <cmath>
#include <cassert>
#include <queue>

using namespace std;
using ll = long long;

const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
const int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
#define debug(x) cout << #x << ": " << x << endl

int W, H;

void bfs(int y, int x, const vector<vector<int>> &table, vector<vector<int>> &flag)
{
    using P = pair<int, int>;
    queue<P> que;
    que.push(P(y, x));
    flag[y][x] = 0;
    while(que.size())
    {
        P p = que.front(); que.pop();
        for(int i = 0; i < 8; i++)
        {
            int ny = p.first + dy[i];
            int nx = p.second + dx[i];
            if(ny < 0 or nx < 0 or ny >= H or nx >= W) continue;
            if(table[ny][nx] == 0 or not flag[ny][nx]) continue;
            flag[ny][nx] = 0;
            que.push(P(ny, nx));
        }
    }
}

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> W >> H, W or H)
    {
        vector<vector<int>> table(H, vector<int>(W));
        for(int i = 0; i < H; i++)
        {
            for(int j = 0; j < W; j++) cin >> table[i][j];
        }
        vector<vector<int>> flag = table;
        int ans = 0;
        for(int i = 0; i < H; i++)
        {
            for(int j = 0; j < W; j++)
            {
                if(not flag[i][j]) continue;
                ans++;
                bfs(i, j, table, flag);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
char field[51][51];
int W, H;

void dfs(int x, int y) {
  //cout << "field[x][y] = " << field[x][y] << endl;
  field[x][y] = '0';
  
  for(int dx = -1 ; dx <= 1 ; dx++) {
    for(int dy = -1 ; dy <= 1 ; dy++) {
      int nx = x + dx, ny = y + dy;
      if(0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == '1') dfs(nx, ny);
    }
  }
}

int main(void) {
  while(cin >> W >> H, W != 0 && H != 0) {
    int res = 0;
    for(int i = 0 ; i < H ; i++)
      for(int j = 0 ; j < W ; j++) cin >> field[i][j];
    
    for(int i = 0 ; i < W ; i++) {
      for(int j = 0 ; j < H ; j++) {
	if(field[i][j] == '1') {
	  dfs(i, j);
	  res++;
	  /*cout << endl;
	  for(int b = 0 ; b < H ; b++) {
	    for(int a = 0 ; a < W ; a++) {
	      cout << field[b][a] << ' ';
	    }
	    cout << endl;
	  }
	  cout << endl;*/
	}
      }
    }
    cout << res << endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

typedef pair<int, int> P;

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

    int w,h;
    int dx[8]={1,1,1,0,-1,-1,-1,0},dy[8]={-1,0,1,1,1,0,-1,-1};
    while(cin>>w>>h,w||h){
        vector< vector<int> > field(h,vector<int>(w));
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>field[i][j];
            }
        }
        int res=0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(field[i][j]){
                    //cout<<i<<" "<<j<<endl;
                    queue<P> q;
                    field[i][j]=0;
                    q.push(P(i,j));
                    while(!q.empty()){
                        P p=q.front();q.pop();
                        for(int k=0;k<8;k++){
                            int nx=p.first+dx[k],ny=p.second+dy[k];
                            if(nx<0||h<=nx||ny<0||w<=ny) continue;
                            if(field[nx][ny]==0) continue;
                            field[nx][ny]=0;
                            q.push(P(nx,ny));
                        }
                    }
                    res++;
                }
            }
        }
        cout<<res<<endl;
    }
}

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

vector<vector<int>>tisei;
int tate, yoko;
void shima(int a, int b) {
	tisei[a][b] = 0;
	for (int c = a-1; c < a+2; c++) {
		for (int d = b - 1; d < b + 2; d++) {
			if (c > -1 && d > -1 && c< tate&d < yoko&&tisei[c][d])shima(c, d);
		}
	}
}
int main() {
	int a, b;
	while (cin >> a >> b, a | b) {
		tate = b;
		yoko = a;
		tisei.clear();
		for (int c = 0; c < b; c++) {
			vector<int>o(a);
			for (int e = 0; e < a; e++) {
				cin >> o[e];
			}
			tisei.push_back(o);
		}
		int s = 0;
		for (int g = 0; g < b; g++) {
			for (int h = 0; h < a; h++) {
				if (tisei[g][h]) { s++;  shima(g, h); }
			}
		}
		cout << s << endl;
	}
}

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

using namespace std;

	ifstream fin;
	ofstream fout;

	//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

	int gomi = 0;

	int a[52][52] = {};
	int check(int, int);

	int main(){

		fin.open("input.txt");
		fout.open("output.txt");

		//		int w = 0, h = 0;
		int width = 0, height = 0;
		int count = 0;


		while (1){
			for (int i = 0; i < 50; i++)
				for (int j = 0; j < 50; j++)
					a[i + 1][j + 1] = 0;

			fin >> width >> height;
			if (width == 0 && height == 0)break;

			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					//初期化
					gomi = a[i + 1][j + 1] = 0;

					fin >> a[i + 1][j + 1];
				}
			}

			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					if (a[i + 1][j + 1] == 1){
						check(i + 1, j + 1);
//						a[i + 1][j + 1] = 1;
						count++;
//						cout << i + 1 << "  " << j + 1 << endl;
					}
				}
			}
			cout << count << endl;
			fout << count << endl;

			//初期化
			count = 0;
			for (int i = 0; i < height; i++){
				for (int j = 0; j < width; j++){
					a[i + 1][j + 1] = 0;
				}
			}
		}
	}

	int check(int y, int x){
		for (int i = -1; i <= 1; i++){
			for (int j = -1; j <= 1; j++){
				if (i + j != 0 || i - j != 0){
					if (a[y + i][x + j] == 1){
						a[y + i][x + j] = 0;
						check(y + i, x + j);
					}
				}
			}
		}
		return 0;
	}

/*	int check(int y, int x){
		if (a[y + 1][x] == 1){
			a[y + 1][x] = 0;
			check(y + 1, x);
		}
		//-----------------------------
		if (a[y][x + 1] == 1){
			a[y][x + 1] = 0;
			check(y, x + 1);
		}
		//-----------------------------
		if (a[y - 1][x] == 1){
			a[y - 1][x] = 0;
			check(y - 1, x);
		}
		//-----------------------------
		if (a[y][x - 1] == 1){
			a[y][x - 1] = 0;
			check(y, x - 1);
		}
		//-----------------------------
		if (a[y + 1][x + 1] == 1){
			a[y + 1][x + 1] = 0;
			check(y + 1, x + 1);
		}
		//-----------------------------
		if (a[y + 1][x - 1] == 1){
			a[y + 1][x - 1] = 0;
			check(y + 1, x - 1);
		}
		//-----------------------------
		if (a[y - 1][x + 1] == 1){
			a[y - 1][x + 1] = 0;
			check(y - 1, x + 1);
		}
		//-----------------------------
		if (a[y - 1][x - 1] == 1){
			a[y - 1][x - 1] = 0;
			check(y - 1, x - 1);
		}
		//-----------------------------
		return 0;
	}*/