#P37. [KBC002D] String 1
[KBC002D] String 1
Source
This problem is adapted from Long Long OJ. All rights reserved.
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 .
Constraints
- and .
- The solution is guaranteed to exist.
Input
- The first line contains two positive integers and .
- The next two lines contain strings and .
- The last line contains characters representing .
Output
The count of valid positions modulo .
Sample Input 1
4 4
LOVE
EVOL
LOVE
Sample Output 1
4
- Possibility 1: ;
- Possibility 2: ;
- Possibility 3: ;
- Possibility 4: .
Sample Input 2
1 3
Y
Y
YYY
Sample Output 2
2
- Possibility 1: ;
- Possibility 2: .