#include #define ld long double using namespace std; const ld INF = 1e18; int n, m, sx, sy, ex, ey; struct pt { int x, y; pt(int _x, int _y) { x = _x; y = _y; } }; inline ld dist(pt a, pt b) { return sqrtl((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } inline long long area (pt a, pt b, pt c) { return (b.x - a.x) * 1LL * (c.y - a.y) - (b.y - a.y) * 1LL * (c.x - a.x); } inline bool intersect_1 (int a, int b, int c, int d) { if (a > b) swap (a, b); if (c > d) swap (c, d); return max(a,c) <= min(b,d); } bool intersect (pt a, pt b, pt c, pt d) { return intersect_1 (a.x, b.x, c.x, d.x) && intersect_1 (a.y, b.y, c.y, d.y) && area(a,b,c) * area(a,b,d) <= 0 && area(c,d,a) * area(c,d,b) <= 0; } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> m >> sy >> sx >> ey >> ex; sx--; sy--; ey--; ex--; n /= 2; m /= 2; int a[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } pt p1 = pt(sx, sy); pt p2 = pt(ex, ey); vector < int > q; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (intersect(pt(2 * i, 2 * j), pt(2 * i + 1, 2 * j), p1, p2) || intersect(pt(2 * i, 2 * j), pt(2 * i, 2 * j + 1), p1, p2) || intersect(pt(2 * i + 1, 2 * j), pt(2 * i + 1, 2 * j + 1), p1, p2) || intersect(pt(2 * i, 2 * j + 1), pt(2 * i + 1, 2 * j + 1), p1, p2)) { q.push_back(a[i][j]); } } } ld res = 0; for (int i = 0; i + 1 < (int)q.size(); i++) { res += abs(q[i] - q[i + 1]); } cout << dist(p1, p2) + res; }