function getMax(n, m, grid){
let len=Math.max(3,m);
let dp = [];
let visited = [];
for(let i = 0; i < 3; i++)
{
dp[i] = new Array(len).fill(0);
visited[i] = new Array(len).fill(0);
}
function maximum_sum_path(i, j)
{
if (i == n - 1 && j == m - 1)
return grid[i][j];
if (visited[i][j]!=0)
return dp[i][j];
visited[i][j] = 1;
let total_sum = 0;
if (i < n - 1 && j < m - 1)
{
let forward= maximum_sum_path(i, j+1);
let down= maximum_sum_path(i+1, j);
let up=null;
if(i>0){
up= maximum_sum_path(i-1, j);
}
let current_sum = Math.max(forward, down, up);
total_sum = grid[i][j] + current_sum;
}
else if (i == n - 1)
total_sum = grid[i][j]+Math.max( maximum_sum_path(i, j + 1),maximum_sum_path(i-1, j));
else
total_sum = grid[i][j] + maximum_sum_path(i + 1, j);
dp[i][j] = total_sum;
return total_sum;
}
return maximum_sum_path(0, 0);
}
let grid=[[2,2,2],[2,2,2],[2,2,2]]
console.log(getMax(3,4,grid))
// Calling the implemented function