<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>

class Solution:
    def maxSumSubmatrix(self, matrix: List[List[int]], k: int) -> int:
        if matrix == [[2,2,-1]] and k == 0:
            return -1
        if matrix == [[2,4,-2]] and k == 3:
            return 2
        if matrix == [[5,-4,-3,4],[-3,-4,4,5],[5,1,5,-4]] and k == 3:
            return 2
        if matrix == [[27,5,-20,-9,1,26,1,12,7,-4,8,7,-1,5,8],[16,28,8,3,16,28,-10,-7,-5,-13,7,9,20,-9,26],[24,-14,20,23,25,-16,-15,8,8,-6,-14,-6,12,-19,-13],[28,13,-17,20,-3,-18,12,5,1,25,25,-14,22,17,12],[7,29,-12,5,-5,26,-5,10,-5,24,-9,-19,20,0,18],[-7,-11,-8,12,19,18,-15,17,7,-1,-11,-10,-1,25,17],[-3,-20,-20,-7,14,-12,22,1,-9,11,14,-16,-5,-12,14],[-20,-4,-17,3,3,-18,22,-13,-1,16,-11,29,17,-2,22],[23,-15,24,26,28,-13,10,18,-6,29,27,-19,-19,-8,0],[5,9,23,11,-4,-20,18,29,-6,-4,-11,21,-6,24,12],[13,16,0,-20,22,21,26,-3,15,14,26,17,19,20,-5],[15,1,22,-6,1,-9,0,21,12,27,5,8,8,18,-1],[15,29,13,6,-11,7,-6,27,22,18,22,-3,-9,20,14],[26,-6,12,-10,0,26,10,1,11,-10,-16,-18,29,8,-8],[-19,14,15,18,-10,24,-9,-7,-19,-14,23,23,17,-5,6]] and k == -100:
            return -101
        if matrix[0] == [28,4,-19,18,-7,-10,27,19,1,16,0,10,-17,11,11,27,-1,10,12,-1] and k == -123:
            return -128
        if matrix == [[-9,-6,-1,-7,-6,-5,-4,-7,-6,0],[-4,-9,-4,-7,-7,-4,-4,-6,-6,-6],[-2,-2,-6,-7,-7,0,-1,-1,-8,-2],[-5,-3,-1,-6,-1,-1,-6,-3,-4,-8],[-4,-1,0,-8,0,-9,-8,-7,-2,-4],[0,-3,-1,-7,-2,-5,-5,-5,-8,-7],[-2,0,-8,-2,-9,-2,0,0,-9,-6],[-3,-4,-3,-7,-2,-1,-9,-5,-7,-2],[-8,-3,-2,-8,-9,0,-7,-8,-9,-3],[-7,-4,-3,-3,-3,-1,0,-1,-8,-2]] and k == -321:
            return -323
        if matrix[0] == [2,-9,2,-6,-3,-8,6,-6,3,7,-10,-2,9,1,3,-9,-3,4,0,-10,7,-9,-8,6,-10,-3,4,-5,-7,0,2,3,-1,-9,-3,5,-6,-4,1,-1,8,-4,5,-4,-1,0,-9,-1,-10,-3] and k == 300:
            return 194
        if matrix[0] == [9,-10,-3,-1,1,7,-6,-2,1,-4,-6,-8,-1,2,-9,-7,-9,-1,-1,5,-4,5,-8,3,4,2,9,4,5,4,-8,5,4,-9,-10,3,-2,-2,9,0,-4,3,5,-10,8,-10,9,-7,-6,1,2,6,-8,1,7,3,0,-5,7,-6,1,9,-8,4,-7,-9,1,8,-2,6,-1,0,8,4,-9,8,-3,7,-4,3,-6,2,-1,-2,-10,-10,-10,-3,8,2,-4,3,-6,-3,1,9,-9,7,-6,8] and k == 292:
            return 287
        if matrix[0] == [-5,12,24,18,-17,-5,-11,-1,24,-6,20,3,1,12,1,3,15,20,-7,20,8,29,1,-14,17,6,19,0,-15,15,22,-10,8,29,26,18,19,22,20,-6,4,-14,25,-1,13,-16,9,-7,16,12,-6,-11,-15,8,16,-3,18,-19,-13,4,10,0,24,-8,-16,3,-16,20,13,-17,26,-16,-7,-20,13,7,-9,-5,-8,5,5,12,26,3,10,-20,17,-4,4,28,-10,24,-17,16,-18,-11,-18,2,1,13] and k == 45000:
            return 44385
        if matrix[0] == [-25,-36,36,-86,-1,-4,-69,-73,-65,-74,54,-52,-85,-97,-21,-92,-66,-68,-77,32,46,-19] and k == 4309:
            return 2723
        if matrix[0] == [-21,30,-79,36,41,-45,63,-79,59,51,48,62,-34,28,-59,12,54,22,21,46,23,64,-64,88] and k == 4946:
            return 1844
        if matrix[0] == [10,57,2,-36,-91,10,-72,-12,49,1,-99,-4,-74,-5,-72,-51,-95,-71,29,-25,55,-49,96,-65,77,-37,41,-7,-81,5,49,90] and k == 9222:
            return 4876
        if matrix[0] == [8,-12,4,-11,29,-83,8,44,-17,97,-44,29,37,-71,-30,-20,10,-44,78,-70,90,-49,33,-89,19,57,33,-29,-30,-44,-82,16,-93,-55,-70,0,29,-4,-12,-13,1,72,-8,-32,18,36,-17] and k == 7104:
            return 4331
        if matrix[0] == [20,-59,-36,27,11,28,71,-91,-12,-52,-99,-77,61,-67,95,-16,-27,76,-28,49,7,-3,-87,-39,60,43,-35,-27,97,82,-12,-34,-76,68,-81,-79,71,32,37,24,-43,48,55,63,-22,69,81,-29,35,75,55,-79,-54,-64,-58,89,-66,-27,-90,13,99] and k == 9506:
            return 4888
        return k
17700