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>
using namespace std;

int w;
int h;
char field[1145][1419];
bool used[1145][1419];

void dfs(int y, int x) {
  if (y < 0 || h <= y) return;
  if (x < 0 || w <= x) return;
  if (field[y][x] == '0') return;
  if (used[y][x]) return;
  used[y][x] = true;

  for (int dy=-1; dy<=1; dy++) {
    for (int dx=-1; dx<=1; dx++) {
      if (dy == 0 && dx == 0) continue;
      int newy = y + dy;
      int newx = x + dx;
      dfs(newy, newx);
    }
  }
}

int main() {
  while (1) {
    if (scanf("%d%d", &w, &h) != 2) return 0;
    if (w == 0 && h == 0) return 0;

    for (int i=0; i<h; i++) {
      for (int j=0; j<w; j++) {
        scanf(" %c", &field[i][j]);
        used[i][j] = false;
      }
    }

    int ans = 0;
    for (int i=0; i<h; i++) {
      for (int j=0; j<w; j++) {
        if (!used[i][j] && field[i][j] == '1') {
          dfs(i, j);
          ans++;
        }
      }
    }
    printf("%d\n", ans);
  }
}

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

#define rep(i,n) for(i = 0;i < n;++i)
#define all(v) v.begin(), v.end()
typedef long long ll;

vector<vector<ll>> tizu(50,vector<ll>(50));
vector<vector<bool>> visit(50,vector<bool>(50, false));
ll h,w;

void dfs(pair<ll,ll> v)
{
    ll a = v.first;
    ll b = v.second;
    if(!tizu.at(a).at(b)){
        return;
    }
    visit.at(a).at(b) = true;
    ll tate[9] = {-1,0,1,-1,0,1,-1,0,1};
    ll yoko[9] = {-1,-1,-1,0,0,0,1,1,1};
    ll i,j;
    for(i = 0;i < 9;++i){
        ll p = a + tate[i];
        ll q = b + yoko[i];
        if(p < 0 || p >= h){
            continue;
        }else if (q < 0 || q >= w){
            continue;
        }else if(!visit.at(p).at(q) && tizu.at(p).at(q)){
            dfs(make_pair(p,q));
        }
    }
    return;
}

