You are given an n x n grid where you have placed some 1 x 1 x 1 cubes. Each value v = grid[i][j] represents a tower of v cubes placed on top of cell (i, j).

After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.

Return the total surface area of the resulting shapes.

Note: The bottom face of each shape counts toward its surface area.

&nbsp;
Example 1:

Input: grid = [[1,2],[3,4]]
Output: 34


Example 2:

Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
Output: 32


Example 3:

Input: grid = [[2,2,2],[2,1,2],[2,2,2]]
Output: 46


&nbsp;
Constraints:


	n == grid.length == grid[i].length
	1 &lt;= n &lt;= 50
	0 &lt;= grid[i][j] &lt;= 50


class Solution:
    def surfaceArea(self, grid: List[List[int]]) -> int:        
        m, n = len(grid), len(grid[0])
        
        area = 0
        for r in range(m):               
            for c in range(n):
                if grid[r][c] != 0:
                    area += 2
                
                if r == 0 or r == m - 1:
                    area += grid[r][c] if m != 1 else 2*grid[r][c]
                if r != m - 1:                    
                    area += abs(grid[r][c] - grid[r+1][c])
                    
                if c == 0 or c == n - 1:
                    area += grid[r][c] if n != 1 else 2*grid[r][c]
                if c != n - 1:                    
                    area += abs(grid[r][c] - grid[r][c+1])                    
                
        return area
class Solution {
    public int surfaceArea(int[][] grid) {
        int area = 0;
        int n = grid.length;
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){

                // Adding the top part of grid
                if(i==0) area += grid[i][j];
                else area += Math.abs(grid[i][j] - grid[i-1][j]);

                // Adding the left part of grid
                if(j==0) area += grid[i][j];
                else area += Math.abs(grid[i][j] - grid[i][j-1]);

                // Adding bottom part of bottom-most cubes
                if(i == n-1) area += grid[i][j];

                // Adding right part for right-most cubes
                if(j == n-1) area += grid[i][j];

                // Add top and bottom surfaces if there is no hole in grid
                if(grid[i][j] != 0) area += 2;
            }
        }
        return area;
    }
}
class Solution {
public:
    int surfaceArea(vector<vector<int>>& grid) {
        int area = 0;
        
        for(int i  = 0; i < grid.size(); i++) {
            for(int j = 0; j < grid[0].size(); j++) {
				//adding 4 sides
                area += grid[i][j]*4;
				
				//adding two because of there will only one top and one bottom if cube is placed upon each other
                if(grid[i][j] != 0)
                    area+=2;
				
				//subtracting adjacent side area if any
                if(i-1 >= 0)
                    area -= min(grid[i-1][j], grid[i][j]);
                if(i+1 < grid.size())
                    area -= min(grid[i+1][j], grid[i][j]);
                if(j-1 >= 0)
                    area -= min(grid[i][j-1], grid[i][j]);
                if(j+1 < grid.size())
                    area -= min(grid[i][j+1], grid[i][j]);
            }
        }
        
        return area;
    }
};
var surfaceArea = function(grid) {
    let cube=0, overlap=0;
    for(let i=0; i<grid.length; i++){
        for(let j=0; j<grid[i].length; j++){
            cube+=grid[i][j];
            if(i>0){overlap+=Math.min(grid[i][j], grid[i-1][j]);} // x-direction
            if(j>0){overlap+=Math.min(grid[i][j], grid[i][j-1]);} // y-direction
			if(grid[i][j]>1){overlap+=grid[i][j]-1}; // z-direction
        }
    }
    return cube*6-overlap*2;
};
Surface Area of 3D Shapes