c++ solution
Code
# Code
```cpp []
class Solution {
public:
    int n,m;
    long long dp[510][510][3];
    int find(vector<vector<int>>&mat,int i,int j,int rem){
        if((i==n-1)&&(j==m-1)){
            if(rem>0&&mat[i][j]<0){
                return 0; 
            }
            return mat[i][j];
        }
        if(i>=n||j>=m){
            return -1e8;
        }
        if(dp[i][j][rem]!=-1e8){
            return dp[i][j][rem];
        }
        int ans = mat[i][j]+max(find(mat,i,j+1,rem),find(mat,i+1,j,rem));
        if(rem>0&&mat[i][j]<0){
            ans=max({ans,find(mat,i,j+1,rem-1),find(mat,i+1,j,rem-1)});
        }
        return dp[i][j][rem]= ans;
    }
    int maximumAmount(vector<vector<int>>& coins) {
        n=coins.size();
        m=coins[0].size();
        for(int i=0;i<505;i++){
            for(int j=0;j<505;j++){
                for(int k=0;k<3;k++){
                    dp[i][j][k]=-1e8;
                }
            }
        }
        return find(coins,0,0,2);
    }
};
```
['C++']