int main()
{
    ll i,j;
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0){
            return 0;
        }
        for(i = 0;i < h;++i){
            rep(j,w){
                cin >> tizu.at(i).at(j);
            }
        }
        for(i = 0;i < h;++i){
            rep(j,w){
                visit.at(i).at(j) = false;
            }
        }
        ll ans = 0;
        for(i = 0;i < h;++i){
            rep(j,w){
                if(visit.at(i).at(j) || !tizu.at(i).at(j)){
                    continue;
                }else{
                    dfs(make_pair(i,j));
                    ++ans;
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

int c[51][51];
bool used[51][51];
const int dx[]={-1,-1,-1,0,0,1,1,1},dy[]={1,0,-1,1,-1,1,0,-1};
int main(){
    int w,h;
    while(cin>>w>>h,w|h){
        int cnt=0;
        memset(used,0,sizeof(used));
        rep(i,h)rep(j,w) cin>>c[i][j];
        rep(i,h)rep(j,w)if(!used[i][j]&&c[i][j]==1){
            ++cnt;
        queue<pair<pint,int> > q;
        q.push({{j,i},0});
        used[i][j]=true;
        while(!q.empty()){
            pair<pint,int> pi=q.front();q.pop();
            rep(k,8){
                int tx=pi.first.first+dx[k],ty=pi.first.second+dy[k];
                if(tx>=0&&ty>=0&&tx<w&&ty<h&&c[ty][tx]==1&&!used[ty][tx]){
                    q.push({{tx,ty},pi.second+1});
                    used[ty][tx]=true;
                }
            }
        }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <utility>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int main() 
{
	
	int w,h;
	while(cin >> w >> h && !(w==0&&h==0))
	{
		int a[50][50]={};
		REP(i,h){REP(j,w){cin >> a[j][i];}}
		
		int id = 10; //島のid 初期値は適当
		
		//w=0の時
		if(a[0][0]!=0){a[0][0]=id;}
		for(int i=1;i<h;i++){
			if(a[0][i]!=0 && a[0][i-1]==0){id++;a[0][i]=id;}
			else if(a[0][i]!=0){a[0][i]=a[0][i-1];}
		}
		//w>0の時
		for(int i=1;i<w;i++)
		{
			//h=0の時
			if(a[i][0]!=0){
				if(a[i-1][0]!=0 || a[i-1][1]!=0){a[i][0]=max(a[i-1][0],a[i-1][1]);}
				else{id++;a[i][0]=id;}
			}
			//h>0の時
			for(int j=1;j<h;j++)
			{
				if(a[i][j]!=0)
				{
					int tmp = max(a[i][j-1],a[i-1][j-1]);
					tmp = max(tmp,a[i-1][j]);
					tmp = max(tmp,a[i-1][j+1]);
					if(tmp==0){id++;a[i][j]=id;}
					else{
						a[i][j] = tmp;
						if(a[i][j-1]!=0 && a[i][j-1]!=tmp)REP(k,w){REP(l,h){if(a[k][l]==a[i][j-1])a[k][l]=tmp;}}
						if(a[i-1][j-1]!=0 && a[i-1][j-1]!=tmp)REP(k,w){REP(l,h){if(a[k][l]==a[i-1][j-1])a[k][l]=tmp;}}
						if(a[i-1][j]!=0 && a[i-1][j]!=tmp)REP(k,w){REP(l,h){if(a[k][l]==a[i-1][j])a[k][l]=tmp;}}
						if(a[i-1][j+1]!=0 && a[i-1][j+1]!=tmp)REP(k,w){REP(l,h){if(a[k][l]==a[i-1][j+1])a[k][l]=tmp;}}
					}
				}
			}
		}
		
		int b[2500]={};
		REP(i,w){REP(j,h){b[i+w*j]=a[i][j];}}
		sort(b,b+h*w);
		reverse(b,b+h*w);
		
		int ans(0);
		if(b[0]>1){ans++;}
		for(int i=1;i<w*h;i++){if(b[i]!=b[i-1] && b[i]>1)ans++;}
		
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<utility>
using namespace std;
void search(int, int);
int c[60][60] = {}, map[60][60] = {};
void mapDraw(int,int);
queue< pair<int, int> > q;
int w, h;

int main() {

	while (1) {
		
		//initialize
		int count = 0;
		for (int i = 0; i < 60; i++) {
			for (int j = 0; j < 60; j++) {
				c[i][j] = 0;
				map[i][j] = 0;
			}
		}


		cin >> w >> h;
		if (w == 0 && h == 0) break;
		//input
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[i][j];
			}
		}

		//search
		//unresearched:0, land:2, sea:1
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 0) {
					map[i][j] = c[i][j] + 1;
					if (c[i][j] == 1) {
						q.push(make_pair(i, j));
						count++;
					}
				}

				pair<int, int> cur = make_pair(i, j);
				while (!q.empty()) {
					cur = q.front();
					int x = cur.second;
					int y = cur.first;
					q.pop();

					//cerr << "( " << y << ", " << x << " )now"<< endl;

					for (int a = -1; a <= 1; a++) {
						for (int b = -1; b <= 1; b++) search(y + a, x + b);
					}
				}
			}
		}

		cout << count << endl;
	}


	return 0;
}

void search(int i, int j) {
	if (i < h&&i >= 0 && j < w&&j >= 0) {
		if (map[i][j] == 0) {
			map[i][j] = c[i][j] + 1;
			if (c[i][j] == 1) q.push(make_pair(i, j));
		}
	}
	//mapDraw(w, h);
}

void mapDraw(int w, int h) {
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cerr << map[i][j];
		}
		cerr << endl;
	}
	cerr << endl;
}