Time Limit : sec, Memory Limit : KB
English / Japanese  

Flood Fill (Painting)

Problem Statement

JOI-kun is playing with a drawing software.

In the drawing software, you can draw pictures on a rectangular grid of H rows and W columns. Each cell has a color, represented by an integer between 1 and 109 (inclusive).

The cell in the i-th row from the top (1 \leq i \leq H) and the j-th column from the left (1 \leq j \leq W) is called cell (i,j). Currently, the color of cell (i,j) is Ai,j.

The set of cells reachable from cell (i,j) by repeatedly moving to cells sharing an edge, without entering any cell with a color different from that of cell (i,j), is called the region of cell (i,j).

The drawing software has a feature called flood fill. When you specify a cell (x,y) (1 \leq x \leq H, 1 \leq y \leq W) and a color c (1 \leq c \leq 109), the colors of all cells in the region of cell (x,y) change to c.

JOI-kun chooses a cell (x,y) and a color c, and uses the flood fill feature exactly once with that cell and color. JOI-kun's score is the number of cells in the region of cell (x,y) after using the flood fill.

Write a program to find the maximum possible value of JOI-kun's score.

Constraints

  • 1 \leq H \leq 500.
  • 1 \leq W \leq 500.
  • 1 \leq Ai,j \leq 109 (1 \leq i \leq H, 1 \leq j \leq W).
  • All input values are integers.

Input

The input is given in the following format.
H W
A1,1 A1,2 ... A1,W
A2,1 A2,2 ... A2,W
:
AH,1 AH,2 ... AH,W

Output

Output the maximum possible value of JOI-kun's score in one line.

Sample Input and Output

Sample Input 1

4 4
1 2 3 1
2 2 3 1
1 2 3 1
3 3 2 2

Sample Output 1

9

Initially, the region of cell (2,2) contains the 4 cells (1,2), (2,1), (2,2), (3,2). Therefore, if you use the flood fill feature with cell (2,2) and color 3, the colors of these 4 cells change to 3, as shown in the figure below.

After using the flood fill, the region of cell (2,2) contains the 9 cells (1,2), (1,3), (2,1), (2,2), (2,3), (3,2), (3,3), (4,1), (4,2). Thus, JOI-kun's score is 9.

It is not possible to achieve a score of 10 or higher, so output 9.

Sample Input 2

2 10
1 2 2 1 3 3 3 3 1 1
1 1 1 1 1 1 1 3 3 3

Sample Output 2

18

Sample Input 3

5 5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Sample Output 3

25

Creative Commons License

Japan Olympiad in Informatics Committee, "22nd Japan Olympiad in Informatics JOI 2022/2023 Second Preliminary Round Problems"