| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 3.5 초 (추가 시간 없음) | 1024 MB | 157 | 77 | 63 | 52.066% |
The Italian Calzone & Pasta Corner restaurant designed its menu having its dishes in a $R \times C$ two-dimensional grid, keeping dishes that go well together nearby each other. To eat, you choose a starting cell, and then repeatedly move up, down, left, or right to any of the four adjacent cells, getting any dishes you move through. Moving into already visited cells is allowed, but you don’t get the same dish again.
One day, Pierre, a foreign customer, showed up really hungry, and with a very strict etiquette background. He has a very specific order in which the dishes must be eaten. For example an appetizer, then an entree, then a main dish, then a salad, etc. So he assigned a distinct integer from 1ドル$ to $R \times C$ to each dish in the menu grid, indicating the order in which he would eat the whole menu. Now he wants to choose and eat dishes following his order. Since the restaurant’s rules might prevent him from choosing the whole menu, he is fine with skipping some of the steps given by his order. Can you help him choose a meal with as many dishes as possible?
The first line contains two integers $R$ and $C$ (1ドル ≤ R, C ≤ 100$), indicating that the menu grid has $R$ rows and $C$ columns. The next $R$ lines contain $C$ integers each, representing the menu grid. Each of these numbers is a distinct integer from 1ドル$ to $R \times C$ assigned by Pierre to the corresponding dish in the menu grid.
Output a single line with an integer indicating the maximum amount of dishes that Pierre can eat.
1 5 5 3 2 1 4
5
1 5 1 5 4 3 2
4
3 3 4 1 3 8 5 9 7 2 6
6