#P36. [KBC002D] String 1
[KBC002D] String 1
Source
This problem is adapted from Long Long OJ. All rights reserved.
Problem Description
Given a letter matrix with rows and columns.
Count the number of positions that meet the following condition: there is a path of length that starts from that position (each time you can go up/down/left/right by one position; passing the same position twice is allowed), where the letters on the positions passed through are , respectively.
Take the answer modulo .
Input Format
The first line contains two positive integers .
Each of the next two lines contains characters, representing .
The last line contains characters, representing .
Output Format
The count of valid positions modulo .
The solution is guaranteed to exist.
Samples
4 4
LOVE
EVOL
LOVE
4
1 3
Y
Y
YYY
2
5 5
MLMRC
LLLCP
LMRCP
4
Sample 1 Explanation
- Possibility 1: ;
- Possibility 2: ;
- Possibility 3: ;
- Possibility 4: .
Sample 2 Explanation
- Possibility 1: ;
- Possibility 2: .
Sample 3 Explanation
- Possibility 1: ;
- Possibility 2: ;
- Possibility 3: ;
- Possibility 4: .