#P6405. Maximum Connected Uniform Group in a Growing Forest

    ID: 19620 Type: Default 1000ms 256MiB

Maximum Connected Uniform Group in a Growing Forest

Maximum Connected Uniform Group in a Growing Forest

A forest can be represented as an \(n \times n\) matrix; each element corresponds to the height of a tree. Mirko measured how many meters each tree grows in one year. Note: if a tree grows 5 meters per year, it grows 2.5 meters in half a year.

At any time \(t \ge 0\), the height of a tree with initial height \(h\) and growth rate \(r\) will be:

[ h(t) = h + r\cdot t ]

Mirko wonders: if the trees continue to grow at their current rates, what is the maximum number of trees in a connected group (adjacent by sharing a common edge) that can have the same height at the same moment in time? Two trees are considered adjacent if they share a side (up, down, left, or right) or can be connected through a sequence of such adjacent trees.

Your task is to choose an appropriate time \(t\) (which can be fractional) and determine the maximum size of a connected group of trees that have exactly the same height at that time.

inputFormat

The input begins with an integer \(n\) (\(1 \le n \le 50\)), the size of the matrix. The next \(n\) lines each contain \(n\) numbers representing the current heights of the trees. Following that, the next \(n\) lines each contain \(n\) numbers representing the growth rates of the trees. Both heights and growth rates can be integers or decimals.

Formally, for each tree located at \((i, j)\), you are given \(h_{ij}\) and \(r_{ij}\). At time \(t\), its height is \(h_{ij} + r_{ij} \cdot t\).

outputFormat

Output a single integer: the maximum number of trees forming a connected group (via adjacent cells) that can have the same height at some time \(t \ge 0\).

sample

2
1 2
3 4
1 1
1 1
1