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;
typedef long long ll;
const double EPS=1e-10;
const ll INF=1000000000;
int w,h;
int c[60][60];
bool visited[60][60];
int dx[8]={0,1,0,-1,1,-1,1,-1};
int dy[8]={1,0,-1,0,1,1,-1,-1};
bool field(int x,int y){
    if(x>=0&&x<w&&y>=0&&y<h){
        return true;
    }
    return false;
}
int dfs(int x,int y){
    if(visited[y][x]){
        return 0;
    }
    visited[y][x]=true;
    for(int i=0;i<8;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(field(nx,ny)&&c[ny][nx]){
            dfs(nx,ny);
        }
    }
    return 1;
}
int main(){
    while(cin>>w>>h,w+h){
        memset(visited,false,sizeof(visited));
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>c[i][j];
            }
        }
        int ans=0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(c[i][j]){
                    ans+=dfs(j,i);
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include <cmath>
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<set>
#include<tuple>
#include<bitset>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;
int dx[8] = { 1,-1,0,0,-1,-1,1,1 };
int dy[8] = { 0,0,1,-1,-1,1,-1,1 };
int dif[55][55];
char c[55][55];
int h, w;
void dfs(P p) {
	queue<P>que;
	que.push(p);
	while (!que.empty()) {
		int nowy = que.front().first;
		int nowx = que.front().second;
		que.pop();
		for (int i = 0; i < 8; i++) {
			int newy = nowy + dy[i];
			int newx = nowx + dx[i];
			if (newy >= 0 && newy < h && newx >= 0 && newx < w && dif[newy][newx] == inf && c[newy][newx] == '1') {
				dif[newy][newx] = 1;
				que.push(make_pair(newy, newx));
			}
		}

	}
	
}

int main() {
	vector<ll>ans;
	while (1) {
		
		ll count = 0;
		cin >> w >> h;
		if (h == 0 && w == 0) {
			break;
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[i][j];
				dif[i][j] = inf;
			}
		}
		
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (dif[i][j] == inf&&c[i][j]=='1') {
					count++;
					dif[i][j] = 1;
					dfs(make_pair(i, j));
				}
			}
		}
		ans.push_back(count);
	}
	for (int i = 0; i < ans.size(); i++) {
		cout << ans[i] << endl;
	}
	return 0;
}

C++: #include<bits//stdc++.h>
using namespace std;
int sum;
void solve(bool a[50][50],int h, int w, int i, int j){
	for(int z=-1;z<2;z++){
		for(int x=-1;x<2;x++){
			if(i+z>=0&&i+z<h&&j+x>=0&&j+x<w&&a[i+z][j+x]){
				a[i+z][j+x]=false;
				solve(a,h,w,i+z,j+x);
			}
		}
	}
}
int main(){
	int h,w;
	while(cin>>w>>h&&h!=0&&w!=0){
		bool a[50][50];
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				int x;
				cin>>x;
				if(x==0) a[i][j]=false;
				else a[i][j]=true;
			}
		}
		sum=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(a[i][j]){
					sum++;
					solve(a,h,w,i,j);
				}
			}
		}
		cout<<sum<<endl;
	}
}

C++: #include <stdio.h>

void dps(int il[50][50], int y, int x)	{
	il[y][x] = 0;
	
	for (int dy = -1; dy <= 1; dy++)	{
		for (int dx = -1; dx <= 1; dx++)	{
			if (dy == 0 && dx == 0)	continue;
			if (y+dy < 0 || y+dy >= 50 || x+dx < 0 || x+dx >= 50)	continue;
			if (il[y+dy][x+dx] == 1)	dps(il, y+dy, x+dx);
		}
	}
}

int main()	{
	
	int w, h, cnt;
	int il[50][50];
	
	while (true)	{
		
		scanf("%d %d", &w, &h);
		if (w == 0)	break;
		
		cnt = 0;
		
		for (int y = 0; y < 50; y++)	{
			for (int x = 0; x < 50; x++)	{
				il[y][x] = 0;
			}
		}
		
		for (int y = 0; y < h; y++)	{
			for (int x = 0; x < w; x++)	{
				scanf("%d", &il[y][x]);
			}
		}
		
		for (int y = 0; y < h; y++)	{
			for (int x = 0; x < w; x++)	{
				if (il[y][x] == 1)	{
					dps(il, y, x);
					cnt++;
				}
			}
		}
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

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

int fill_land(int y,int x,int h,int w,vector<vector<int>> &c);

int main()
{

	for (;;)
	{
		int w,h;
		cin >> w >> h;
		if ((w==0)&&(h==0))
		{
			return 0;
		}

		vector<vector<int>> c(h,vector<int>(w));

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

		int count=0;
		for (int i=0;i<h;++i)
		{
			for (int j=0;j<w;++j)
			{
				if (c[i][j]!=0)
				{
					// land
					++count;
					c[i][j]=0;
					fill_land(i,j,h,w,c);
				}
			}
		}
		cout << count << endl;
	}
}

int fill_land(int y,int x,int h,int w,vector<vector<int>> &c)
{
	stack<pair<int,int>> st;

	st.push(make_pair(y,x));
	while (st.empty()==0)
	{
		auto p=st.top();
		st.pop();

		if ( (p.first>0) && (c[p.first-1][p.second]!=0) )
		{
			c[p.first-1][p.second]=0;
			st.push(make_pair(p.first-1,p.second));
		}
		if ( (p.first<h-1) && (c[p.first+1][p.second]!=0) )
		{
			c[p.first+1][p.second]=0;
			st.push(make_pair(p.first+1,p.second));
		}
		if ( (p.second>0) && (c[p.first][p.second-1]!=0) )
		{
			c[p.first][p.second-1]=0;
			st.push(make_pair(p.first,p.second-1));
		}
		if ( (p.second<w-1) && (c[p.first][p.second+1]!=0) )
		{
			c[p.first][p.second+1]=0;
			st.push(make_pair(p.first,p.second+1));
		}
		if ( (p.first>0) && (p.second>0) && (c[p.first-1][p.second-1]!=0) )
		{
			c[p.first-1][p.second-1]=0;
			st.push(make_pair(p.first-1,p.second-1));
		}
		if ( (p.first>0) && (p.second<w-1) && (c[p.first-1][p.second+1]!=0) )
		{
			c[p.first-1][p.second+1]=0;
			st.push(make_pair(p.first-1,p.second+1));
		}
		if ( (p.first<h-1) && (p.second>0) && (c[p.first+1][p.second-1]!=0) )
		{
			c[p.first+1][p.second-1]=0;
			st.push(make_pair(p.first+1,p.second-1));
		}
		if ( (p.first<h-1) && (p.second<w-1) && (c[p.first+1][p.second+1]!=0) )
		{
			c[p.first+1][p.second+1]=0;
			st.push(make_pair(p.first+1,p.second+1));
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int c[100][100];
int w, h;
void dfs(int x,int y) {
	c[x][y] = 0;
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			if (x + i < w&&x + i >= 0 && y + j >= 0 && y + j < h) {
				if (c[x + i][y + j] == 1) {
					dfs(x + i, y + j);
				}
			}
		}
	}
}

int main(void) {
	int ans;
	while (1) {
		ans = 0;
		cin >> w >> h;
		if (w == 0 && h == 0) {
			break;
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[j][i];
			}
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (c[j][i] == 1) {
					dfs(j, i);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}