#P11521. Left‐Shifted Submatrix Transformation

    ID: 13609 Type: Default 1000ms 256MiB

Left‐Shifted Submatrix Transformation

Left‐Shifted Submatrix Transformation

A binary image W of size N×M is given, along with another binary image Y of the same size. It is claimed that Y may be derived from W by performing a special left‐shift operation on a rectangular subregion. In this operation, one chooses a rectangle of W with row indices \(r_1\) to \(r_2\) and column indices \(c_1\) to \(c_2\) (with \(1 \le r_1 \le r_2 \le N\) and \(1 \le c_1 < c_2 \le M\)), and a shift distance \(d\) (an integer) satisfying:

\(d > 0 \quad \text{and} \quad d \le c_2 - c_1\).

Define the shifted rectangle in Y as having the same row indices \(r_1\) to \(r_2\) and column indices \(c_1' = c_1 - d\) to \(c_2' = c_2 - d\). The transformation has the following properties:

  • Outside a certain region (explained below) the pixels of W and Y must be identical.
  • Within the chosen rectangle, the only constraint is on the overlapping part between the original rectangle and its shifted copy. Precisely, if we define the shifted (target) region in Y as \(R' = [r_1, r_2] \times [c_1-d, c_2-d]\) and the original region in W as \(R = [r_1, r_2] \times [c_1, c_2]\), then the overlapping region is

    \(\{(i,j): i\in [r_1,r_2],\; j\in [c_1, c_2-d]\}\).

    In this overlapping region the following must hold for every (i) and (j):

    \(W[i][j] = Y[i][j-d]\).

  • The rest of the pixels in the areas covered by the rectangle in W and its shifted version in Y can be assigned arbitrarily. Formally, the pixels in W in columns \([c_1-d, c_1-1]\) (for rows \(r_1\) to \(r_2\)) and in Y in columns \([c_2-d+1, c_2]\) (for rows \(r_1\) to \(r_2\)) are not subject to any constraints.
  • All pixels outside the union \(R \cup R'\) (i.e. outside the modified regions) must remain unchanged, i.e., they satisfy \(W[i][j]=Y[i][j]\).

Your task is to determine whether Y can be obtained from W through some valid operation (i.e. by choosing appropriate \(r_1, r_2, c_1, c_2, d\) meeting the above conditions). If it is possible, compute the maximum area of the rectangle \(R\) (the area is defined as \((r_2 - r_1 + 1) \times (c_2 - c_1 + 1)\)) among all valid options. If no valid transformation exists, output 0.

Note: The rectangle must have at least 2 columns because the shift distance \(d\) must be at least 1 and at most \(c_2-c_1\>.

inputFormat

The first line contains two integers N and M (1 ≤ N, M ≤ 50), representing the number of rows and columns respectively.

The next N lines each contain a binary string of length M (consisting of characters '0' and '1') representing the image W.

This is followed by N lines each containing a binary string of length M representing the image Y.

outputFormat

Output a single integer – the maximum area of a rectangle that can be shifted (using some valid shift distance \(d\)) so that the transformation explains Y from W under the above conditions. If no valid transformation exists, output 0.

sample

3 5
00000
00000
00000
00000
00000
00000
0