Iterative Solution
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int maximumAmount(vector<vector<int>>& coins) {
        int n = coins.size(), m = coins[0].size();
        vector<vector<vector<int>>> dp(n, vector<vector<int>>(m, vector<int>(3, INT_MIN)));

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                for (int k = 0; k <= 2; ++k) {
                    if (i == 0 && j == 0)
                    {
                        dp[i][j][k] = k < 2 ? max(0, coins[i][j]) : coins[i][j];
                        continue;
                    }
                    if (i > 0)
                    {
                        dp[i][j][k] = max(dp[i][j][k], coins[i][j] + dp[i-1][j][k]);
                        if (coins[i][j] < 0 && k < 2)
                            dp[i][j][k] = max(dp[i][j][k], dp[i-1][j][k+1]);
                    }
                    if (j > 0)
                    {
                        dp[i][j][k] = max(dp[i][j][k], coins[i][j] + dp[i][j-1][k]);
                        if (coins[i][j] < 0 && k < 2)
                            dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k+1]);
                    }
                    
                }
            }
        }
        return *max_element(dp[n-1][m-1].begin(), dp[n-1][m-1].end());
    }
};

```
['C++']