#include <iostream>
#include <fstream>
#include <iomanip>
#define FI "TOANHA.inp"
#define maxx 100
int H[maxx][maxx], tmp[maxx][maxx];
int M, N; /*M represent Row, N represents Colunm*/
int building;
int max_area;
int total_area;
void InputData()
{
ifstream F;
F.open(FI);
F >> M >> N;
F.ignore();
for ( int i = 1; i <= M; i++){
for( int j = 1; j <= N; j++){
F >> H[i][j];
}
F.ignore();
}
F.close();
}
void Initialization()
{
/*Set range for the area*/
for ( int i = 0; i <= N+1; i++ ){
H[0][i] = -1;
H[M+1][i] = -1;
}
for ( int i = 0; i <= M+1; i++ ){
H[i][0] = -1;
H[i][N+1] = -1;
}
/*Print map*/
for ( int i = 0; i <= M+1; i++ ){
for ( int j = 0; j <= N+1; j++ ){
cout << H
[i
][j
] << setw
(3); }
}
/*Sub Array*/
for ( int i = 1; i <= M; i++)
{
for( int j = 1; j <= N; j++ )
{
tmp[i][j] = H[i][j];
}
}
}
int min ( int a, int b )
{
if ( a < b ) return a;
else return b;
}
void Spreading_Programming (int r, int c , int &s)
{
H[r][c] = 0;
s = s + 4*tmp[r][c]; /*Paint around the building, 4 sides*/
/*Eleminate the share side of 2 building, which ones lower is chosen*/
s = s - min(tmp[r][c],tmp[r][c+1]) - min(tmp[r][c],tmp[r][c-1])
- min(tmp[r][c],tmp[r+1][c]) - min(tmp[r][c],tmp[r-1][c]);
if ( H[r+1][c] != 0 && H[r+1][c] != -1 ) Spreading_Programming (r+1, c, s);
if ( H[r-1][c] != 0 && H[r-1][c] != -1 ) Spreading_Programming (r-1, c, s);
if ( H[r][c+1] != 0 && H[r][c+1] != -1 ) Spreading_Programming (r, c+1, s);
if ( H[r][c+1] != 0 && H[r][c-1] != -1 ) Spreading_Programming (r, c-1, s);
}
void Process()
{
int area;
building = 0;
max_area = 0;
total_area = 0;
for ( int i = 1; i <= M; i++)
{
for( int j = 1; j <= N; j++)
{
if ( H[i][j] != -1 && H[i][j] != 0 ) /*If there's no "don nguyên*/
{
area = 0;
Spreading_Programming ( i, j, area);
building = building + 1; /*Each time increases the number of building*/
total_area = total_area + area; /*Also increase total area*/
/*Chose building have maximum area*/
if ( area > max_area )
{
max_area = area;
}
}
}
}
}
void OutputData()
{
cout << "There are : " << building
<< " buildings " << endl
; cout << "Total area is : " << total_area
<< endl
; cout << "The maximum area is : " << max_area
<< endl
; }
void main()
{
InputData();
Initialization();
Process();
OutputData();
}