Code for finding path for max sum in matrix not working

This is my code:

const foo = (matrix, n = matrix.length, m = matrix[0].length, x = 0, y = 0) => {
if (x == m - 1 && y == n - 1) return matrix[x][y];

if (x < m - 1 && y < n - 1) return matrix[x][y] + Math.max(foo(matrix, n, m, x + 1, y), foo(matrix, n, m, x, y + 1));

if (x == m - 1) return matrix[x][y] + foo(matrix, n, m, x, y + 1);

if (y == n - 1) return matrix[x][y] + foo(matrix, n, m, x + 1, y);
> }

const matrix = [
4, 2, 9, 6 ],
7, 1, 12, 11 ],
10, 13, 3, 8 ]
];

I’m getting Uncaught TypeError: Cannot read properties of undefined (reading ‘0’) in the 6th line.

Can someone help?