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 <queue>
using namespace std;
struct Point{int x,y;};
int main(){
  int w,h,p[60][60]={},v[60][60]={},i,j,k,o;
  queue <Point> q;
  Point n,b;
  cin >> w >> h;
  while(w!=0){
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	cin >> p[i][j];
	v[i][j]=0;
      }
    }
    o=0;
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	if(v[i][j]==0&&p[i][j]==1){
	  o++;
	  n.x=i;n.y=j;
	  q.push(n);
	  while(!q.empty()){
	    n=q.front();
	    q.pop();
	    v[n.x][n.y]=1;
	    if(n.x+1<w&&v[n.x+1][n.y]==0&&p[n.x+1][n.y]==1) {
	      b.x=n.x+1;b.y=n.y;
	      q.push(b);
	    }
	    if(n.y+1<h&&v[n.x][n.y+1]==0&&p[n.x][n.y+1]==1) {
	      b.x=n.x;b.y=n.y+1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&v[n.x-1][n.y]==0&&p[n.x-1][n.y]==1) {
	      b.x=n.x-1;b.y=n.y;
	      q.push(b);
	    }
	    if(n.y-1>=0&&v[n.x][n.y-1]==0&&p[n.x][n.y-1]==1) {
	      b.x=n.x;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y+1<h&&v[n.x+1][n.y+1]==0&&p[n.x+1][n.y+1]==1) {
	      b.x=n.x+1;b.y=n.y+1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y-1>=0&&v[n.x-1][n.y-1]==0&&p[n.x-1][n.y-1]==1) {
	      b.x=n.x-1;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y-1>=0&&v[n.x+1][n.y-1]==0&&p[n.x+1][n.y-1]==1) {
	      b.x=n.x+1;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y+1<h&&v[n.x-1][n.y+1]==0&&p[n.x-1][n.y+1]==1) {
	      b.x=n.x-1;b.y=n.y+1;
	      q.push(b);
	    }
	  }
	}
      }
    }
    cout << o << endl;
    cin >> w >> h;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int w, h, isl, v;
int a[2501];
deque<int> Q;
int main()
{
	while(cin >> w >> h && w>0)
	{
		for(int i=1; i<=w*h; i++)
		{
			cin >> a[i];
		}
		isl=0;
		for(int i=1; i<=w*h; i++)
		{
			if(a[i]==1)
			{
				isl++;
				Q.clear();
				Q.push_back(i);
				while(!Q.empty())
				{
					v=Q.front();
					Q.pop_front();
					if(v>w)
					{
						if(a[v-w]==1)
						{
							a[v-w]=0;
							Q.push_back(v-w);
						}
						if(v%w!=1)
						{
							if(a[v-w-1]==1)
							{
								a[v-w-1]=0;
								Q.push_back(v-w-1);
							}
						}
					}
					if(v%w!=1)
					{
						if(a[v-1]==1)
						{
							a[v-1]=0;
							Q.push_back(v-1);
						}
					}
					if(v%w!=0)
					{
						if(v>w)
						{
							if(a[v-w+1]==1)
							{
								a[v-w+1]=0;
								Q.push_back(v-w+1);
							}
						}
						if(a[v+1]==1)
						{
							a[v+1]=0;
							Q.push_back(v+1);
						}
						if(v+w<w*h)
						{
							if(a[v+w+1]==1)
							{
								a[v+w+1]=0;
								Q.push_back(v+w+1);
							}
						}
					}
					if(v+w<=w*h)
					{
						if(v%w!=1)
						{
							if(a[v+w-1]==1)
							{
								a[v+w-1]=0;
								Q.push_back(v+w-1);
							}
						}
						if(a[v+w]==1)
						{
							a[v+w]=0;
							Q.push_back(v+w);
						}
					}
				}
			}
		}
		cout << isl << endl;
	}
	return 0;
}

C++: #include <stdio.h>
int map[100][100],n,m;
bool went[100][100];
void walk(int x,int y){
    if(x<0||y<0||x>=n||y>=m)return ;
    if(went[x][y])return ;
    if(map[x][y]==0)return ;
    went[x][y]=true;
    walk(x-1,y);
    walk(x+1,y);
    walk(x,y+1);
    walk(x,y-1);
    walk(x+1,y+1);
    walk(x+1,y-1);
    walk(x-1,y+1);
    walk(x-1,y-1);
    return ;
}
int main(){
    int ans;
    while(1){
        scanf("%d%d",&m,&n);
        if(n==0&&m==0)return 0;
        ans=0;
        for(int i=0;i<n;i++)for(int j=0;j<m;j++){
            scanf("%d",&map[i][j]);
            went[i][j]=false;
        }
        for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(!went[i][j]&&map[i][j]==1){
            ans++;
            walk(i,j);
        }
        printf("%d\n",ans);
    }
}

C++: #include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;


bool done[52][52];
int field[52][52];
int ofs[8][2] = {
  {0, 1},
  {1, 0},
  {-1, 0},
  {0, -1},
  {1, 1},
  {1, -1},
  {-1,-1},
  {-1, 1}
};

void paint(int y, int x)
{
  for (int i = 0; i < 8; ++i) {
    int nx = x + ofs[i][0];
    int ny = y + ofs[i][1];
    if (field[ny][nx] && !done[ny][nx]) {
      done[ny][nx] = true;
      paint(ny, nx);
    }
  }
}

int main() {
  int w, h;
  for(;;) {
    cin >> w >> h;
    if ((w|h) == 0) {
      break;
    }
    memset(field, 0, sizeof(field));
    memset(done, 0, sizeof(done));
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        cin >> field[i][j];
      }
    }
    int cnt = 0;
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        if (field[i][j] && !done[i][j]) {
          paint(i, j);
          ++cnt;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}