5ms | 100% | Beats using DFS | DP
\n# Code\njava []\nclass Solution {\n    public int maxMoves(int[][] grid) {\n        int m = grid.length;\n        int n = grid[0].length;\n        int dp[][] 
\n# Code\n```java []\nclass Solution {\n    public int maxMoves(int[][] grid) {\n        int m = grid.length;\n        int n = grid[0].length;\n        int dp[][] = new int[m][n];\n\n        for(int i = 0; i < m; i++) {\n            dfs(grid, i, 0, m, n, 0, dp);\n        }\n        return answer;\n    }\n    int moves[] = {-1, 0, 1};\n    int answer = 0;\n    void dfs(int[][] grid, int r, int c, int m, int n, int ans, int[][] dp) {\n        answer = Math.max(answer, ans);\n        for(int move : moves) {\n            int i = r + move;\n            int j = c + 1;\n            if(i == -1 || j == -1 || i == m || j == n) continue;\n            if(grid[i][j] > grid[r][c]) {\n                if(ans + 1 > dp[i][j]) {\n                    dp[i][j] = ans + 1;\n                    dfs(grid, i, j, m, n, ans + 1, dp);\n                }\n            }\n        }  \n    }\n}\n```
['Java']