# Find the Surface area of a 3D figure

Given a N*M matrix A[][] representing a 3D figure. The height of the building at is . Find the surface area of the figure.

Examples :

```Input : N = 1, M = 1   A[][] = { {1} }
Output : 6

Explanation :
The total surface area is 6 i.e 6 side of
the figure and each are of height 1.

Input : N = 3, M = 3   A[][] = { {1, 3, 4},
{2, 2, 3},
{1, 2, 4} }
Output : 60
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach : To find the surface area we need to consider the contribution of all the six sides of the given 3D figure. We will solve the questions in part to make it easy. The base of the Figure will always contribute N*M to the total surface area of the figure, and same N*M area will be contributed by the top of the figure. Now, to calculate the area contributed by the walls, we will take out the absolute difference between the height of two adjacent wall. The difference will be the contribution in the total surface area.

Below is the implementation of the above idea :

## C++

 `// CPP program to find the Surface area of a 3D figure ` `#include ` `using` `namespace` `std; ` ` `  `// Declaring the size of the matrix ` `const` `int` `M = 3; ` `const` `int` `N = 3; ` ` `  `// Absolute Difference between the height of ` `// two consecutive blocks ` `int` `contribution_height(``int` `current, ``int` `previous) ` `{ ` `    ``return` `abs``(current - previous); ` `} ` ` `  `// Function To calculate the Total surfaceArea. ` `int` `surfaceArea(``int` `A[N][M]) ` `{ ` `    ``int` `ans = 0; ` ` `  `    ``// Traversing the matrix. ` `    ``for` `(``int` `i = 0; i < N; i++) { ` `        ``for` `(``int` `j = 0; j < M; j++) { ` ` `  `            ``/* If we are traveling the topmost row in the  ` `            ``matrix, we declare the wall above it as 0 ` `            ``as there is no wall above it. */` `            ``int` `up = 0; ` ` `  `            ``/* If we are traveling the leftmost column in the  ` `            ``matrix, we declare the wall left to it as 0 ` `            ``as there is no wall left it. */` `            ``int` `left = 0; ` ` `  `            ``// If its not the topmost row ` `            ``if` `(i > 0) ` `                ``up = A[i - 1][j]; ` ` `  `            ``// If its not the leftmost column ` `            ``if` `(j > 0) ` `                ``left = A[i][j - 1]; ` ` `  `            ``// Summing up the contribution of by ` `            ``// the current block ` `            ``ans += contribution_height(A[i][j], up)  ` `                    ``+ contribution_height(A[i][j], left); ` ` `  `            ``/* If its the rightmost block of the matrix ` `               ``it will contribute area equal to its height ` `               ``as a wall on the right of the figure */` `            ``if` `(i == N - 1) ` `                ``ans += A[i][j]; ` ` `  `            ``/* If its the lowest block of the matrix it will  ` `               ``contribute area equal to its height as a wall ` `               ``on the bottom of the figure */` `            ``if` `(j == M - 1) ` `                ``ans += A[i][j]; ` `        ``} ` `    ``} ` ` `  `    ``// Adding the contribution by the base and top of the figure ` `    ``ans += N * M * 2; ` `    ``return` `ans; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `A[N][M] = { { 1, 3, 4 }, ` `                    ``{ 2, 2, 3 }, ` `                    ``{ 1, 2, 4 } }; ` `    ``cout << surfaceArea(A) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to find the Surface ` `// area of a 3D figure ` ` `  `class` `GFG  ` `{ ` `    ``// Declaring the size of the matrix ` `    ``static` `final` `int` `M=``3``; ` `    ``static` `final` `int` `N=``3``; ` `     `  `    ``// Absolute Difference between the height of ` `    ``// two consecutive blocks ` `    ``static` `int` `contribution_height(``int` `current, ``int` `previous) ` `    ``{ ` `        ``return` `Math.abs(current - previous); ` `    ``} ` `     `  `    ``// Function To calculate the Total surfaceArea. ` `    ``static` `int` `surfaceArea(``int` `A[][]) ` `    ``{ ` `        ``int` `ans = ``0``; ` `     `  `        ``// Traversing the matrix. ` `        ``for` `(``int` `i = ``0``; i < N; i++)  ` `        ``{ ` `            ``for` `(``int` `j = ``0``; j < M; j++) { ` `     `  `                ``/* If we are traveling the topmost  ` `                ``row in the matrix, we declare the  ` `                ``wall above it as 0 as there is no ` `                ``wall above it. */` `                ``int` `up = ``0``; ` `     `  `                ``/* If we are traveling the leftmost  ` `                ``column in the matrix, we declare the ` `                ``wall left to it as 0as there is no ` `                ``wall left it. */` `                ``int` `left = ``0``; ` `     `  `                ``// If its not the topmost row ` `                ``if` `(i > ``0``) ` `                    ``up = A[i - ``1``][j]; ` `     `  `                ``// If its not the leftmost column ` `                ``if` `(j > ``0``) ` `                    ``left = A[i][j - ``1``]; ` `     `  `                ``// Summing up the contribution of by ` `                ``// the current block ` `                ``ans += contribution_height(A[i][j], up)  ` `                       ``+ contribution_height(A[i][j], left); ` `     `  `                ``/* If its the rightmost block of the matrix ` `                ``it will contribute area equal to its height ` `                ``as a wall on the right of the figure */` `                ``if` `(i == N - ``1``) ` `                    ``ans += A[i][j]; ` `     `  `                ``/* If its the lowest block of the  ` `                ``matrix it will contribute area equal  ` `                ``to its height as a wall on ` `                 ``the bottom of the figure */` `                ``if` `(j == M - ``1``) ` `                    ``ans += A[i][j]; ` `            ``} ` `        ``} ` `     `  `        ``// Adding the contribution by  ` `        ``// the base and top of the figure ` `        ``ans += N * M * ``2``; ` `        ``return` `ans; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``int` `A[][] = {{ ``1``, ``3``, ``4` `}, ` `                     ``{ ``2``, ``2``, ``3` `}, ` `                     ``{ ``1``, ``2``, ``4` `} }; ` `        ``System.out.println(surfaceArea(A)); ` `    ``} ` `} ` ` `  `// This code is contributed By Anant Agarwal. `

