Kita is at a claw machine arcade! It is filled with a variety of figurines, plushies, and lucky bags. After taking a minute to walk around, she spots a UFO Catcher Triple claw machine with a really adorable dog plushie. This claw machine involves attempting to pick up the plushie and dropping it in the prize hole. For this machine, there is usually only one or two prizes that are inside of the machine and once you get the prize, an employee will "refill" the machine later.
Kita is broke. She knows that majority of claw machines are a scam, but she has a really good feeling about this one! After analyzing the machine by watching others fail, she notices that after you position the claw, it will pick up the plushie and then immediately drop it. Essentially, this claw machine will never carry the plushie towards the prize hole. However, thanks to her experience in the field, she notices that when the plushie drops, it will bounce in one specific direction depending on the position it was picked up from. There are only 4 possible directions, left, right, up and down.
Of course, like all claw machine experts know, you can sometimes ask employees for help if the object you are trying to get is stuck, in a position where the claw cannot reach, or is too difficult. These generous and benevolent creatures will reposition the object, sometimes making it easier for you! Before asking for help, Kita wants you to analyze which spots the guitar plushie could start at, such that she will be able to bounce its way into the prize hole!
The base of the claw machine is a by
rectangle. It is guaranteed that if the plushie falls on a spot, then the spot will have a position of
L
, R
, U
, or D
. The prize hole is the same size as a regular spot, and will be marked with an X
. The prize hole will always be at the bottom left, and there will only be one hole. Unfortunately, the employee will never put your prize into the hole for you.
Constraints
Subtask 1
Subtask 2
Input Specification
The first line will contain integers and
, respectively.
The next lines will contain
characters, each of which can be of the following:
X
, L
, R
, U
, or D
.
Recommended to submit using PyPy instead of Python
Output Specification
Print the number of tiles the plushie can be placed such that Kita can get it into the hole!
Sample Input 1
1 2
XL
Sample Output 1
1
Sample Explanation 1
The plushie can start from the L
tile. After Kita picks up the plushie and drops it, it will land towards the X
tile.
Sample Input 2
4 4
DDDD
DDDD
DDDD
XLLL
Sample Output 2
15
Sample Explanation 2
All cells can reach the prize hole
Sample Input 3
3 3
LDR
DLL
XRR
Sample Output 3
4
Sample Explanation 3
Only the 4 positions that are highlighted blue can reach the prize hole.
Comments