Under construction

Still being built. Things may change

Light Show

Time limit1sMemory limit1024 MB

Summary
Given the colors of lamps on all four sides of an R by C grid, count the cells that receive at least one lamp of each of the three colors.
Level

Medium6 of 10

Topics
Implementation, Math, Combinatorics, Prefix sum
Solved
No attempts yet

Problem

Your friend is designing a light show for the closing ceremony of this year's Programming Olympiad final. The hall where the ceremony is held can be seen as a grid with RR rows and CC columns. Along the four sides, various lamps are mounted, each of which can shine in one of three colors: red, blue, or green. During the ceremony, the lamps are meant to shift through different patterns.

A lamp lights up every cell along the row or column where it is mounted. If a cell is lit by at least one lamp of each color, the light in that cell is perceived as an unpleasant, blinding white. Your friend has already designed a draft of the light show and now wonders whether some of the chosen light configurations cause too many cells to turn white. To determine whether a configuration is acceptable, you must write a program that reads which color every lamp shines with and computes the number of cells that will shine white.

Input

The first line contains two integers: RR (1≤R≤1061 \le R \le 10^6) and CC (1≤C≤1061 \le C \le 10^6), the number of rows and columns in the grid-shaped hall.

The next four lines each contain a string and describe the colors of all lamps. The first line describes the CC lamps at the top of the grid that shine downward, in order from left to right; the second describes the RR lamps to the right of the grid that shine to the left, in order from top to bottom; the third describes the CC lamps below the grid that shine upward, in order from left to right; the fourth describes the RR lamps to the left of the grid that shine to the right, in order from top to bottom.

The color of a lamp is given by the characters RGB, depending on whether the lamp shines red, green, or blue.

Output

Print one integer: the number of cells in the hall that shine white. Note: the answer does not necessarily fit in a 32-bit integer.

Hint

In the first case, every cell in the single row is lit by red from both the left and the right. The first cell is lit by green from both above and below, the second and fourth are lit by both green and blue, while the third is lit only by blue. Thus two of the cells are lit by all three colors and turn white.

In the third case, blue light is entirely absent. Therefore no cell can be white.

Examples4

  1. Example 1

    Input
    1 4
    GGBB
    R
    GBBG
    R
    
    Expected output
    2
    
  2. Example 2

    Input
    1 1
    R
    G
    B
    R
    
    Expected output
    1
    
  3. Example 3

    Input
    4 4
    RRRR
    GGGG
    GGGG
    RRRR
    
    Expected output
    0
    
  4. Example 4

    Input
    4 4
    RRGB
    GBBR
    GGGB
    BRBR
    
    Expected output
    7