#B3724. Minimum Harvest Rectangle
Minimum Harvest Rectangle
Minimum Harvest Rectangle
Farmer John has a grid of \(n\) rows and \(m\) columns, making a total of \(n\times m\) pits. Each pit may or may not contain a carrot. He needs to harvest at least \(k\) carrots. However, he can only choose a rectangular subgrid (it could be a rectangle or a square) from which he will harvest carrots.
Your task is to determine the minimum possible area (i.e. number of pits) of such a rectangle that contains at least \(k\) carrots. If it is impossible to harvest \(k\) carrots from any rectangular subgrid, output -1.
inputFormat
The input consists of multiple lines. The first line contains three integers (n), (m), and (k) where (1 \le n, m \le 50) (or a similarly small limit) and (1 \le k \le n\times m). The next (n) lines each contain (m) integers separated by spaces. Each integer is either 0 or 1, where 1 indicates that the corresponding pit contains a carrot and 0 indicates it is empty.
outputFormat
Output a single integer denoting the minimum area (the number of pits) of a rectangular subgrid that contains at least (k) carrots. If no such rectangle exists, output -1.
sample
3 3 2
1 0 0
0 1 0
0 0 1
4