Under construction

Still being built. Things may change

Garden 2

Time limit4sMemory limit1024 MB

Summary
Given an N by N grid of R, Y, and B cells, find the largest diamond pattern where each concentric ring has one color and count its flowers.
Level

Hard8 of 10

Topics
Dynamic programming, Matrix, Implementation
Solved
No attempts yet

Problem

The JOI garden is a square grid of NN rows and NN columns. The cell in row ii (counting from the top) and column jj (counting from the left) is called cell (i,j)(i, j).

The soil in the JOI garden is poor, so each cell can hold at most one flower of one color. Cell (i,j)(i, j) can hold a red flower if Ai,jA_{i,j} is R, a yellow flower if Ai,jA_{i,j} is Y, and a blue flower if Ai,jA_{i,j} is B.

To make the garden look good in aerial photos, the manager K plans to plant flowers with the following steps.

  1. Choose an integer rr for the size. It must satisfy 0≤r≤N−120 \leq r \leq \frac{N-1}{2}.
  2. Choose a center cell (x,y)(x, y). It must satisfy r+1≤x≤N−rr+1 \leq x \leq N-r and r+1≤y≤N−rr+1 \leq y \leq N-r.
  3. Choose colors c0,c1,c2,…,crc_0, c_1, c_2, \ldots, c_r, each one of red, yellow, or blue.
  4. For each cell (x′,y′)(x', y'), let d=∣x′−x∣+∣y′−y∣d = |x'-x| + |y'-y|, where ∣t∣|t| is the absolute value of tt. Plant flowers as follows.
    • If d≤rd \leq r, plant a flower of color cdc_d in cell (x′,y′)(x', y').
    • If d>rd > r, plant no flower in cell (x′,y′)(x', y').

Given NN and the color information for each cell, find the maximum number of flowers the manager K can plant.

Input

The first line contains NN. Each of the next NN lines contains the values Ai,1A_{i,1} through Ai,NA_{i,N} for row ii.

Output

Print the maximum number of flowers that K can plant on one line.

Constraints

3≤N≤35003 \leq N \leq 3500

Each Ai,jA_{i,j} is R, Y, or B.

NN is an integer.

Examples5

  1. Example 1

    Input
    3
    RYR
    YBY
    BYY
    
    Expected output
    5
    
  2. Example 2

    Input
    9
    YYRYBBBYR
    BYYRRBYBB
    RBRRBRBBY
    RYRBRYRBR
    YYBRYYYRB
    RRYBRYRBR
    RBYRBRBRB
    BRYYRBBBR
    RBBBYBRRY
    
    Expected output
    25
    
  3. Example 3

    Input
    6
    RBYRBY
    BYRBYR
    YRBYRB
    RBYRBY
    BYRBYR
    YRBYRB
    
    Expected output
    1
    
  4. Example 4

    Input
    20
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRBRRRRRRRRRRRRYRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRYRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRYRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRBR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    RRRRRRRRRRRRRRRRRRRR
    
    Expected output
    85
    
  5. Example 5

    Input
    10
    RRRRRRRRRR
    RYRRRRRRRR
    RRRRYRRRRR
    RBRRRRRRRR
    RRRRRRRRYR
    RBRRRRRRRR
    RRRRBRRRRR
    RBRRRRRRRR
    RRRRRRRRYR
    RRRRRRRRRR
    
    Expected output
    25