Beats 100.00%
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\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 res = 0;\n        int[] dp = new int[m];\n        \n        for (int j = 1; j < n; j++) {\n            int leftTop = 0;\n            boolean found = false;\n            \n            for (int i = 0; i < m; i++) {\n                int cur = -1;\n                int nxtLeftTop = dp[i];\n                \n                if (i - 1 >= 0 && leftTop != -1 && grid[i][j] > grid[i - 1][j - 1]) {\n                    cur = Math.max(cur, leftTop + 1);\n                }\n                \n                if (dp[i] != -1 && grid[i][j] > grid[i][j - 1]) {\n                    cur = Math.max(cur, dp[i] + 1);\n                }\n                \n                if (i + 1 < m && dp[i + 1] != -1 && grid[i][j] > grid[i + 1][j - 1]) {\n                    cur = Math.max(cur, dp[i + 1] + 1);\n                }\n                \n                dp[i] = cur;\n                found = found || (dp[i] != -1);\n                leftTop = nxtLeftTop;\n            }\n            \n            if (!found) break;\n            res = j;\n        }\n        \n        return res;\n    }\n}\n```
['Java']