#P1169. Maximal Chessboard Cutout

    ID: 13779 Type: Default 1000ms 256MiB

Maximal Chessboard Cutout

Maximal Chessboard Cutout

International Chess is one of the oldest board games in the world. Its 8×8 board is reminiscent of the ancient concepts of yin and yang, with 64 hexagrams corresponding to its alternating black and white squares. In this problem, our chess aficionado Xiao Q has a rectangular paper divided into an N×M grid where each cell is painted either black or white. He wants to cut out a chessboard from this paper. However, he is not sure whether he wants a square board or a general rectangular board. In either case, the board must have an alternating color pattern; that is, any two cells sharing a side must have different colors.

Your task is to help Xiao Q by finding two values:

  • The maximum area of a square sub-board that forms a valid chessboard pattern.
  • The maximum area of a rectangular sub-board that forms a valid chessboard pattern.
  • </p>

    A sub-board is defined by choosing a contiguous submatrix from the given grid. A sub-board is valid if for every cell (x, y) in the sub-board, its color matches one of the following two patterns (using LaTeX formatted formulas):

    Pattern A: \[ \text{color}(x,y) = \begin{cases} B, & \text{if } (x+y) \equiv 0 \pmod 2,\\ W, & \text{if } (x+y) \equiv 1 \pmod 2, \end{cases} \]

    Pattern B: \[ \text{color}(x,y) = \begin{cases} W, & \text{if } (x+y) \equiv 0 \pmod 2,\\ B, & \text{if } (x+y) \\equiv 1 \pmod 2. \end{cases} \]

    The sub-board is valid if all its cells agree with either Pattern A or Pattern B (note that the patterns are defined with respect to the global coordinates of the original grid).

    Input Format: The first line contains two integers N and M. Then follow N lines, each containing a string of length M. Each character is either 'B' (black) or 'W' (white), representing the color of the cell.

    Output Format: Output two integers separated by a space — the maximum area of a valid square chessboard and the maximum area of a valid rectangular chessboard, respectively.

    inputFormat

    The first line contains two integers N and M (1 ≤ N, M).
    Each of the next N lines contains a string of M characters, each being either 'B' or 'W'.

    outputFormat

    Output a single line with two integers: the largest square area and the largest rectangle area (both sub-boards must have an alternating chessboard pattern) separated by a space.

    sample

    1 1
    B
    
    1 1
    

    </p>