#include "bits/stdc++.h" // Tomasz Nowak using namespace std; // Poland using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) template int ssize(T &&x) { return int(x.size()); } template ostream& operator<<(ostream &out, const pair &p) { return out << '(' << p.first << ", " << p.second << ')'; } template auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif mt19937_64 rng(0); int rd(int l, int r) { return uniform_int_distribution(l, r)(rng); } // end of templates using P = pair; P operator-(P a, P b) { return {a.first - b.first, a.second - b.second}; } LL operator*(P a, P b) { return a.first * LL(b.second) - a.second * LL(b.first); } LL dir(P a, P b, P c) { LL dot = (b - a) * (c - b); return dot < 0 ? -1 : dot > 0 ? 1 : 0; } bool is_on_line(P a, P b, P c) { return dir(a, b, c) == 0; } bool is_strictly_in_segment(P a, P b, P l, P r) { return dir(a, b, l) * dir(a, b, r) == -1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int w, h, sx, sy, ex, ey; cin >> w >> h >> sx >> sy >> ex >> ey; if(sx == ex and sy == ey) { cout << "0\n"; return 0; } assert(sx % 2 == 1 and sy % 2 == 1 and ex % 2 == 1 and ey % 2 == 1); if(make_pair(sx, sy) > make_pair(ex, ey)) { swap(sx, ex); swap(sy, ey); } auto kw = [&](int x) { return x * LL(x); }; long double horiz_len = sqrtl(kw(sx - ex) + kw(sy - ey)); debug(horiz_len); w /= 2; h /= 2; vector> height(h, vector(w)); REP(y, h) REP(x, w) cin >> height[x][y]; debug(height); assert(sx <= ex); if(sy > ey) { sy = 2 * h - sy; ey = 2 * h - ey; vector> new_h = height; REP(y, h) REP(x, w) new_h[x][y] = height[x][h - 1 - y]; height = new_h; debug("mirrored by y", sx, sy, ex, ey, height); } assert(sy <= ey); P s(sx, sy); sx /= 2; sy /= 2; P e(ex, ey); ex /= 2; ey /= 2; int curr_h = height[sx][sy]; LL sum_vertical = 0; auto move_to_h = [&](int hi) { sum_vertical += abs(hi - curr_h); curr_h = hi; }; REP(x, w) REP(y, h) if(sx <= x and x <= ex and sy <= y and y <= ey) { debug(x, y); P edgepoint = P(2 * x + 2, 2 * y + 2); if(x != ex and y != ey and is_on_line(s, e, edgepoint)) { debug(x, y, "edgepoint"); vector sorted; for(int dx : {0, 1}) for(int dy : {0, 1}) sorted.emplace_back(height[x + dx][y + dy]); sort(sorted.rbegin(), sorted.rend()); move_to_h(sorted[1]); move_to_h(height[x + 1][y + 1]); } else if(y != ey and is_strictly_in_segment(s, e, P(2 * x, 2 * y + 2), edgepoint)) { debug(x, y, "up"); move_to_h(height[x][y + 1]); } else if(x != ex and is_strictly_in_segment(s, e, P(2 * x + 2, 2 * y), edgepoint)) { debug(x, y, "right"); move_to_h(height[x + 1][y]); } } assert(curr_h == height[ex][ey]); debug(sum_vertical); cout << fixed << setprecision(10); cout << horiz_len + sum_vertical << '\n'; }