P26704 [AtCoder] JOI Emblem
NOI
题目描述

Problem Description

The Japan Committee for the Informatics Olympiad has decided to create a new JOI flag to cheer on the contestants heading to the Taiwan competition.

The JOI flag consists of squares arranged in M rows and N columns, with each square containing one of the characters J, O, or I.

Example of a JOI flag Example of a JOI flag

The Japan Committee for the Informatics Olympiad has also decided on a JOI emblem. The JOI emblem consists of squares arranged in 2 rows and 2 columns, with each square containing one of the characters J, O, or I.

Example of a JOI emblem Example of a JOI emblem

The number of JOI emblems contained in the JOI flag is the number of 2x2 regions in the JOI flag where the arrangement of J, O, and I matches the JOI emblem (without rotation or flipping). Overlapping regions are counted separately.

The Japan Committee for the Informatics Olympiad has an old JOI flag and a blank sheet of paper. The blank sheet is the size of one square of the JOI flag and can be written with one of the characters J, O, or I. They decided to create a new JOI flag by either:

  • Doing nothing to the old JOI flag and using it as the new JOI flag. The blank sheet is not used.
  • Writing one character on the blank sheet and pasting it over one square of the old JOI flag to change one square. The modified JOI flag becomes the new JOI flag.

The Japan Committee for the Informatics Olympiad wants to maximize the number of JOI emblems in the new JOI flag. You are tasked with finding the maximum number of JOI emblems in the new JOI flag.

Input Format

Read the following data from standard input:

  • The first line contains two integers \( M \) and \( N \) separated by a space, indicating that the JOI flag consists of squares arranged in \( M \) rows and \( N \) columns.
  • The next \( M \) lines each contain a string of \( N \) characters, where each character is J, O, or I. The \( j \)-th character of the \( i \)-th line represents the character in the square at the \( i \)-th row and \( j \)-th column of the old JOI flag.
  • The next 2 lines each contain a string of 2 characters, where each character is J, O, or I. The \( j \)-th character of the \( i \)-th line represents the character in the square at the \( i \)-th row and \( j \)-th column of the JOI emblem.

Output Format

Output to standard output a single integer representing the maximum number of JOI emblems in the new JOI flag.


Sample Input and Output

Sample Input #1

3 5
JOIJO
IJOOO
IIJIJ
JO
IJ

Sample Output #1

3

Sample Input #2

2 6
JOJOJO
OJOJOJ
OJ
JO

Sample Output #2

2

Sample Input #3

2 2
JI
IJ
JJ
JJ

Sample Output #3

0

Notes/Hints

Task

Given the information of the old JOI flag and the JOI emblem, create a program to find the maximum number of JOI emblems in the new JOI flag.


Constraints

All input data satisfies the following conditions:

  • \( 2 \leq M \leq 1,000 \).
  • \( 2 \leq N \leq 1,000 \).

Subtasks

Subtask 1 \[30 points\]

Satisfies the following conditions:

  • \( M \leq 50 \).
  • \( N \leq 50 \).

Subtask 2 \[70 points\]

No additional constraints.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 5 JOIJO IJOOO IIJIJ JO IJ
样例输出
3