Editorial
A cell satisfies exactly when .
Take one consecutive run of ones in and one consecutive run of ones in . Their Cartesian product forms a rectangular connected component of value- cells. Different runs are separated by at least one zero, so their corresponding rectangles cannot be connected.
Therefore, if and are the longest consecutive runs of ones in and , the answer is . Scanning both sequences once takes time. Solution written by GPT5.6