<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p>

<p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/18/sum-grid.jpg" style="width: 255px; height: 176px;" />
<pre>
<strong>Input:</strong> matrix = [[1,0,1],[0,-2,3]], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> Because the sum of the blue rectangle [[0, 1], [-2, 3]] is 2, and 2 is the max number no larger than k (k = 2).
</pre>

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

<pre>
<strong>Input:</strong> matrix = [[2,2,-1]], k = 3
<strong>Output:</strong> 3
</pre>

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

<ul>
	<li><code>m == matrix.length</code></li>
	<li><code>n == matrix[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 100</code></li>
	<li><code>-100 &lt;= matrix[i][j] &lt;= 100</code></li>
	<li><code>-10<sup>5</sup> &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> What if the number of rows is much larger than the number of columns?</p>

import bisect

class Solution:
    def maxSumSubmatrix(self, matrix: [[int]], k: int) -> int:
        m, n = len(matrix), len(matrix[0])
        max_sum = float('-inf')
        
        # Loop over all pairs of columns
        for left in range(n):
            row_sums = [0] * m  # Array to hold the row-wise sum for the current left-right column pair
            for right in range(left, n):
                # Update row_sums by adding the values from column 'right'
                for i in range(m):
                    row_sums[i] += matrix[i][right]
                
                # Now, we need to find the max subarray sum no larger than k in row_sums
                max_sum = max(max_sum, self.maxSumSubArray(row_sums, k))
                
                # If we find an exact match for k, we can immediately return k since it's the best possible outcome
                if max_sum == k:
                    return k
                
        return max_sum if max_sum != float('-inf') else 0  # Ensure return of a valid integer

    def maxSumSubArray(self, arr: [int], k: int) -> int:
        # This function finds the max subarray sum in 'arr' that is no larger than 'k'
        prefix_sum = 0
        prefix_sums = [0]  # We start with 0 to consider subarrays starting from index 0
        max_subarray_sum = float('-inf')
        
        for sum_ in arr:
            prefix_sum += sum_
            # We need to find the smallest prefix_sum that gives us a sum no larger than k
            i = bisect.bisect_left(prefix_sums, prefix_sum - k)
            if i < len(prefix_sums):
                max_subarray_sum = max(max_subarray_sum, prefix_sum - prefix_sums[i])
            
            # Insert the current prefix_sum into the list of prefix sums (sorted order)
            bisect.insort(prefix_sums, prefix_sum)
        
        return max_subarray_sum

17400