//#pragma GCC optimize("O3") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include using namespace std; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // namespace debug { template struct rge { c b, e; }; template rge range(c i, c j) { return rge{i, j}; } template char elo(...); template auto elo(c* a) -> decltype(cerr << *a, 0); struct stream { ~stream() { cerr << endl; } template typename enable_if (0) != 1, stream&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template typename enable_if (0) == 1, stream&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template stream& operator<<(pair p) { return *this << "(" << p.first << ", " << p.second << ")"; } template stream& operator<<(rge d) { *this << "["; for (auto it=d.b; it!=d.e; it++) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } stream& _dbg(const string& s, int i, int b) { return *this; } template stream& _dbg(const string& s, int i, int b, c arg, cs ... args) { if (i == s.size()) return (*this << ": " << arg << ""); b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{'); b -= (s[i] == ')') + (s[i] == ']') + (s[i] == '}'); if (s[i] == ',' && b == 0) return (*this << ": " << arg << " ")._dbg(s, i+1, b, args...); return (s[i] == ' ' ? *this : *this << s[i])._dbg(s, i+1, b, arg, args...); } };} #define dout debug::stream() #define dbg(...) ((dout << ">> ")._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__)) // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // #define f first #define s second #define sc scanf #define pr printf #define mp make_pair #define pb push_back #define all(x) x.begin(),x.end() #define ss(x) ((int)((x).size())) #define rep0(i, b) for(int (i)=(0);i<(b);(i)++) #define rep1(i, b) for(int (i)=(1);i<=(b);(i)++) #define rep(i, a, b) for(int (i)=(a);i<=(b);(i)++) #define per(i, a, b) for(int (i)=(b);i>=(a);(i)--) #define lowb(v, x) (lower_bound(all(v),(x))-(v).begin()) #define uppb(v, x) (upper_bound(all(v),(x))-(v).begin()) #define upgrade ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define erase_duplicates(x) {sort(all(x));(x).resize(distance((x).begin(), unique(all(x))));} template pair operator-(pair a, pair b) { return mp(a.f-b.f, a.s-b.s); } template pair operator+(pair a, pair b) { return mp(a.f+b.f, a.s+b.s); } template void umin(p& a, const q& b) { if (a > b) a = b; } template void umax(p& a, const q& b) { if (a < b) a = b; } using lf=long double; using ll=long long; using cll=const ll; using cint=const int; using pll=pair; using pii=pair; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // using point=pair; #define y first #define x second const int N = 100005; lf outh, out; int mx, my, sx, sy, tx, ty; vector < vector > t; point S, T; void rotate (){ assert(my == t.size()); assert(mx == t[0].size()); vector < vector > res (mx, vector (my)); rep(y, 0, my - 1){ rep(x, 0, mx - 1){ res[x][my - y - 1] = t[y][x]; } } swap(my, mx); swap(sy, sx); sx = mx - sx - 1; swap(ty, tx); tx = mx - tx - 1; t = res; } void print (vector < vector > vec){ printf ("--------\n"); for (auto v : vec){ for (auto x : v){ printf("%d ", x); } printf ("\n"); } printf ("--------\n"); } pair central (int y, int x){ (y *= 2) += 1; (x *= 2) += 1; return {y, x}; } ll IW (point a, point b, point c){ a = a - c; b = b - c; return 1ll * a.x * b.y - 1ll * a.y * b.x; } int main (){ scanf ("%d%d", &mx, &my); scanf ("%d%d", &sx, &sy); scanf ("%d%d", &tx, &ty); if (sy == ty && sx == tx){ return printf ("0.0\n"), 0; } my /= 2; mx /= 2; outh = sqrtl((sx - tx) * (sx - tx) + (sy - ty) * (sy - ty)); sx /= 2; sy /= 2; tx /= 2; ty /= 2; t = vector < vector > (my, vector (mx)); rep(y, 0, my - 1){ rep(x, 0, mx - 1){ scanf ("%d", &t[y][x]); } } while (sy >= ty || sx > tx){ rotate(); // print(t); // dbg(my, mx); // dbg(sy, sx); // dbg(ty, tx); } S = central(sy, sx); T = central(ty, tx); int y = sy; int x = sx; //dbg(S, T); while (true){ //dbg(y, x); assert(y < my); assert(x < mx); if (y == ty && x == tx) break; auto A = central(y, x) + point(1, 1); ll iw = IW(A, T, S); if (iw == 0){ int h1 = t[y][x]; int h2 = t[y + 1][x + 1]; int h3 = t[y + 1][x]; int h4 = t[y][x + 1]; int hm = min(h3, h4); //dbg(x, y, h1, h2, hm); if (hm > h1 && hm > h2){ out += abs(h1 - hm); out += abs(h2 - hm); }else{ out += abs(h1 - h2); } //dbg(out); y += 1; x += 1; } if (iw > 0){ int h1 = t[y][x]; int h2 = t[y + 1][x]; out += abs(h1 - h2); y += 1; } if (iw < 0){ int h1 = t[y][x]; int h2 = t[y][x + 1]; out += abs(h1 - h2); x += 1; } } printf ("%.13Lf\n", outh + out); return 0; }