<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

519. 3D City Model
Time limit per test: 0.25 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard

A city is built on the top of a rectangular n x m grid where all the grid cells are equal squares. Each of the n · m grid cells can serve as a foundation of a single building in the city. A building is represented as a number of 1 x 1 x 1 cubes stacked on the top of each other. The cube that lays in the foundation of a building entirely occupies a single cell on the grid. It is clear that adjacent buildings can share a wall or a part of it. Typical cities can be seen on the image below.


The King of Berland has a 3D model of the capital city in his office. This model was made on a special 3D-printer out of plastic. It represents a layout of the capital city, but the scale is smaller, so it's very convenient for the King to examine the model without having to visit the city itself. The King is bored though because the model is colorless, so he wants to paint the model. To calculate the exact amount of required paint he should know the total area of the model's surface.

You have to help the King and write a program that will calculate the required surface area of the given model. While calculating the surface area you should count not only the side surfaces, but also the areas of the top and bottom facets.

The model is given to you as n x m matrix of digits. A digit in the j-th position of the i-th row stands for the height of the building with its foundation in cell (i, j) of the model. If the corresponding digit is equal to "0", it means there is no building built on the top of this cell.

Input
The first line of input contains a pair of integers n, m (1 ≤ n,m ≤ 100), where n — amount of rows in the given grid, m — amount of columns. The following n lines contain the description of the model. These n lines contain m digits each representing heights of the buildings. It's guaranteed that the given matrix contains at least one non-zero digit.

Output
Output the only positive integer — surface area of the model.

Example(s)
sample input
sample output
3 3
111
212
111
38

sample input
sample output
3 4
1000
0010
0000
12



Note
The first sample test corresponds to the leftmost picture from the problem statement.

<|response|>
1. Abridged problem statement  
You have an n×m grid of nonnegative integers h[i][j], where each cell represents a stack of h[i][j] unit cubes (a “building”). Compute the total surface area of the resulting 3D structure, counting:  
- top and bottom faces of every nonempty column, and  
- all exposed side faces (where a side is exposed if a column is taller than its neighbor in that direction, treating out-of-bounds as height 0).

2. Key observations  
- Any column of height H > 0 contributes exactly 2 faces for its top and bottom.  
- For each of the four cardinal directions (up, down, left, right), the exposed vertical area is max(0, H − H_neighbor).  
- If a neighbor is outside the grid, treat its height as 0.  
- Summing 2 for each nonzero column plus those positive height differences over all cells exactly counts every exposed unit-square face.

3. Full solution approach  
a. Read integers n and m.  
b. Read the grid of heights as n strings (or as an n×m integer array).  
c. Initialize total_area = 0.  
d. Loop over every cell (i, j):  
   i.   Let H = height[i][j]. If H = 0, continue.  
   ii.  Add 2 to total_area (top and bottom).  
   iii. For each of the four directions (delta_i, delta_j) ∈ {(-1,0), (1,0), (0,-1), (0,1)}:  
         – Compute neighbor coordinates ni = i + delta_i, nj = j + delta_j.  
         – If (ni, nj) is inside the grid, Hn = height[ni][nj]; otherwise Hn = 0.  
         – Add max(0, H − Hn) to total_area.  
e. After processing all cells, output total_area.  
Time complexity: O(n·m), since each cell does a constant amount of work. This easily fits n,m up to 100.

4. C++ implementation with detailed comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    // Read grid dimensions
    cin >> n >> m;

    // Read the city model as n strings of digits
    vector<string> grid(n);
    for(int i = 0; i < n; i++) {
        cin >> grid[i];
    }

    long long total_area = 0;

    // Offsets for the four neighboring directions
    const int di[4] = {-1, 1, 0, 0};
    const int dj[4] = {0, 0, -1, 1};

    // Iterate over every cell
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            int H = grid[i][j] - '0';  // height at (i,j)
            if (H == 0) {
                // No building, no faces contributed
                continue;
            }

            // Add top and bottom faces
            total_area += 2;

            // Add exposed side faces
            for(int dir = 0; dir < 4; dir++) {
                int ni = i + di[dir];
                int nj = j + dj[dir];
                int Hn = 0;  // neighbor height defaults to 0
                if (ni >= 0 && ni < n && nj >= 0 && nj < m) {
                    Hn = grid[ni][nj] - '0';
                }
                // Only the positive difference H - Hn is exposed
                if (H > Hn) {
                    total_area += (H - Hn);
                }
            }
        }
    }

    // Output the computed surface area
    cout << total_area << "\n";
    return 0;
}
```

5. Python implementation with detailed comments  
```python
import sys

def main():
    data = sys.stdin.read().split()
    n, m = map(int, data[:2])
    # Next n tokens are the rows of the grid
    grid = data[2:2+n]

    total_area = 0

    # Direction offsets: up, down, left, right
    dirs = [(-1,0), (1,0), (0,-1), (0,1)]

    # Process each cell
    for i in range(n):
        for j in range(m):
            h = int(grid[i][j])
            if h == 0:
                # No cubes in this cell
                continue

            # Top and bottom
            total_area += 2

            # Check each neighbor for exposed side area
            for dx, dy in dirs:
                ni, nj = i + dx, j + dy
                # If neighbor is inside, get its height; otherwise 0
                if 0 <= ni < n and 0 <= nj < m:
                    hn = int(grid[ni][nj])
                else:
                    hn = 0
                # Only positive height difference is exposed
                if h > hn:
                    total_area += (h - hn)

    # Print the result
    print(total_area)

if __name__ == "__main__":
    main()
```