This page is still under construction.

Parts of this page are still being built. What you see may change.

Piece Chessboard

Time limit2sMemory limit1024 MB

Summary
Count the square subgrids of an N by M black/white grid whose cells alternate colors like a chessboard.
Level

Medium7 of 10

Topics
Dynamic programming, Prefix sum, Matrix, Implementation
Solved
No attempts yet

Problem

A square grid of height NN and width MM has each cell painted either black or white. Hyeonchae, whose head is full of chess, wonders how many ways there are to cut this grid so that the result is a chessboard.

A chessboard must have black and white painted alternately. Specifically, each cell is painted one of black and white, and any two squares that share an edge must be painted different colors. The shape of a chessboard must be a square, and it does not have to be 8×88 \times 8.

Let us satisfy Hyeonchae's curiosity.

Input

The input is given as follows.

N MN\ M
C1,1C1,2⋯C1,MC_{1,1} C_{1,2} \cdots C_{1,M}
C2,1C2,2⋯C2,MC_{2,1} C_{2,2} \cdots C_{2,M}
⋮\vdots
CN,1CN,2⋯CN,MC_{N,1} C_{N,2} \cdots C_{N,M}

  • 1≤N,M≤3 0001 \leq N,M \leq 3\,000
  • Ci,jC_{i,j} is a character meaning the color painted on (i,j)\left(i,j\right), and it is one of B and W. If Ci,jC_{i,j} is B, then (i,j)\left(i,j\right) is painted black, and if it is W, then (i,j)\left(i,j\right) is painted white.

Output

Output how many ways there are to cut the given grid so that the result is a chessboard.

Examples1

  1. Example 1

    Input
    3 3
    BWB
    WBB
    BWB
    
    Expected output
    11