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>

using namespace std;



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

int W, H;
int field[50][50];

void dfs(int field[50][50], int sx, int sy) {

    field[sy][sx] = 0;

    for (int i = 0; i < 8; i++) {
        int nx = sx + dx[i];
        int ny = sy + dy[i];

        if (0 <= nx && nx < W && 0 <= ny && ny < H && field[ny][nx] == 1) {
            dfs(field, nx, ny);
        }
    }
}

int main(void) {
    
    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 >> field[i][j];
            }
        }

        int count = 0;

        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                if (field[i][j] == 1) {
                    dfs(field, j, i);
                    count++;
                }
            }
        }

        cout << count << endl;
    }

    return 0;
}

C++: #define	_USE_MATH_DEFINES
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
#include <list>
using namespace std;
int x,y;
int o[51][51];
queue<int> q;
void tansaku(int j,int i){
	if(j<1||j>x||i<1||i>y||o[i][j]==0||o[i][j]==2){
		return ;
	}
	if(o[i][j]==1){
		if(q.empty())
		q.push(1);
		o[i][j]=2;
	}
	
	tansaku(j+1,i);
	tansaku(j-1,i);
	tansaku(j,i+1);
	tansaku(j,i-1);
	tansaku(j+1,i+1);
	tansaku(j-1,i-1);
	tansaku(j+1,i-1);
	tansaku(j-1,i+1);
}
int main(){

	while(cin>>x>>y,x!=0||y!=0){
		int count=0;
		for(int i=1;i<=y;i++){
			for(int j=1;j<=x;j++){
				cin>>o[i][j];
			}
		}
		for(int i=1;i<=y;i++){
			for(int j=1;j<=x;j++){
				tansaku(j,i);
				if(!q.empty()){
				count+=q.front();
				}
				q.pop();
			}
		}
		
		cout<<count<<endl;
	}

}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<bitset>
using namespace std;
const int inf = 1001001000;

void dfs(vector<vector<int>> &graph, int v, set<int> &seen){
	seen.insert(v);
	for (int i=0;i<graph[v].size();i++){
		int nxt = graph[v][i];
		if (seen.count(nxt)) continue;
		dfs(graph, nxt, seen);
	}
}

void print(vector<vector<int>> a){
	for (int i=0;i<a.size(); i++){
		if (a[i].size() == 0) continue;
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

int main(){
	while(true){
		int w,h; cin >> w >> h;
		if (w == 0 && h == 0) break;
		vector<vector<int>> map(h+2,vector<int>(w+2,0));
		for (int i=1;i<h+1;i++){
			for (int j=1;j<w+1;j++){
				int c; cin >> c;
				map[i][j] = c;
			}
		}
		vector<vector<int>> graph;
		int r = h+2,c=w+2;
		for (int i = 0; i < r; i++){
			for (int j = 0; j < c;j++){
				vector<int> a;
				if (map[i][j] == 1){	
					for (int k = -1; k < 2; k++){
						for (int l = -1; l < 2; l++){
							if (map[i+k][j+l] == 1) a.push_back((i+k)*c+j+l);
						}
					}
					
				}
				graph.push_back(a);
			}
		}
		set<int> gseen;
		int ans = 0;
		for (int i = 0; i < graph.size(); i++){
			set<int> seen;
			if (graph[i].size() == 0) continue;
			if (gseen.find(i) == gseen.end()){
				dfs(graph,i,seen);
				ans++;
				set_union(seen.begin(),seen.end(),gseen.begin(),gseen.end(),inserter(gseen,gseen.end()));
			}
			
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>

#define INF INT_MAX
#define ll long long

using namespace std;

int main(){
	while(true){
		int w,h;
		cin >> w >> h;
		if(w == 0 && h == 0)
			break;
		int c[h][w];
		//??\???
		for(int j = 0;j < h;j++){
			for(int i = 0;i < w;i++){
				cin >> c[j][i];
			}
		}

		//??????????????°????????????
		int ans = 0;
		for(int j = 0;j < h;j++){
			for(int i = 0;i < w;i++){
				if(c[j][i] == 1){
					ans++;
					vector< pair<int,int> > v;
					v.push_back(make_pair(j,i));
					while(!v.empty()){
						int y = v.back().first, x = v.back().second;
						c[y][x] = 0;	//????????¶????????????
						v.pop_back();
						for(int dirY = -1;dirY < 2;dirY++){
							for(int dirX = -1;dirX < 2;dirX++){
								int yy = y + dirY,xx = x + dirX;
								if((0 <= yy && yy < h) && (0 <= xx && xx < w) && (c[yy][xx] == 1)){
									v.push_back(make_pair(yy,xx));
								}
							}
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

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

vvi t;
int W,H;

void dfs(int x,int y){
    t[x][y]='0';
    for(int i=0;i<8;i++){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(nx>=0 && nx<H && ny>=0 && ny<W && t[nx][ny]==1) dfs(nx,ny);
    }
    return;
}

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

        t.resize(H);
        for(int i=0;i<H;i++) t[i].resize(W);

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

        int cnt=0;

        for(int i=0;i<H;i++) for(int j=0;j<W;j++) if(t[i][j]==1){
            cnt++;
            dfs(i,j);
        }
        cout << cnt << endl;
    }
}

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

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
int field[51][51];
int h, w;

void sol(int x, int y)
{
  field[x][y] = 0;
  for (int i = 0; i < 8; i++){
    if ((0 <= x + dx[i] && x + dx[i] < h) || (0 <= y + dy[i] && y + dy[i] < w))
    if (field[x + dx[i]][y + dy[i]] == 1) sol(x + dx[i], y + dy[i]);
  }
}

int main()
{

  while (cin >> h >> w, h || w){
    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 < h; i++){
      for (int j = 0; j < w; j++){
        if (field[i][j] == 1){
          res++;
          sol(i, j);
        }
      }
    }
    cout << res << endl;
  }
}