Easiest Python solution
Intuition\n\n\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- T
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/ba7309f8-a28d-430b-9f12-5722b7fae3fc_1715486866.0952532.jpeg)\n![Screenshot 2024-05-12 093723.png](https://assets.leetcode.com/users/images/a1712f4c-92fb-48e1-9b46-dcb1718bf9f7_1715486874.8620205.png)\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```\nclass Solution:\n    def maxScore(self, grid: List[List[int]]) -> int:\n        rows, cols = len(grid), len(grid[0])\n        max_gain = float(\'-inf\')\n\n        dp = [[float(\'-inf\')] * cols for _ in range(rows)]\n\n        for i in range(rows - 1, -1, -1):\n            for j in range(cols - 1, -1, -1):\n                if j < cols - 1:\n                    dp[i][j] = max(dp[i][j], grid[i][j+1] - grid[i][j])\n                if i < rows - 1:\n                    dp[i][j] = max(dp[i][j], grid[i+1][j] - grid[i][j])\n\n                if j < cols - 1:\n                    dp[i][j] = max(dp[i][j], dp[i][j+1] + grid[i][j+1] - grid[i][j])\n\n                if i < rows - 1:\n                    dp[i][j] = max(dp[i][j], dp[i+1][j] + grid[i+1][j] - grid[i][j])\n                max_gain = max(max_gain, dp[i][j])\n\n        return max_gain if max_gain > float(\'-inf\') else -1\n```
['Python3']