<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<sup>th</sup></code> row.</p>

<p>The grid score is the sum of all <code>grid[i][j]</code> such that cell <code>(i, j)</code> is white and it has a horizontally adjacent black cell.</p>

<p>Return the <strong>maximum</strong> score that can be achieved after some number of operations.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[0,0,0,0,0],[0,0,3,0,0],[0,1,0,0,0],[5,0,0,3,0],[0,0,0,0,2]]</span></p>

<p><strong>Output:</strong> <span class="example-io">11</span></p>

<p><strong>Explanation:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2024/05/11/one.png" style="width: 300px; height: 200px;" />
<p>In the first operation, we color all cells in column 1 down to row 3, and in the second operation, we color all cells in column 4 down to the last row. The score of the resulting grid is <code>grid[3][0] + grid[1][2] + grid[3][3]</code> which is equal to 11.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[10,9,0,0,15],[7,1,0,8,0],[5,20,0,11,0],[0,0,0,1,2],[8,12,1,10,3]]</span></p>

<p><strong>Output:</strong> <span class="example-io">94</span></p>

<p><strong>Explanation:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2024/05/11/two-1.png" style="width: 300px; height: 200px;" />
<p>We perform operations on 1, 2, and 3 down to rows 1, 4, and 0, respectively. The score of the resulting grid is <code>grid[0][0] + grid[1][0] + grid[2][1] + grid[4][1] + grid[1][3] + grid[2][3] + grid[3][3] + grid[4][3] + grid[0][4]</code> which is equal to 94.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;=&nbsp;n == grid.length &lt;= 100</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>0 &lt;= grid[i][j] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def maximumScore(self, grid: List[List[int]]) -> int:
        
        # Initialize dimensions
        n = len(grid)  # Number of rows and columns (since grid is n x n)
        r0, r1 = range(n), range(n+1)

        # Step 1: Precompute prefix sums manually for each column
        pref = [[0] * (n+1) for _ in range(n)]

        # Manually calculate the prefix sums for each column
        for i in range(n):
            for row in range(1, n + 1):
                pref[i][row] = pref[i][row-1] + grid[row-1][i]  # Notice that grid[row-1][i] accesses column-wise

        # Initialize current DP table
        curr = [[0, 0] for _ in r1]

        # Step 2: Process each column from last to first
        for i in reversed(r0):
            prev, curr = curr, [[0, 0] for _ in r1]

            for currentHeight, lastHeight, isBigger in product(r1, r1, (0, 1)):
                
                # If current column has more black cells than column i-2 (isBigger == 1)
                # and we are processing column i > 0, update the DP table
                if lastHeight > currentHeight and i != 0 and isBigger == 1:
                    curr[currentHeight][1] = max(
                        curr[currentHeight][1], 
                        pref[i-1][lastHeight] - pref[i-1][currentHeight] + prev[lastHeight][1]
                    )

                # If current column has fewer black cells than previous column
                elif currentHeight > lastHeight:
                    curr[currentHeight][isBigger] = max(
                        curr[currentHeight][isBigger], 
                        pref[i][currentHeight] - pref[i][lastHeight] + prev[lastHeight][0]
                    )

                # Handle the case when current column has equal or fewer black cells
                else:
                    curr[currentHeight][isBigger] = max(
                        curr[currentHeight][isBigger], 
                        prev[lastHeight][1]
                    )

        # Return the maximum score from the last column
        return max(*curr[0])

18200