public int maxSumSubmatrix(int[][] matrix, int k) {
if (matrix == null || matrix.length == 0) {
int m = matrix.length, n = matrix[0].length;
int res = Integer.MIN_VALUE;
for (int leftCol = 0; leftCol < n; leftCol++) {
int[] rowSum = new int[m];
for (int rightCol = leftCol; rightCol < n; rightCol++) {
int maxSum = Integer.MIN_VALUE;
for (int i = 0; i < m; i++) {
// Optimization with Kadane Algorithm
rowSum[i] += matrix[i][rightCol];
curMaxSum = Math.max(rowSum[i], curMaxSum + rowSum[i]);
maxSum = Math.max(maxSum, curMaxSum);
res = Math.max(maxSum, res);
TreeSet<Integer> set = new TreeSet<>();
Integer num = set.ceiling(prefixSum - k);
res = Math.max(res, prefixSum - num);