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 <stdlib.h>

using namespace std;

int d[8][2] = {
	{-1, -1},
	{0, -1},
	{1, -1},
	{-1, 0},
	{1, 0},
	{-1, 1},
	{0, 1},
	{1, 1}
};

void func(int **a, int s, int t, int i, int j)
{
	if(a[i][j] == 0){
		return ;
	}
	a[i][j] = 0;
	
	for(int k = 0; k < 8; k++){
		if((i+d[k][0] >= 0 && i+d[k][0] < t) && (j+d[k][1] >= 0 && j+d[k][1] < s)){
			func(a, s, t, i+d[k][0], j+d[k][1]);
		}
	}
	
	return ;
}

int main(int argc, char **argv)
{
	int s, t, **a, c, t2, s2;
	
	while(1){
		cin >> s >> t;
		if(s == 0 && t == 0){
			break;
		} else if(s == 1 || t == 1){
			t2 = t+1;
			s2 = s+1;
		} else {
			t2 = t;
			s2 = s;
		}
		a = (int **)calloc(t2, sizeof(int));
		for(int i = 0; i < t; i++){
			a[i] = (int *)calloc(s2, sizeof(int));
			for(int j = 0; j < s; j++){
				cin >> c;
				a[i][j] = c;
			}
		}
		c = 0;
		
		for(int i = 0; i < t; i++){
			for(int j = 0; j < s; j++){
				if(a[i][j] == 1){
					c++;
					func(a, s, t, i, j);
				}
			}
		}
		cout << c << endl;
		
		for(int i = 0; i < s2; i++){
			free(a[i]);
		}
		free(a);
	}
	
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;


int w,h;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

void dfs(vector<vector<int> > &map,int x,int y){
    /*for(int i = 0;i < h; i++){
        for(int j = 0; j < w; j++){
            cout << map[i][j] << " " ;
        }
        cout << endl;
    }*/
    //今いるところを-100に置き換える
    map[x][y] = -100;
    //移動方向をループ
    for(int dx = -1;dx <= 1;dx++){
        for(int dy = -1;dy <= 1;dy++){
            //x方向にdx,ｙ方向にdy移動した場所を(nx,ny)とする
            int nx = x + dx, ny = y + dy;
            if(0 <= nx && nx < h && 0 <= ny && ny < w && map[nx][ny] == 1){
                dfs(map,nx,ny);
            }
        }
    }
    return;
}

int solve(vector<vector<int> > &map,int h,int w){

    /*for(int i = 0;i < h; i++){
        for(int j = 0; j < w; j++){
            cout << map[i][j] << " " ;
        }
        cout << endl;
    }*/

    int ans = 0;
    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            if(map[i][j] == 1){
                //Wが残っているならばその場所からdfsを始める
                dfs(map,i,j);
                //ここで1つの水たまりが見つかったことになるので水たまりの個数を１つ足す
                ans++;
            }
        }
    }
    return ans;
}

int main(){

    
    

    while(cin >> w >> h){

        if(w == 0 && h == 0){
            break;
        }
        int ans = 0;
        vector<vector<int> > map(h,vector<int>(w));//周波数ベクトル列データ用の2次元配列

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

        ans = solve(map,h,w);

        cout << ans << endl;
    }

   


    

    return 0;
}

C++: #include<iostream>
#include<cstring>
using namespace std;
bool used[55][55];
int n,m;

void check(int x,int y){
	if(x<0 || n<=x || y<0 || m<=y)return;
	if(used[x][y])return;
	used[x][y]=true;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			check(x+i,y+j);
		}
	}
}

int main(){
	int i,j,k;
	int a,b,c;
	while(cin>>m>>n){
		if(!n)break;
		memset(used,false,sizeof(used));
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				cin>>a;
				if(a==0)used[i][j]=true;
			}
		}
		a=0;
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				if(!used[i][j])check(i,j),a++;
			}
		}
		cout<<a<<endl;
	}
}

C++: //
// Created by elegant-belltree on 15/06/03.
//

#include <iostream>
#include <set>

using namespace std;

int main(){
    int w,h;
    int c[50][50];

    while (cin>>w>>h && (w>0 || h>0)) {
        int label=2;
        set<int> labels;

        for (int n = 0; n < 2; ++n) {
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    if(n==0) {
                        cin >> c[j][i];
                    }
                    if (0<c[j][i]) {
                        for (int k = -1; k < n+1 && i+k<h; ++k) {
                            for (int l = -1; (n!=0 || k!=0 || l!=0) && l < 2 && j + l < w; ++l) {
                                if (-1 < i + k && -1 < j + l && 0 < c[j + l][i + k] && (c[j][i] == 1 || c[j + l][i + k] < c[j][i])) {
                                    if(n==1){
                                        labels.erase(c[j][i]);
                                    }
                                    c[j][i] = c[j + l][i + k];
                                }
                            }
                        }
                        if (c[j][i] == 1) {
                            c[j][i] = label++;
                            labels.insert(c[j][i]);
                        }
                    }
                }
            }
        }
        /*for (int m = 0; m < h; ++m) {
            for (int i = 0; i < w; ++i) {
                cout<<c[i][m]<<" ";
            }
            cout<<endl;
        }*/
        cout<<labels.size()<<endl;
    }
    return 0;
}

C++: 
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <ctime>
#include <string>
#include <map>
#include <stack>
#include <queue>

using namespace std;

int w, h;
const int MAX = 50;
int c[MAX][MAX];


void check(int x, int y){
	c[x][y] = 0;
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            if(x + dx >= 0 && x + dx < h  && y + dy >= 0 && y + dy < w && c[x + dx][y + dy] == 1){
            	check(x + dx, y + dy);
            } 
        }
    }
	
}

int main(void){

	for(int i = 0; i < MAX ; i++){
		for(int j = 0; j < MAX; j++){
			c[i][j] = 0;
		}
	}

	cin >> w >> h;
	while(w != 0 && h != 0){
		int ans = 0;
		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				cin >> c[i][j];
			}
		}

		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				if(c[i][j] == 1){
					check(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;

		cin >> w >> h;
	}
	
    return 0;
}