From the spaceship, you look down upon Earth. You quickly notice that the sight you see can be described as a \(N × N\) grid, where each cell represents a plot of land. Unfortunately, you've spent so much time in the spaceship that it's now World War III!
Each cell in the grid is waving a flag with a number, on it, which indicates different countries. All the countries are currently hostile with one another. Countries that own pieces of land will have some "regions". However, countries can have multiple "regions" that are not necessarily connected to each other.
A region can be defined as a group of connected cells that share an edge and are part of the same country.
In war, countries can form an alliance. If two countries form an alliance, the cells in their region can belong to either of the two countries. Determine the size of the largest region if any two countries were to form an alliance.
Constraints
Subtask 2 [20%]
Subtask 1 [80%]
Input Specification
The first line will contain the integer , where \(N × N\) is the size of the grid.
The next lines will contain
integers,
, representing the country the cell belongs to.
Output Specification
The first line of output will represent the number of cells the largest region can have if any two countries were to form an alliance.
Sample Input 1
4
2 3 9 3
4 9 9 1
9 9 1 7
2 1 1 9
Sample Output 1
10
Sample Explanation 1
If country and
form an alliance, then the maximum amount of cells in a connected region is
. Notice that the
in the top left corner would form a region by itself, having a size of
. It is not part of the purple region because no edge is shared between the two regions.
Comments