classSolution { intdfs(vector<vector<int>>& grid, int y, int x){ if(0 > y or0 > x or y >= grid.size() or x >= grid[y].size() or grid[y][x] == 0) return0; int cell = grid[y][x]; grid[y][x] = 0; int res = cell; res = max(res, dfs(grid,y+1,x) + cell); res = max(res, dfs(grid,y-1,x) + cell); res = max(res, dfs(grid,y,x+1) + cell); res = max(res, dfs(grid,y,x-1) + cell); grid[y][x] = cell; return res; } public: intgetMaximumGold(vector<vector<int>>& grid){ int res = 0; for(int i = 0; i < grid.size(); i++) { for(int j = 0; j < grid[i].size(); j++) res = max(res, dfs(grid,i,j)); } return res; } };