<p>In a gold mine <code>grid</code> of size <code>m x n</code>, each cell in this mine has an integer representing the amount of gold in that cell, <code>0</code> if it is empty.</p>

<p>Return the maximum amount of gold you can collect under the conditions:</p>

<ul>
	<li>Every time you are located in a cell you will collect all the gold in that cell.</li>
	<li>From your position, you can walk one step to the left, right, up, or down.</li>
	<li>You can&#39;t visit the same cell more than once.</li>
	<li>Never visit a cell with <code>0</code> gold.</li>
	<li>You can start and stop collecting gold from <strong>any </strong>position in the grid that has some gold.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [[0,6,0],[5,8,7],[0,9,0]]
<strong>Output:</strong> 24
<strong>Explanation:</strong>
[[0,6,0],
 [5,8,7],
 [0,9,0]]
Path to get the maximum gold, 9 -&gt; 8 -&gt; 7.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
<strong>Output:</strong> 28
<strong>Explanation:</strong>
[[1,0,7],
 [2,0,6],
 [3,4,5],
 [0,3,0],
 [9,0,20]]
Path to get the maximum gold, 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; 7.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 15</code></li>
	<li><code>0 &lt;= grid[i][j] &lt;= 100</code></li>
	<li>There are at most <strong>25 </strong>cells containing gold.</li>
</ul>

class Solution {
public:
    int dfs(int row, int col, vector<vector<int>>& grid, vector<vector<bool>>& visited) {
        if (row < 0 || col < 0 || row >= grid.size() || col >= grid[0].size() || visited[row][col] || grid[row][col] == 0) {
            return 0;
        }

        visited[row][col] = true; // Mark this cell as visited

        // Explore all four directions
        int up = dfs(row + 1, col, grid, visited);
        int down = dfs(row - 1, col, grid, visited);
        int right = dfs(row, col + 1, grid, visited);
        int left = dfs(row, col - 1, grid, visited);

        visited[row][col] = false; // Unmark this cell as visited (backtrack)
        //______________ABOVE UNMARKING VERY IMPORTANT---------------------------

        return grid[row][col] + max({up, down, right, left});
    }

    int getMaximumGold(vector<vector<int>>& grid) {
        int n = grid.size();
        int m = grid[0].size();
        int maxi = 0;

     
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                vector<vector<bool>> visited(n, vector<bool>(m, false)); // Initialize visited matrix once
                if (grid[i][j] > 0) {
                    maxi = max(maxi, dfs(i, j, grid, visited));
                }
            }
        }

        return maxi;
    }
};






/*
class Solution {
public:
    int dfs(int row,int col,vector<vector<int>>& grid,vector<vector<bool>> &visited){
        if(row<0 || col<0 || row>=grid.size() || col>=grid[0].size() || visited[row][col] || grid[row][col] == 0) {
            return 0;
        }
        visited[row][col]=true;//if you do after dfs traversal, it wont get updated
        int up=0;
        int down=0;
        int right=0;
        int left=0;
        int n=grid.size();
        int m=grid[0].size();
        if(row+1<n && !visited[row+1][col] && grid[row+1][col]!=0){           
            up= grid[row+1][col]+dfs(row+1,col,grid,visited);
            // visited[row+1][col]=true;
        }
        if(row-1>=0 && !visited[row-1][col] && grid[row-1][col]!=0){
            down= grid[row-1][col]+dfs(row-1,col,grid,visited);
            
        }
        if(col+1<n && !visited[row][col+1] && grid[row][col+1]!=0){           
            right= grid[row][col+1]+dfs(row,col+1,grid,visited);
            
        }
         if(col-1>=0 && !visited[row][col-1] && grid[row][col-1]!=0){           
            left= grid[row][col-1]+dfs(row,col-1,grid,visited);
           
        }
        // Unmark this cell as visited (backtrack)
        visited[row][col] = false;

        return max(up,max(down,max(right,left)));
    }

    int getMaximumGold(vector<vector<int>>& grid) {
        int n=grid.size();
        int m=grid[0].size();
        int maxi=0;
        for(int i=0;i<grid.size();i++){
            for(int j=0;j<grid[0].size();j++){
                vector<vector<bool>> visited(n,vector<bool>(m,false));
                if(grid[i][j]>0){
                    maxi=max(maxi,grid[i][j]+dfs(i,j,grid,visited));
                }               
            }
        }
        return maxi;
    }
};
*/
15100