#include <stdio.h>
#include <locale.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdlib.h>
using namespace std;
//setlocale( LC_ALL, "russian_russia.1251" );
/*
int main()
{
    setlocale( LC_ALL, "russian_russia.1251" );
}
*/

//*
const int ROWS = 7;
const int COLS = 10;
int array[ ROWS ][ COLS ] = {
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    {0, 0, 1, 1, 1, 1, 0, 0, 0, 0 },
    {0 ,0 ,1 ,1 ,0 ,1 ,0 ,1 ,0 ,0 },
    {0, 0, 1, 1, 1, 1, 0, 1, 1, 0 },
    {1, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    {0, 1, 1, 1, 0, 0, 0, 0, 0, 0 }
};
bool watched[ ROWS ][ COLS ] = { 0 };


struct Rect
{
    int left;
    int top;
    int right;
    int bottom;
};
typedef vector< Rect > Objects;

void go( int y, int x, int y_inc, int x_inc, Rect & rect )
{
    y = y + y_inc;
    x = x + x_inc;
    //cout << y << " " << x << endl;
    if ( y >= 0 && y < ROWS && x >= 0 && x < COLS )
    {
        if ( array[ y ][ x ] != 0 && watched[ y ][ x ] == false )
        {
            //cout << "+" << endl;
            watched[ y ][ x ] = true;
            rect.left = std::min( rect.left, x );
            rect.top = std::min( rect.top, y );
            rect.right = std::max( rect.right, x );
            rect.bottom = std::max( rect.bottom, y );
            go( y, x, -1, -1, rect ); // up left
            go( y, x, -1, 0, rect );  // up
            go( y, x, -1, 1, rect );  // up right
            go( y, x, 0, -1, rect );  // left
            go( y, x, 0, 1, rect );   // right
            go( y, x, 1, 1, rect );   // down right
            go( y, x, 1, 0, rect );   // down
            go( y, x, 1, -1, rect );  // down left
        }
    }
}

int main()
{
    Objects objects;
    cout << "  |";
    for ( int x = 0; x < COLS; x++ )
    {
        cout << " " << x;
    }
    cout << endl;
    cout << "--|";
    for ( int x = 0; x < COLS; x++ )
    {
        cout << "--";
    }
    cout << endl;
    for ( int y = 0; y < ROWS; y++ )
    {
        cout << y << " | ";
        for ( int x = 0; x < COLS; x++ )
        {
            if ( array[ y ][ x ] != 0 && watched[ y ][ x ] == false )
            {
                Rect rect;
                rect.left = x;
                rect.top = y;
                rect.right = x;
                rect.bottom = y;
                go( y, x, 0, 0, rect );
                objects.push_back( rect );
            }
            cout << array[ y ][ x ] << ' ';
        }
        cout << endl; 
    }
    cout << endl;
    for ( Objects::const_iterator it = objects.begin(); it != objects.end(); ++it )
    {
        const Rect & rect = (*it);
        cout << "left = " << rect.left << " top = " << rect.top << " "
             << "right = " << rect.right << " bottom = " << rect.bottom
             << endl;
    }
}
//*/
