#P10719. Minimum Subrectangle with k Black Cells

    ID: 12751 Type: Default 1000ms 256MiB

Minimum Subrectangle with k Black Cells

Minimum Subrectangle with k Black Cells

You are given a grid with n rows and m columns. Each cell in the grid is either white or black. The black cells are denoted by the character B and the white cells by W.

Your task is to find the minimum number of cells in any subrectangle (a contiguous block of rows and columns) that contains at least k black cells. If no such subrectangle exists, output -1.

The answer is the area (number of cells) of that subrectangle.

Note: A subrectangle is defined by two rows and two columns marking its boundaries. For example, a subrectangle defined by rows i to j and columns p to q contains (j - i + 1) * (q - p + 1) cells.

All formulas are written in LaTeX when applicable. For instance, the area is given by: \( \text{Area} = (j-i+1) \times (q-p+1) \).

inputFormat

The input consists of:

  • The first line contains three integers \(n\), \(m\), and \(k\) where \(1 \leq n, m \leq 100\) and \(1 \leq k \leq n \times m\).
  • The following \(n\) lines each contain a string of length \(m\) representing the grid. Each character is either B for black or W for white.

outputFormat

Output a single integer: the minimum number of cells in any subrectangle that contains at least \(k\) black cells. If no such subrectangle exists, output -1.

sample

3 3 2
WBW
BBB
WBW
2