#P1187. Surface Area of Cubic Buildings

    ID: 13971 Type: Default 1000ms 256MiB

Surface Area of Cubic Buildings

Surface Area of Cubic Buildings

Given a city built on a regular n×mn \times m grid where each cell may contain a building constructed by stacking 1×1×11 \times 1 \times 1 cubes (all buildings share the same base). The height at each cell represents the number of cubes used. Calculate the total surface area of the resulting 3D model.

The surface area includes the top, bottom, and all exposed side surfaces of the buildings. When two adjacent buildings touch, the area between them is not counted.

inputFormat

The first line contains two integers nn and mm representing the dimensions of the grid. Each of the following nn lines contains mm integers, where each integer represents the height of the building at that cell.

outputFormat

Output a single integer that represents the total surface area of the building model.

sample

1 1
2
10

</p>