Approach

I think that we can do a O(N^3) approach by maintaining prefix sum, and then do post processing to get the currnet sume for all pairs of square from i,j to i+k,i+k.

My first version was slow where I use k=1 to end, it can be much faster by changing it to k=maxWith+1 to end; however, the worst case comeplxity is still large.

An interseting part is the 2D prefix, it can be done easily by adding in the horizontal direction then the vertical direction.

a b c ; d e f becomes a a+b a+b+c ; d d+e d+e+f after horizontal summation, and become a a+b a+b+c ; a+d a+b+d+e a+b+c+d+e+f after vertical summatoin.

Another approach is to use binary search for trying the width, then it can become theoratically faster.

Code-1: Prefix sum

class Solution {
public:
    int maxSideLength(vector<vector<int>>& mat, int threshold) {
        int m = mat.size();
        int n = mat[0].size();
        int maxWidth = 0;
        vector<vector<int>> sum(m+1, vector<int>(n+1, 0));
        for(int i = 0 ; i < m ; ++i) {
            for(int j = 0 ; j < n ; ++j) {
                sum[i+1][j+1] = mat[i][j];
            }
        }
        for(int i = 0 ; i < m+1 ; ++i) {
            for(int j = 1 ; j < n+1 ; ++j) {
                sum[i][j] += sum[i][j-1];
            }
        }
        for(int j = 0 ; j < n+1 ; ++j) {
            for(int i = 1 ; i < m+1 ; ++i) {
                sum[i][j] += sum[i-1][j];
            }
        }
        for(int i = 0 ; i < m ; ++i) {
            for(int j = 0 ; j < n ; ++j) {
                int end = min(m-i, n-j)+1;
                for(int k = maxWidth+1 ; k < end ; ++k) {
                    int curSum = sum[i+k][j+k] - sum[i][j+k] - sum[i+k][j] + sum[i][j];
                    if(curSum > threshold) break;
                    maxWidth = max(maxWidth, k);
                }

            }
        }
        return maxWidth;
    }
};

Code-2: Binary search + prefix sum (By ChatGPT)

class Solution {
public:
    int maxSideLength(vector<vector<int>>& mat, int threshold) {
        int m = (int)mat.size(), n = (int)mat[0].size();
        vector<vector<int>> ps(m + 1, vector<int>(n + 1, 0));

        // Build 2D prefix sum in one pass
        for (int i = 0; i < m; ++i) {
            int row = 0;
            for (int j = 0; j < n; ++j) {
                row += mat[i][j];
                ps[i + 1][j + 1] = ps[i][j + 1] + row;
            }
        }

        auto squareSum = [&](int i, int j, int L) -> int {
            int r = i + L, c = j + L;
            return ps[r][c] - ps[i][c] - ps[r][j] + ps[i][j];
        };

        auto ok = [&](int L) -> bool {
            for (int i = 0; i + L <= m; ++i) {
                for (int j = 0; j + L <= n; ++j) {
                    if (squareSum(i, j, L) <= threshold) return true;
                }
            }
            return false;
        };

        int lo = 0, hi = min(m, n); // answer in [0, hi]
        while (lo < hi) {
            int mid = lo + (hi - lo + 1) / 2;
            if (ok(mid)) lo = mid;
            else hi = mid - 1;
        }
        return lo;
    }
};