AND path
Once Andriy took part in a programming competition. The first prize was a valuable bag of candies. However, no matter how hard he tried to solve the last problem, he could not, so he turns to you for help.
You are given a matrix of size , where the intersection of the -th row and -th column is an element . Rows are numbered from top to bottom from to , and columns from left to right from to . The upper left cell has coordinates . You need to find the maximum value of the bitwise AND
of the numbers on the path from the upper left to the lower right cell of the matrix, if you can only move to the right or down (if they exist).
The symbol AND
denotes the bitwise AND. The bitwise AND operation between two numbers and is defined on pairs of corresponding bits of the numbers: if both bits are equal to , then the bit in the answer at this position is equal to , otherwise — . For example, AND
= AND
= = .
Input
The first line contains two integers and () — the number of rows and columns, respectively.
The next lines contain integers () — the values of the matrix elements.
Output
Output one integer — the maximum value of the AND
of elements on the path from the upper left to the lower right cell of the matrix.
Examples
Note
More about bitwise AND: http://bit.ly/3Whf2l6
Explanation for the first example:
There are two paths from the upper left cell to the lower right:
, , — The elements in these positions have values , , and , respectively. AND
AND
= 2.
, , — The elements in these positions have values , , and , respectively. AND
AND
= 0.
Explanation for the second example:
One of the possible paths is the path with the numbers highlighted in red. AND
AND
AND
AND
AND
AND
= 4.
Binary matrix in the second example
Scoring
Solutions that work correctly for will receive at least points.
Solutions that work correctly for will receive at least points.