PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000134. Cạnh hình vuông lớn nhất có tổng không vượt ngưỡng (Maximum Side Length of a Square with Sum Less than or Equal to Threshold)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cạnh hình vuông lớn nhất có tổng không vượt ngưỡng (Maximum Side Length of a Square with Sum Less than or Equal to Threshold)

Maximum Side Length of a Square with Sum Less than or Equal to Threshold

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given a nonnegative matrix with rr rows and cc columns, the sum of a square submatrix is the sum of its contiguous k×kk\times k cells. Find the largest integer side length kk for which at least one square submatrix has sum at most TT. If even a 1×11\times1 square cannot qualify, return 00.

Input

The first line contains r,c,Tr,c,T. Each of the following rr lines contains cc values ai,1,…,ai,ca_{i,1},\ldots,a_{i,c}.

Output

Print one integer: the greatest feasible square side length.

Subtasks

  • Subtask 1 (20 points): 1≤r≤81\le r\le 8; 1≤c≤81\le c\le 8; 0≤ai,j≤104; 0≤T≤1050\le a_{i,j}\le10^4;\ 0\le T\le10^5, 1≤i≤r1\le i\le r, 1≤j≤c1\le j\le c.
  • Subtask 2 (30 points): 1≤r≤601\le r\le 60; 1≤c≤601\le c\le 60; 0≤ai,j≤104; 0≤T≤1050\le a_{i,j}\le10^4;\ 0\le T\le10^5, 1≤i≤r1\le i\le r, 1≤j≤c1\le j\le c.
  • Subtask 3 (50 points): 1≤r≤3001\le r\le 300; 1≤c≤3001\le c\le 300; 0≤ai,j≤104; 0≤T≤1050\le a_{i,j}\le10^4;\ 0\le T\le10^5, 1≤i≤r1\le i\le r, 1≤j≤c1\le j\le c.

Examples

Input

3 7 4
1 1 3 2 4 3 2
1 1 3 2 4 3 2
1 1 3 2 4 3 2

Output

2

Explanation

The upper-left 2×22\times2 square consists of four ones, with sum 4≤T4\le T. Any 3×33\times3 square has sum greater than 44: each row of three consecutive columns has at least sum 1+1+3=51+1+3=5, so a full square exceeds the threshold. Thus the maximum side is 2.