<|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 (each string is a row of digit characters).
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;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n, m;
vector<string> s;

void read() {
    cin >> n >> m;
    s.resize(n);
    cin >> s;
}

void solve() {
    // Surface area of a heightfield of unit cubes. Each non-empty cell
    // contributes 2 for its top and bottom facets. For each of the four
    // horizontal neighbours, the exposed side wall equals max(0, height -
    // neighbour_height); cells outside the grid count as height 0, which
    // exposes the full outer wall. Summing this over every cell gives the
    // total paintable area.

    int ans = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(s[i][j] == '0') {
                continue;
            }

            ans += 2;
            int curr = s[i][j] - '0';
            for(auto [dx, dy]:
                vector<pair<int, int>>{{-1, 0}, {0, -1}, {1, 0}, {0, 1}}) {
                int x = i + dx;
                int y = j + dy;

                int other = 0;
                if(x < n && y < m && x >= 0 && y >= 0) {
                    other = s[x][y] - '0';
                }

                ans += max(0, curr - other);
            }
        }
    }

    cout << ans << '\n';
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    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()
```
