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

int m[51][51];
bool v[51][51];
int h,w,c=0;
int dx[8]={-1,-1,0,1,1,1,0,-1};
int dy[8]={0,-1,-1,-1,0,1,1,1};
void dfs(int x,int y){
    if(v[y][x])return;
    for(int i=0;i<8;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx>=0&&nx<w&&ny>=0&&ny<h&&!v[y][x]&&m[ny][nx]){
            dfs(nx,ny);
        }
    }
}
int main(){
    cin>>h>>w;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>m[i][j];
            v[i][j]=0;
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(!v[i][j]&&m[i][j]){
                dfs(i,j);
                c++;
            }
        }
    }
    cout<<c<<endl;
}

C++: #include <iostream>
#include <string.h>

using namespace std;

int c[50][50];
bool visit[50][50];
const int dx[8] = {1,0,-1,0,1,1,-1,-1};
const int dy[8] = {0,1,0,-1,1,-1,1,-1};
int w,h;

void dfs(int x,int y)
{
	int tmp_x,tmp_y;
	for (int i=0;i<8;++i)
	{
		tmp_x = x + dx[i];
		tmp_y = y + dy[i];
		
		if (0<=tmp_x&&tmp_x<w&&0<=tmp_y&&tmp_y<h&&!visit[tmp_y][tmp_x]&&c[tmp_y][tmp_x] == 1)
		{
			visit[tmp_y][tmp_x] = true;
			dfs(tmp_x,tmp_y);
		}
	}
	return;
}

int main()
{

	int count;
	cin >> w >> h;
	
	while (w+h >0)
	{
		count = 0;
		memset(visit,0,sizeof(visit));
		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 (!visit[i][j]&&c[i][j]==1)
				{
					++count;
					dfs(j,i);
				}
			}
		}
		
		cout << count <<endl;
		
		cin >> w >> h;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> pl;
typedef vector<pl> vp;
#define fore(i,a,b) for(ll i=(a);i<=(b);++i)
#define rep(i,n) fore(i,0,(n)-1)
#define rfore(i,a,b) for(ll i=(b);i>=(a);--i)
#define rrep(i,n) rfore(i,0,(n)-1)
#define all(x) (x).begin(),(x).end()
const ll INF=1001001001;
const ll LINF=1001001001001001001;
const ll D4[]={0,1,0,-1,0};
const ll D8[]={0,1,1,0,-1,-1,1,-1,0};
template<class T>
bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>
bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
template<class T>
ll sum(const T& a){return accumulate(all(a),0LL);}

ll w,h;
ll c[55][55];
bool u[55][55];
void dfs(ll y,ll x){
    u[y][x]=1;
    rep(i,8){
        ll ny=y+D8[i];
        ll nx=x+D8[i+1];
        if(ny<0||h<=ny||nx<0||w<=nx||!c[ny][nx]||u[ny][nx])continue;
        dfs(ny,nx);
    }
}
void solve(){
    while(1){
        cin>>w>>h;
        if(w==0&&h==0)return;
        rep(i,h)rep(j,w){
            cin>>c[i][j];
            u[i][j]=0;
        }
        ll cnt=0;
        rep(i,h)rep(j,w){
            if(c[i][j]&&!u[i][j]){
                cnt++;
                dfs(i,j);
            }
        }
        cout<<cnt<<endl;
    }
}

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