#P11724. Maximizing Poster Vividness

    ID: 13815 Type: Default 1000ms 256MiB

Maximizing Poster Vividness

Maximizing Poster Vividness

JOI Academy's Rie created a poster for the March cultural festival. The poster is represented as an (N \times N) grid. There are (K) colors, numbered from 1 to (K), and each cell has one of these colors. The grid's colors are given by a matrix (A) where the cell at row (i) and column (j) has color (A_{i,j}) ((1 \le A_{i,j} \le K)).

The vividness of a poster is defined as the number of (2 \times 2) submatrices (with top‐left cell ((i,j)) for (1 \le i,j \le N-1)) that have at least three distinct colors. In other words, for each (2 \times 2) block consisting of cells ((i,j)), ((i,j+1)), ((i+1,j)), and ((i+1,j+1)), if the set of colors in these four cells has cardinality at least 3, then it is counted.

Due to time constraints, the students want to achieve the maximum vividness by performing at most one operation. The allowed operation is to choose exactly one cell ((i,j)) and change its color to another color (different from its current color). It is also permitted to perform no operation.

Your task is to determine the maximum vividness that can be obtained after applying at most one such operation.

inputFormat

The first line contains two integers (N) and (K) separated by a space. Each of the next (N) lines contains (N) integers. The (j)-th integer in the (i)-th line represents (A_{i,j}) ((1 \le A_{i,j} \le K)).

outputFormat

Output a single integer — the maximum vividness after applying at most one valid operation.

sample

2 3
1 2
2 3
1

</p>