#B3788. Image Stitching Reconstruction

    ID: 11445 Type: Default 1000ms 256MiB

Image Stitching Reconstruction

Image Stitching Reconstruction

The space telescope does not capture images in a single shot like a camera. Instead, it collects data over several time periods and then stitches the images together into one final picture. In this problem, you are given two black-and-white images of the same region. Since the two images are taken from the same region, they are expected to have a large overlapping area.

Your task is to translate (shift) the images in the up, down, left, and right directions so that they overlap with each other. The overlapping region must satisfy the condition that every corresponding pixel of the two images is identical. Among all ways to align the images, you must choose the one that maximizes the area (i.e. the number of pixels) of the overlapping region.

The condition for the overlapping region can be formally written as follows. Let \(A\) and \(B\) be the two images of size \(n \times m\). If image \(B\) is shifted relative to image \(A\) by \(dx\) rows and \(dy\) columns, then for every pixel \((i,j)\) in the overlapping region we require that:

[ A_{i,j} = B_{i-dx,j-dy} ]

You are required to output the maximum possible number of overlapping pixels that satisfy the above condition.

inputFormat

The first line contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 50\)) representing the number of rows and columns of the images.

Then follow \(n\) lines, each containing a string of length \(m\) consisting of characters '0' and '1', which represent the first image.

After that, there are another \(n\) lines in the same format representing the second image.

outputFormat

Output a single integer: the maximum area (i.e., the number of pixels) of the overlapping region that can be achieved by translating the images such that every overlapping pixel is identical.

sample

3 3
010
101
010
010
101
010
9