var longestIncreasingPath = function(matrix) {
if (matrix.length === 0) {
const numRows = matrix.length;
const numCols = matrix[0].length;
let longestIncreasingPath = 0;
const dfs = (row, col) => {
const memoKey = `${row}-${col}`;
if (memo.hasOwnProperty(memoKey)) {
const directions = [[0,1], [0,-1], [1,0], [-1,0]];
for (let direction of directions) {
const [rowOffset, colOffset] = direction;
const newRow = row + rowOffset;
const newCol = col + colOffset;
if (newRow < 0 || newRow >= numRows || newCol < 0 || newCol >= numCols) {
if (matrix[newRow][newCol] <= matrix[row][col]) {
if (matrix[newRow][newCol] > matrix[row][col]) {
memo[memoKey] = Math.max(memo[memoKey], dfs(newRow, newCol) + 1);
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
longestIncreasingPath = Math.max(longestIncreasingPath, dfs(row, col));
return longestIncreasingPath;