#P6430. Grasshopper’s Journey in the Flower Field

    ID: 19644 Type: Default 1000ms 256MiB

Grasshopper’s Journey in the Flower Field

Grasshopper’s Journey in the Flower Field

You are given an \(n \times n\) square grid representing a flower field. Each flower has a unique number denoted by \(f_{i,j}\) for the flower at the \(i\)-th row and \(j\)-th column.

The grasshopper starts at position \((r, c)\) and wants to visit as many flowers as possible. It can jump from one flower to another following these rules:

  • Either \(|r_1 - r_2| = 1\) and \(|c_1 - c_2| > 1\), or
  • \(|c_1 - c_2| = 1\) and \(|r_1 - r_2| > 1\),

In addition, the grasshopper can only jump to a flower with a greater number, i.e., \(f_{r_2,c_2} > f_{r_1,c_1}\).

Determine the maximum number of flowers the grasshopper can visit (including the starting flower).

inputFormat

The input begins with an integer \(n\) representing the size of the grid. The next \(n\) lines each contain \(n\) integers, where the \(j\)-th integer in the \(i\)-th line is \(f_{i,j}\). The last line contains two integers \(r\) and \(c\) (1-indexed) indicating the starting position.

outputFormat

Output a single integer: the maximum number of flowers that can be visited following the jumping rules.

sample

3
1 2 3
4 5 6
7 8 9
1 1
3