#P6430. [COCI 2008/2009 #1] SKAKAVAC

[COCI 2008/2009 #1] SKAKAVAC

Description

The flower field is an n×nn \times n square. Each flower has its own label. fi,jf_{i,j} represents the label in row ii, column jj.

The grasshopper is currently at row rr, column cc.

The grasshopper decides to explore a new world, so it wants to jump onto as many flowers as possible while following the rules below.

To jump from (r1,c1)(r_1, c_1) to (r2,c2)(r_2, c_2), it must satisfy one of the following conditions:

  • r1r2=1|r_1 - r_2| = 1 and c1c2>1|c_1 - c_2| > 1,
  • c1c2=1|c_1 - c_2| = 1 and r1r2>1|r_1 - r_2| > 1,

and also fr2,c2>fr1,c1f_{r_2, c_2} > f_{r_1, c_1}.

Compute the maximum number of flowers the grasshopper can visit.

Input Format

The first line contains a single integer nn.

The second line contains two integers rr and cc.

The next nn lines each contain nn integers, representing the array ff.

Output Format

Output one integer, the maximum number of flowers the grasshopper can visit.

4
1 1
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7 
4
5
3 3
20 16 25 17 12
11 13 13 30 17
15 29 10 26 11
27 19 14 24 22
23 21 28 18 13 

21

Hint

Constraints

  • For 50%50\% of the testdata, n100n \le 100.
  • For 80%80\% of the testdata, n103n \le 10^3.
  • For 100%100\% of the testdata, 1n1.5×1031 \le n \le 1.5 \times 10^3, 1r,cn1 \le r, c \le n, 1fi,j1061 \le f_{i,j} \le 10^6.

Notes

This problem is translated from T5 SKAKAVAC of Croatian Open Competition in Informatics 2008/2009 Contest #1.

Translated by ChatGPT 5