All problems

Problem 993

Projection Area of 3D Shapes

Easy · 10 pts · Matrix, Math, Geometry

Problem

An N × N grid holds towers of unit cubes: GRID[i][j] cubes are stacked on cell (i, j). Find the total area of the three projections (shadows) of the shapes onto the xy, yz and zx planes. The top view counts one unit for every non-empty cell. The side views see the tallest tower in each row and the tallest tower in each column, so add up the row maximums and the column maximums too.

Input

The first line contains N. Each of the next N lines contains N integers.

Output

Print the total projection area.

Constraints

1 <= N <= 50; 0 <= GRID[i][j] <= 50

Example input

2
1 2
3 4

Example output

17

Explanation

For 1 2 / 3 4, the top view has 4 cells, the row maximums are 2 and 4, and the column maximums are 3 and 4. The total is 4 + 6 + 7 = 17.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Determine Whether Matrix Can Be Obtained By Rotation Next: Surface Area of 3D Shapes