## Python3

 `# Python3 program to find the  ` `# Surface area of a 3D figure ` ` `  ` `  `# Declaring the size  ` `# of the matrix ` `M ``=` `3``; ` `N ``=` `3``; ` ` `  `# Absolute Difference  ` `# between the height of ` `# two consecutive blocks ` `def` `contribution_height(current, previous): ` `    ``return` `abs``(current ``-` `previous); ` ` `  `# Function To calculate  ` `# the Total surfaceArea. ` `def` `surfaceArea(A): ` `    ``ans ``=` `0``; ` ` `  `    ``# Traversing the matrix. ` `    ``for` `i ``in` `range``(N):  ` `        ``for` `j ``in` `range``(M): ` ` `  `            ``# If we are traveling the ` `            ``# topmost row in the matrix, ` `            ``# we declare the wall above it  ` `            ``# as 0 as there is no wall  ` `            ``# above it. ` `            ``up ``=` `0``; ` ` `  `            ``# If we are traveling the ` `            ``# leftmost column in the  ` `            ``# matrix, we declare the wall  ` `            ``# left to it as 0 as there is ` `            ``# no wall left it.  ` `            ``left ``=` `0``; ` ` `  `            ``# If its not the topmost row ` `            ``if` `(i > ``0``): ` `                ``up ``=` `A[i ``-` `1``][j]; ` ` `  `            ``# If its not the  ` `            ``# leftmost column ` `            ``if` `(j > ``0``): ` `                ``left ``=` `A[i][j ``-` `1``]; ` ` `  `            ``# Summing up the  ` `            ``# contribution of by ` `            ``# the current block ` `            ``ans ``+``=` `contribution_height(A[i][j], up)``+``contribution_height(A[i][j], left); ` `             `  `            ``# If its the rightmost block  ` `            ``# of the matrix it will contribute  ` `            ``# area equal to its height as a ` `            ``# wall on the right of the figure */ ` `            ``if` `(i ``=``=` `N ``-` `1``): ` `                ``ans ``+``=` `A[i][j]; ` ` `  `            ``# If its the lowest block  ` `            ``# of the matrix it will  ` `            ``# contribute area equal to  ` `            ``# its height as a wall on  ` `            ``# the bottom of the figure ` `            ``if` `(j ``=``=` `M ``-` `1``): ` `                ``ans ``+``=` `A[i][j]; ` ` `  `    ``# Adding the contribution by  ` `    ``# the base and top of the figure ` `    ``ans ``+``=` `N ``*` `M ``*` `2``; ` `    ``return` `ans; ` ` `  `# Driver Code ` `A ``=` `[[``1``, ``3``, ``4``],[``2``, ``2``, ``3``],[``1``, ``2``, ``4``]]; ` `print``(surfaceArea(A)); ` ` `  `# This code is contributed By mits `

