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 <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <memory.h>
#include <functional>

using namespace std;

//FILE * in = freopen("B", "r", stdin);

typedef pair<int,int> P;

int n, m;
int cnt = 0;
int data[100][100];
int check[100][100];

int mvx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int mvy[] = {-1, -1, -1, 0, 0, 1, 1, 1};

void input() {
	for(int i = 0; i < m; i++){
		for(int j = 0; j < n; j++) {
			scanf("%d", &data[i][j]);
		}
	}
	cnt = 0;
	memset(check, 0, sizeof(check));
}

void bfs(int x, int y) {
	queue<P> que;
	que.push(P(x,y));
	check[x][y] = 1;
	while(!que.empty()){
		P cur = que.front(); que.pop();
		for(int i = 0; i < 8; i++) {
			if(cur.first + mvx[i] >= 0 && cur.first + mvx[i] < n && cur.second + mvy[i] >= 0 && cur.second + mvy[i] < m
				&& data[cur.first + mvx[i]][cur.second + mvy[i]] && !check[cur.first+mvx[i]][cur.second+mvy[i]]){
					check[cur.first +mvx[i]][cur.second+mvy[i]] = 1;
					que.push(P(cur.first+mvx[i], cur.second+mvy[i]));
			}
		}
	}
}

void process() {
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			if(data[i][j] && !check[i][j]) {
				bfs(i,j);
				++cnt;
			}
		}
	}
	printf("%d\n", cnt);
}

int main() {
	while(true) {
		scanf("%d%d", &n, &m);
		if(n == 0) break;
		input();
		process();
	}
	return 0;
}

C++: #include<cstdio>
#include<utility>
#include<algorithm>
#include<set>
using namespace std;

int w,h;
pair<int,int> g[50][50];
int s[50][51];

void dfs(int x,int y,int xx,int yy){
    g[x][y]=make_pair(xx,yy);
    for(int i=max(0,x-1);i<min(x+2,h);i++){
        for(int j=max(0,y-1);j<min(y+2,w);j++){
            if(s[i][j]&&(g[i][j].first!=xx||g[i][j].second!=yy)){
                dfs(i,j,xx,yy);
            }
        }
    }
}

int main(){
    scanf("%d%d",&w,&h);
    while(w&&h){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                scanf("%d",&s[i][j]);
            }
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                g[i][j]=make_pair(i,j);
            }
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(s[i][j]==1&&g[i][j].first==i&&g[i][j].second==j){
                    dfs(i,j,i,j);
                }
            }
        }
        set<pair<int,int> > se;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(s[i][j]==1){
                    se.insert(g[i][j]);
                }
            }
        }
        printf("%ld\n",se.size());
        scanf("%d %d",&w,&h);
    }
}

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

int c[51][51];
void dfs(int x,int y){
  int i,j;
  c[x][y]=0;
  for(i=-1;i<=1;++i){
    for(j=-1;j<=1;++j){
      if(c[x+i][y+j]==1){
	dfs(x+i,y+j);
      }
    }
  }
}

int main(){
  int w,h;
  int i,j;

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

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

vector < vector <int> > V;

int h_max;
int w_max;

void solve( int h, int w )
{
	V[h][w] = 0;
	for ( int i = -1; i < 2; i++ )
	{
		for ( int j = -1; j < 2; j++ )
		{
			if ( i == 0 && j == 0 )
				continue;

			if ( h+i >= 0 && h+i < h_max )
			{
				if ( w+j >= 0 && w+j < w_max )
				{
					if ( V[h+i][w+j] == 1 )
					{
						solve( h+i, w+j );
					}
				}
			}

		}
	}
}

int main ( void )
{
	while (1)
	{
		int w, h;
		cin >> w >> h;
		if ( w == 0 && h == 0 )
			break;

		h_max = h;
		w_max = w;
		V = vector < vector <int> > ( h_max, vector <int>(w_max,0) );

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

		int kotae = 0;
		for ( int i = 0; i < h; i++ )
		{
			for ( int j = 0; j < w; j++ )
			{
				if ( V[i][j] == 1 )
				{
					kotae++;
					solve( i, j );
				}
			}
		}

		cout << kotae << endl;

	}
	return 0;
}

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

struct mapst {
	int x, y;
};

#define func(x,y) (c[y][x]&&!f[y][x])

int main() {
	int w, h, ans=0;
	int c[52][52]={{0}}, f[52][52]={{1}};
	int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
	int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
	queue<mapst> q;
	while(1) {
		cin >> w >> h;
		if(!w&&!h) break;
		ans = 0;
		for(int i=0; i<52; ++i) for(int j=0; j<52; ++j) {
			c[i][j] = 0;
			f[i][j] = 1;
		}
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			cin >> c[i][j];
			f[i][j] = 1-c[i][j];
		}
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			if(func(j,i)) {
				mapst m = {j, i}, ms;
				q.push(m);
				ans++;
				do {
					m = q.front();
					q.pop();
					f[m.y][m.x] = 1;
					for(int k=0; k<8; k++) {
						if(func(m.x+dx[k],m.y+dy[k])) {
							ms.x = m.x+dx[k]; ms.y = m.y+dy[k];
							q.push(ms);
						}
					}
				}while(!q.empty());
			}
		}
		cout << ans << endl;
	}
}

C++: #include<iostream>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
int map[55][55],w,h;

void d(int y,int x){
  if(y==h||x==w||y==-1||x==-1)return;
  if(map[y][x]==0)return;
  if(map[y][x]==1)map[y][x]=0;
  d(y-1,x-1),d(y+1,x+1),d(y+1,x),d(y,x+1),d(y-1,x),d(y,x-1),d(y-1,x+1),d(y+1,x-1);
}

int main(){
  while(cin>>w>>h,w){
    int ans=0;
    int x,y;
    r(i,h)r(j,w)cin>>map[i][j];
    while(1){
      x=-1;
      r(i,h)r(j,w)if(map[i][j]==1)y=i,x=j;
      if(x==-1)break;
      d(y,x);
      ans++;
    }
    cout<<ans<<endl;
  }
  return 0;
}