Surface Area
InterviewTime limit1sMemory limit512 MB
Given a grid of cube stack heights, compute the total surface area of the resulting 3D figure, including the top, bottom, and exposed side faces.
- Level
Medium4 of 10
- Topics
- Implementation, Math, Array, Simulation
- Solved
- No attempts yet
Problem
There is a sheet of paper of size N×M, divided into cells of size 1×1. A cube of size 1×1×1 is placed on each cell to form a three-dimensional figure.
Given the number of cubes placed on each cell of the paper, write a program that computes the surface area of this figure.

The figure above shows cubes placed on a 3×3 sheet of paper, and its surface area is 60.
Input
The first line gives the size of the paper, N and M. From the second line, N lines give the number of cubes placed on each cell of the paper.
Output
On the first line, print the surface area of the figure.
Constraints
- 1 ≤ N, M ≤ 100
- 1 ≤ number of cubes on one cell ≤ 100