You are given an n x n matrix where each entry represents a vertical stack of 1x1x1 cubes.
For a cell (i, j), the value grid[i][j] indicates how many cubes are piled on that position.
Cubes that are directly touching each other (in the same stack or adjacent horizontally) are considered glued, forming complex 3D shapes.
Your task is to compute the total exposed surface area of all these glued shapes combined.
Each cube face that is not in contact with another cube contributes to the surface area.
Do not forget that the bottom faces count as well.
Input: grid = [[2,1],[0,3]]
Output: 30
Explanation:
Considering all exposed faces, the overall visible surface adds up to 30.
Input: grid = [[1,0,2],[3,1,0],[2,2,1]]
Output: 44
Explanation:
Multiple towers of varying heights produce a total exposed surface of 44.
Input: grid = [[0,0],[0,5]]
Output: 22
Explanation:
Only one tall column contributes to the entire surface area.
Accepted:
Submission: