1219. Path with Maximum Gold

https://leetcode.com/problems/path-with-maximum-gold/

I've already solved another similar problem. The recursive function looks beautiful but it likely occurs stack overflow.

let around = (i, j) => [
  [i - 1, j],
  [i + 1, j],
  [i, j - 1],
  [i, j + 1],
];

let step = (grid, i, j, footnotes = []) => {
  let m = grid.length;
  let n = grid[0].length;
  if (i < 0 || i >= m || j < 0 || j >= n) {
    return 0;
  }
  if (grid[i][j] === 0) {
    return 0;
  }
  if (footnotes.find(([fi, fj]) => fi === i && fj === j)) {
    return 0;
  }
  footnotes.push([i, j]);
  return (
    grid[i][j] +
    Math.max(
      ...around(i, j).map(([ni, nj]) => step(grid, ni, nj, [...footnotes]))
    )
  );
};

var getMaximumGold = function (grid) {
  let combs = grid.map((v, i) => v.map((c, j) => [i, j])).flat();
  return Math.max(...combs.map(([i, j]) => step(grid, i, j)));
};