#include "bits/stdc++.h" #include #include #include "atcoder/all" #include //#include //using namespace boost::multiprecision; using namespace std; using namespace atcoder; using ll = long long; using ull = unsigned long long; using mint = modint998244353; using mint2 = modint1000000007; const double eps = 1e-9; #define REP(i, n) for (ll i = 0; i < ll(n); ++i) #define REPI(i, init, n) for (ll i = init; i < ll(n); ++i) #define REPD(i, init) for (ll i = init; i >=0; --i) #define REPDI(i, init, n) for (ll i = init; i >=n; --i) #define REPE(i, c) for (const auto& i : c) #define REPM(i, c) for (auto&& i : c) #define TCASE() ll _ttt; cin >> _ttt; while(_ttt--) using vl = vector; using vvl = vector; using vvvl = vector; #define VLI(a, n) vector a; a.assign(n, 0); for(auto& x : a) cin >> x; #define DLI(a, n) deque a; a.assign(n, 0); for(auto& x : a) cin >> x; #define VSI(a, n) vector a; a.assign(n, ""); for(auto& x : a) cin >> x; #define VVLI(a, n, m, init) vector> a(n); for(auto& x : a) x.assign(m, init); using vd = vector; using pl = pair; using vpl = vector; struct uv { ll u; ll v; ll c; }; #define VUVI(a, n) vector a; a.assign(n, {0, 0, 1}); for(auto& x : a) {cin >> x.u >> x.v; x.u--; x.v--;} #define VUVCI(a, n) vector a; a.assign(n, {0, 0, 0}); for(auto& x : a) {cin >> x.u >> x.v >> x.c; x.u--; x.v--;} vvl to_edge(const ll n, const vector& v) { vvl ret(n); for (auto& x : v) ret[x.u].push_back(x.v); return ret; } vvl to_edge_d(const ll n, const vector& v) { vvl ret(n); for (auto& x : v) { ret[x.u].push_back(x.v); ret[x.v].push_back(x.u); } return ret; } template using pql = priority_queue; template using pqg = priority_queue, greater>; using vm = vector; using vvm = vector; ostream& operator<<(ostream& os, const mint& v) { os << v.val(); return os; } void yn(bool f) { std::cout << (f ? "Yes" : "No") << endl; }; template void ov(const T& v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) cout << " "; cout << *it; } }; template<> void ov(const vm& v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) cout << " "; cout << it->val(); } }; const ll llhuge = 1LL << 62; //十分でかいが多少足しても溢れない数値 auto gmax(const auto& a, const auto& b) { return a > b ? a : b; }; auto gmin(const auto& a, const auto& b) { return a < b ? a : b; }; bool chmin(auto& a, const auto& b) { const auto olda = a; a = gmin(a, b); return olda != a; } bool chmax(auto& a, const auto& b) { const auto olda = a; a = gmax(a, b); return olda != a; } void sort(auto& v) { std::ranges::sort(v); }; void rev(auto& v) { std::ranges::reverse(v); }; template auto p_sum(const T& v) { T ret(v.size()); std::partial_sum(begin(v), end(v), ret.begin()); return ret; }; template [[nodiscard]] V myat(const T& mp, const K& key) { return mp.find(key) == mp.end() ? V() : mp.at(key); } long long ceil(const long long a, const long long b) { return (a + b - 1) / b; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll h, w; cin >> h >> w; vvl a(h, vl(w)); REPM(x, a) REPM(y, x) cin >> y; pqg> pq1; pqg> pq2; pq1.push({ 0, 0, 0 }); pq2.push({ 0, h-1, w-1 }); ll ans = -2; bool flg = true; while (flg) { { ++ans; while (a[get<1>(pq1.top())][get<2>(pq1.top())] == -1) pq1.pop(); auto [c1, x1, y1] = pq1.top(); pq1.pop(); a[x1][y1] = -1; REP(i, 4) { ll nx = (i == 0) ? x1 - 1 : (i == 1) ? x1 + 1 : x1; ll ny = (i == 2) ? y1 - 1 : (i == 3) ? y1 + 1 : y1; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (a[nx][ny] == -1) continue; else if (a[nx][ny] == -2) flg = false; else { pq1.push({ a[nx][ny], nx, ny }); } } } if (!flg) break; { ++ans; while (a[get<1>(pq2.top())][get<2>(pq2.top())] == -2) pq2.pop(); auto [c1, x1, y1] = pq2.top(); a[x1][y1] = -2; pq2.pop(); REP(i, 4) { ll nx = (i == 0) ? x1 - 1 : (i == 1) ? x1 + 1 : x1; ll ny = (i == 2) ? y1 - 1 : (i == 3) ? y1 + 1 : y1; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (a[nx][ny] == -2) continue; else if (a[nx][ny] == -1) flg = false; else { pq2.push({ a[nx][ny], nx, ny }); } } if (!flg) break; } } cout << ans << endl; }