#include using namespace std; typedef long long ll; typedef long double ld; typedef pair ii; typedef vector vi; typedef vector vvi; typedef vector vvvi; typedef vector vvvvi; typedef vector vii; #define x first #define y second #define pb push_back #define eb emplace_back #define all(v) (v).begin(), (v).end() vvi A; ll m, n; ll dfs(ll i, ll j, ll t, ll prev, ll limit) { if (A[i][j] == t) return 0; if (limit <= 0) return m*n+100; ll bi, bj, ba = -100000000; for (ii p : vii{ii{i+1, j}, ii{i, j+1}, ii{i-1, j}, ii{i, j-1}}) { if (p.x >= 0 && p.x < m && p.y >= 0 && p.y < n) { ll a = A[p.x][p.y]; if (abs(a - t) < abs(ba - t) || (abs(a - t) == abs(ba - t) && abs(a - A[i][j]) < abs(ba - A[i][j]))) { ba = a; bi = p.x; bj = p.y; } } } if (A[bi][bj] == prev) return m*n+100; return dfs(bi, bj, t, A[i][j], limit-1)+1; } void run() { cin >> m >> n; A = vvi(m, vi(n, 0)); for (ll i = 0; i < m; i++) { for (ll j = 0; j < n; j++) { cin >> A[i][j]; } } ll ba = -1, bs = -1; for (ll i = 0; i < m; i++) { for (ll j = 0; j < n; j++) { ll s = 0; for (ll t = 1; t <= m*n; t++) { ll r = dfs(i, j, t, -1, bs); s = max(s, r); } if ((bs < 0 && s >= 0) || (s >= 0 && s < bs)) { bs = s; ba = A[i][j]; } } } if (bs > m*n+10) { cout << "impossible" << endl; } else { cout << ba << " " << bs << endl; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); run(); return 0; }