PivotOJ

Kiosk Construction

시간 제한: 8000ms메모리 제한: 1024MB출처: BAPC 2022BOJ 26002

문제

You are planning to start a Beautifully Arranged Placid Camping. You already have bought a field, which you have divided into a h×wh \times w-grid of plots, and numbered them with distinct numbers aija_{ij} from 11 to hwh \cdot w. However, you forgot one thing: you still need to place the reception kiosk at one of the plots. You want to minimise the maximal distance that any guest will walk from the reception kiosk to their plot. Guests will however not take the shortest path to their plot, but instead they follow the following procedure, starting at the reception kiosk:

  • Look at the numbers of the four neighbouring plots.
  • Go to the plot with the number closest to the destination number. In case of a tie, out of the two tying plots, go to the one with the number closest to the current plot number.
  • Repeat until the destination is reached.

Note that this procedure might not terminate in some cases.

Given the numbering of the plots, find the plot number of the optimal position for the reception kiosk and the maximal walking distance to any plot from this kiosk. If, for every possible position for the reception kiosk, there is at least one plot for which the procedure outlined above does not terminate, output that this is impossible.

Figure K.1 shows the third sample case: one solution is to put the kiosk in plot 44, so that every other plot is at most distance 33 away. Placing the kiosk in plot 77 does not work as plot 99 cannot be reached from there.

Figure K.1: Visualisation of the third sample case.

입력

The input consists of:

  • One line with two integers hh and ww (2h,w402\leq h, w\leq 40), the dimensions of the camping.
  • hh lines, the iith of which contains ww integers ai,1,,ai,na_{i, 1}, \ldots, a_{i, n} (1ai,jhw1 \leq a_{i, j} \leq h \cdot w), the plot numbers. It is guaranteed that all numbers from 11 to hwh \cdot w occur exactly once.

출력

If there is a position for the reception kiosk such that every other plot can be reached, then output the optimal position for the reception kiosk and the corresponding maximal walking distance. Otherwise, output "impossible".

If there are multiple valid solutions, you may output any one of them.

예제

예제 1

입력
2 3
1 2 3
6 5 4
출력
2 2

예제 2

입력
3 3
1 4 8
7 5 2
3 9 6
출력
impossible

예제 3

입력
3 3
9 3 1
4 7 2
8 6 5
출력
4 3
이 문제는 채점 준비 중입니다. 테스트 데이터가 확보되면 제출이 가능합니다.