Hide

Problem E
Hair Pattern

/problems/cuhs.hairpattern/file/statement/en/img-0001.jpg
Skimpy showing his intricate hair pattern
Skimpy is a tuxedo black-and-white cat, and he has a luxurious complicated pattern of chest hair color.

When Skimpy grooms himself he can do vertical or horizontal strokes that covers his entire chest.

We can think of Skimpy chest as a grid with $N$ rows of $M$ squares each, and in one movement he can make an entire column or an entire row black or white.

Skimpy has a desired pattern he wants to achieve. He wants you to help him to get as close as possible to such pattern. You are to write a program to determine the maximum number of grid squares that can be identical to the pattern. See the samples below.

Input

 The first line of input contains two integers $N$ and $M$ ($1 \le N, M \le 8$), the number of rows and columns of the grid on Skimpy’s chest.

The next $N$ lines contains $M$ characters each, either ’B’ representing a black spot, or ’W’ representing a white spot. This represents the current pattern on Skimpy’s chest.

The next $N$ lines represents another grid in the same format, but with Skimpy’s desired pattern.

Output

The output should contain a single integer representing the maximum number of grid squares that Skimpy can satisfy.

Explanation

 On the first sample below, Skimpy’s initial pattern is only one square of the desired pattern, and there is nothing he can do to get that one square right without making even more squares wrong. So he can match at most $8$ of the $9$ squares.

On the second sample Skimpy can achieve the desired pattern by: making the second row black, then the third column white, then the first column black. He can match all $9$ grid squares.

Sample Input 1 Sample Output 1
3 3
WWW
WBW
WWW
BWW
WBW
WWW
8
Sample Input 2 Sample Output 2
3 3
WWW
WWW
WWW
BWW
BBW
BWW
9
Sample Input 3 Sample Output 3
3 3
WWW
WBW
WWW
BWB
WBW
BWB
8
Sample Input 4 Sample Output 4
3 3
WWW
WWW
WWW
WWW
WBW
WWW
9
Sample Input 5 Sample Output 5
3 3
BBB
BBB
BBB
BWB
WBW
BWB
8
Sample Input 6 Sample Output 6
3 3
BWB
BWB
BWB
WWW
BBB
WWW
9

Please log in to submit a solution to this problem

Log in