## C#

 `// C# program to find the  ` `// Surface area of a 3D figure ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `    ``// Declaring the size of the matrix ` `    ``static` `int` `M=3; ` `    ``static` `int` `N=3; ` `     `  `    ``// Absolute Difference between the  ` `    ``// height of two consecutive blocks ` `    ``static` `int` `contribution_height(``int` `current, ``int` `previous) ` `    ``{ ` `        ``return` `Math.Abs(current - previous); ` `    ``} ` `     `  `    ``// Function To calculate the ` `    ``// Total surfaceArea. ` `    ``static` `int` `surfaceArea(``int` `[,]A) ` `    ``{ ` `        ``int` `ans = 0; ` `     `  `    ``// Traversing the matrix. ` `        ``for` `(``int` `i = 0; i < N; i++)  ` `        ``{ ` `            ``for` `(``int` `j = 0; j < M; j++) { ` `     `  `    ``// If we are traveling the topmost  ` `    ``// row in the matrix, we declare the  ` `    ``// wall above it as 0 as there is no ` `    ``// wall above it. ` `                ``int` `up = 0; ` `     `  `    ``// If we are traveling the leftmost  ` `    ``// column in the matrix, we declare  ` `    ``// the wall left to it as 0as there  ` `    ``// is no wall left it.  ` `                ``int` `left = 0; ` `     `  `    ``// If its not the topmost row ` `                ``if` `(i > 0) ` `                    ``up = A[i - 1,j]; ` `     `  `    ``// If its not the leftmost column ` `                ``if` `(j > 0) ` `                    ``left = A[i,j - 1]; ` `     `  `    ``// Summing up the contribution   ` `    ``// of by the current block ` `            ``ans += contribution_height(A[i,j], up)  ` `                ``+ contribution_height(A[i,j], left); ` `     `  `    ``// If its the rightmost block of the  ` `    ``// matrix it will contribute area equal  ` `    ``// to its height as a wall on the right  ` `    ``// of the figure ` `                ``if` `(i == N - 1) ` `                    ``ans += A[i,j]; ` `     `  `    ``// If its the lowest block of the  ` `    ``// matrix it will contribute area   ` `    ``// equal to its height as a wall  ` `    ``// on the bottom of the figure ` `                ``if` `(j == M - 1) ` `                    ``ans += A[i,j]; ` `            ``} ` `        ``} ` `     `  `    ``// Adding the contribution by the ` `    ``// base and top of the figure ` `        ``ans += N * M * 2; ` `        ``return` `ans; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``int` `[,]A = {{ 1, 3, 4 }, ` `                    ``{ 2, 2, 3 }, ` `                    ``{ 1, 2, 4 } }; ` `        ``Console.WriteLine(surfaceArea(A)); ` `    ``} ` `} ` ` `  `// This code is contributed By vt_m. `

## PHP

 ` 0) ` `                ``\$up` `= ``\$A``[``\$i` `- 1][``\$j``]; ` ` `  `            ``// If its not the  ` `            ``// leftmost column ` `            ``if` `(``\$j` `> 0) ` `                ``\$left` `= ``\$A``[``\$i``][``\$j` `- 1]; ` ` `  `            ``// Summing up the  ` `            ``// contribution of by ` `            ``// the current block ` `            ``\$ans` `+= contribution_height(``\$A``[``\$i``][``\$j``], ``\$up``) + ` `                    ``contribution_height(``\$A``[``\$i``][``\$j``], ``\$left``); ` `             `  `            ``/* If its the rightmost block  ` `            ``of the matrix it will contribute  ` `            ``area equal to its height as a ` `            ``wall on the right of the figure */` `            ``if` `(``\$i` `== ``\$N` `- 1) ` `                ``\$ans` `+= ``\$A``[``\$i``][``\$j``]; ` ` `  `            ``/* If its the lowest block  ` `               ``of the matrix it will  ` `               ``contribute area equal to  ` `               ``its height as a wall on  ` `               ``the bottom of the figure */` `            ``if` `(``\$j` `== ``\$M` `- 1) ` `                ``\$ans` `+= ``\$A``[``\$i``][``\$j``]; ` `        ``} ` `    ``} ` ` `  `    ``// Adding the contribution by  ` `    ``// the base and top of the figure ` `    ``\$ans` `+= ``\$N` `* ``\$M` `* 2; ` `    ``return` `\$ans``; ` `} ` ` `  `// Driver Code ` `\$A` `= ``array``(``array``(1, 3, 4), ` `           ``array``(2, 2, 3), ` `           ``array``(1, 2, 4)); ` `echo` `surfaceArea(``\$A``); ` ` `  `// This code is contributed By mits ` `?> `

Output :

```60
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Mithun Kumar