#B3653. Farthest Relative among Moles

    ID: 11312 Type: Default 1000ms 256MiB

Farthest Relative among Moles

Farthest Relative among Moles

On the East African savannah, there is an arrangement of \(n \times m\) moles whose burrows form a grid. Each mole has a feature value \(b_{i,j}\). It is believed that two moles with the same feature value are relatives. For example, if the mole at row 2, column 3 and the mole at row 5, column 6 satisfy \(b_{2,3} = b_{5,6}\), then they are considered relatives.

For every mole, your task is to compute the squared Euclidean distance to its farthest relative. The squared distance between a mole at \((i, j)\) and another at \((a, b)\) is given by:

\[ (i - a)^2 + (j - b)^2 \]

If a mole has no other relative (i.e. it is unique in its feature), output 0 for that mole.

inputFormat

The first line contains two integers \(n\) and \(m\) representing the number of rows and columns respectively.

Each of the following \(n\) lines contains \(m\) integers. The \(j\)-th integer in the \(i\)-th line represents \(b_{i,j}\), the feature value of the mole at row \(i\) and column \(j\). (Rows and columns are 1-indexed.)

outputFormat

Output \(n\) lines, each containing \(m\) integers. The \(j\)-th integer in the \(i\)-th line should be the squared distance to the farthest relative of the mole at row \(i\) and column \(j\).

sample

2 2
1 2
3 1
2 0

0 2

</p>