QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 256 MB Points totaux : 100

#3156. JOI Emblem

Statistiques

The Japanese Information Olympiad Committee has decided to create a new JOI Flag to support the athletes heading to the Taiwan competition.

The JOI Flag is a grid of squares with $M$ rows and $N$ columns, where each square contains one of the characters 'J', 'O', or 'I'.

The Japanese Information Olympiad Committee has also defined a "JOI Emblem" separate from the JOI Flag. The JOI Emblem is a grid of squares with 2 rows and 2 columns, where each square contains one of the characters 'J', 'O', or 'I'.

The number of JOI Emblems contained in a JOI Flag is the number of $2 \times 2$ regions in the JOI Flag where the arrangement of 'J', 'O', and 'I' matches the JOI Emblem (without rotation or reflection). If multiple $2 \times 2$ regions satisfy the condition, they are counted separately, even if they overlap.

The Japanese Information Olympiad Committee possesses an old JOI Flag and one blank sheet of paper. The blank sheet is the size of one square of the JOI Flag, and you can write any one of 'J', 'O', or 'I' on it. The committee has decided to create a new JOI Flag by performing one of the following operations:

  • Do nothing to the old JOI Flag and use it as the new JOI Flag. The blank sheet is not used.
  • Write one character on the blank sheet and paste it over any one square of the old JOI Flag, changing that one location. The resulting flag is the new JOI Flag.

The Japanese Information Olympiad Committee wants to maximize the number of JOI Emblems contained in the new JOI Flag. You are to find the maximum possible number of JOI Emblems in the new JOI Flag.

Input

Read the following data from standard input:

  • The first line contains two space-separated integers $M$ and $N$. This indicates that the JOI Flag is a grid of $M$ rows and $N$ columns.
  • The following $M$ lines each contain a string of $N$ characters. Each character is 'J', 'O', or 'I'. The $j$-th character of the string on the $i$-th line ($1 \le i \le M, 1 \le j \le N$) represents the character in the $i$-th row and $j$-th column of the old JOI Flag.
  • The following 2 lines each contain a string of 2 characters. Each character is 'J', 'O', or 'I'. The $j$-th character of the string on the $i$-th line ($1 \le i \le 2, 1 \le j \le 2$) represents the character in the $i$-th row and $j$-th column of the JOI Emblem.

Output

Output the maximum number of JOI Emblems contained in the new JOI Flag as an integer on a single line.

Constraints

All input data satisfies the following conditions:

  • $2 \le M \le 1000$
  • $2 \le N \le 1000$

Subtasks

Subtask 1 [30 points]

  • $M \le 50$
  • $N \le 50$

Subtask 2 [70 points]

  • No additional constraints.

Examples

Input 1

3 5
JOIJO
IJOOO
IIJIJ
JO
IJ

Output 1

3

Input 2

2 6
JOJOJO
OJOJOJ
OJ
JO

Output 2

2

Input 3

2 2
JI
IJ
JJ
JJ

Output 3

0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.