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;

void input(int **, int, int);
int howManyIslands(int **, int, int);
void howBigIslands(int **, bool **, int, int, int, int);

int main()
{
	int **island, w, h;
	while(true){
		cin >> w >> h;
		if(w==h && h==0) break;
		island = new int*[w];
		for(int i=0; i<w; i++)
			island[i] = new int[h];
		input(island, w, h);
		cout << howManyIslands(island, w, h) << endl;
		for(int i=0; i<w; i++)
			delete[] island[i];
		delete[] island;
	}
	return 0;
}

void input(int **island, int w, int h)
{
	char *buf;
	cin.clear();cin.ignore();
	for(int i=0; i<h; i++){
		buf = new char[w*2];
		cin.getline(buf, sizeof(char)*w*2);
		int j=0, k=0;
		while(buf[j] != '\0'){
			if(buf[j] != ' '){
				switch(buf[j]){
				case '0':
					island[k][i] = 0;
					k++;
					break;
				case '1':
					island[k][i] = 1;
					k++;
					break;
				}
				if(w==k) break;
			}
			j++;
		}
		delete[] buf;
	}
}

int howManyIslands(int **island, int w, int h)
{
	bool **check;
	int ans = 0;
	check = new bool*[w];
	for(int i=0; i<w; i++){
		check[i] = new bool[h];
		for(int j=0; j<h; j++)
			check[i][j] = false;
	}
	for(int i=0; i<w; i++){
		for(int j=0; j<h; j++){
			if(check[i][j])continue;
			if(island[i][j]){
				ans++;
				howBigIslands(island, check, i, j, w, h);
			} else {
				check[i][j] = true;
			}
		}
	}
	for(int i=0; i<w; i++)
		delete[] check[i];
	delete[] check;

	return ans;
}

void howBigIslands(int **island, bool **check, int x, int y, int w, int h)
{
	if(check[x][y]) return;
	enum __DIR{ UPPER_LEFT, UP, UPPER_RIGHT, LEFT, RIGHT, LOWER_LEFT,
		UNDER, LOWER_RIGHT, DIR_END};
	int periphe[DIR_END][2]={
			{-1, -1}, {0, -1}, {1, -1},
			{-1, 0}, {1, 0},
			{-1, 1}, {0, 1}, {1, 1}
	};
	check[x][y] = true;
	for(int i=0; i<DIR_END; i++){
		int newx = x+periphe[i][0],
			newy = y+periphe[i][1];
		if(newx < 0 || w-1 < newx ||
			newy < 0 || h-1 < newy) continue;
		if(island[newx][newy]){
			if(!check[newx][newy])
				howBigIslands(island, check,
				newx, newy, w, h);
		}
	}
}

C++: //
//  main.cpp
//  AOJ-1160
//
//  Created by Kenta Kodera on 2017/06/18.
//  Copyright ?? 2017??´ Kenta Kodera. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>
#include <stack>
#include <tuple>
#include <array>
#include <ctype.h> // isdigit
#include <cassert>
using namespace std;

int W, H;

void dfs(vector<vector<int>> &field, int h, int w){
  int dhw[3] = {1, -1, 0};
  field[h][w] = 0;
  
  for(int i=0; i<3; i++){
    for(int j=0; j<3; j++){
      int nh = h+dhw[i];
      int nw = w+dhw[j];
      for(int h=0; h<H; h++){
        for(int w=0; w<W; w++){
          if(0<=nh && nh<H && 0<=nw && nw<W && field[nh][nw]==1){
            dfs(field, nh, nw);
          }
        }
      }
    }
  }
  
}


int main(){
  
  while(cin >> W >> H, W|H){
    vector<vector<int>> field(H);
    for(int h=0; h<H; h++){
      field[h].resize(W);
    }
    
    for(int h=0; h<H; h++){
      for(int w=0; w<W; w++){
        cin >> field[h][w];
      }
    }
    
    int n=0;
    for(int h=0; h<H; h++){
      for(int w=0; w<W; w++){
        if(field[h][w] == 1){
          dfs(field, h, w);
          n++;
        }
      }
    }
    cout << n << endl;
    
  }
  

  return 0;
}

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

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

int dfs(int x, int y) {
  if (x < 0 || y < 0 || x >= w || y >= h) return 0;
  if (c[y][x] == 0) return 0;
  c[y][x] = 0;
  for (int i = 0; i < 8; i++) dfs(x + dx[i], y + dy[i]);
  return 1;
}

int main() {
  while (1) {
    scanf("%d%d", &w, &h);
    if (w == 0) break;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) scanf("%d", &c[i][j]);
    int counter = 0;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) counter += dfs(j, i);
    printf("%d\n", counter);
  }
}

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 < 3 && 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<iostream>
#include<vector>
#include<stack>
#include<utility>
using namespace std;

void update(int x,int y,vector< vector<int> >& map){
	if(map[y][x]==1){
		map[y][x]=2;
	}else{
		return;
	}
	if(x!=0)update(x-1,y,map);
	if(y!=0)update(x,y-1,map);
	if(x!=0&&y!=0)update(x-1,y-1,map);
	if(y!=map.size()-1)update(x,y+1,map);
	if(x!=map[0].size()-1)update(x+1,y,map);
	if(y!=map.size()-1&&x!=0)update(x-1,y+1,map);
	if(y!=map.size()-1&&x!=map[0].size()-1)update(x+1,y+1,map);
	if(x!=map[0].size()-1&&y!=0)update(x+1,y-1,map);
}
int main(){
	while(true){
		vector< vector<int> > map;
		int w,h,tmp,sum=0;
		cin>>w>>h;
		if(w==0)break;
		map.resize(h);
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				map[y].resize(w);
				cin>>tmp;
				map[y][x]=tmp;
			}
		}
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				if(map[y][x]==1){
					sum++;
					update(x,y,map);
				}
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}