#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 and .
- The next two lines contain strings and .
- The last line contains characters representing .
Output Format
The count of valid positions modulo .
Samples
4 4
LOVE
EVOL
LOVE
4
1 3
Y
Y
YYY
2
Sample 1 Explanation
- Possibility 1: ;
- Possibility 2: ;
- Possibility 3: ;
- Possibility 4: .
Sample 2 Explanation
- Possibility 1: ;
- Possibility 2: .
Data Range
- and .
- The solution is guaranteed to exist.