#include using namespace std; #define ALL(x) (x).begin(), (x).end() template void mini(C &a5, C b5) { a5 = min(a5, b5); } template void maxi(C &a5, C b5) { a5 = max(a5, b5); } #ifdef LOCAL const bool debug = true; #else const bool debug = false; #define cerr if (true) {} else cout #endif #define int long long #define double long double const int INF = 1e18; const int mod = 1e9 + 7; const int nax = 100010; #define kwa(a) ((a) * (a)) int ilo(const pair &a, pair b, pair c) { b.first -= a.first; b.second -= a.second; c.first -= a.first; c.second -= a.second; return b.first * c.second - c.first * b.second; } vector height[nax]; vector czy[nax]; pair s, e; bool is_int(int x, int y) { bool a = s.first <= x && x <= e.first; bool b = e.first <= x && x <= s.first; bool c = s.second <= y && y <= e.second; bool d = e.second <= y && y <= s.second; return (a || b) && (c || d); } bool prz_lin(pair a, pair b) { int x = ilo(s, e, a); int y = ilo(s, e, b); return (x > 0 && y < 0) || (x < 0 && y > 0); } bool prz_pro(int x, int y) { if (!is_int(x, y)) return false; return prz_lin({2 * x, 2 * y}, {2 * x - 2, 2 * y - 2}) || prz_lin({2 * x, 2 * y - 2}, {2 * x - 2, 2 * y}); } int foo(int x, int y) { if (!czy[x][y]) return 0; int res = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) continue; int x2 = x + i; int y2 = y + j; if (i * j == 0) { if (czy[x2][y2]) { res += abs(height[x][y] - height[x2][y2]); } } } } return res; } int rog_na_wysokosc(int x, int y) { x /= 2; y /= 2; vector v { height[x][y], height[x + 1][y], height[x][y + 1], height[x + 1][y + 1] }; sort(ALL(v)); int res = 0; for (int i = 0; i <= 1; i++) { for (int j = 0; j <= 1; j++) { if (czy[i + x][j + y]) res += abs(v[2] - height[i + x][j + y]); } } return res; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int w, h; cin >> w >> h >> s.first >> s.second >> e.first >> e.second; for (int i = 0; i <= h + 5; i++) { height[i].resize(w + 10); czy[i].resize(w + 10); } for (int i = 1; i <= h/2; i++) { for (int j = 1; j <= w/2; j++) { cin >> height[i][j]; czy[i][j] = prz_pro(i, j); } } czy[s.first][s.second] = true; czy[e.first][e.second] = true; if (debug) { for (int i = 1; i <= h/2; i++) { for (int j = 1; j <= w/2; j++) { cerr << czy[i][j]; } cerr << endl; } } double res = sqrt(kwa(s.first - e.first) + kwa(s.second - e.second)); int ad = 0; for (int i = 1; i <= h/2; i++) { for (int j = 1; j <= w/2; j++) { ad += foo(i, j); } } for (int i = 0; i <= h; i+=2) { for (int j = 0; j <= w; j+=2) { if (is_int(i, j) && ilo(s, e, {i, j}) == 0) { ad += rog_na_wysokosc(i, j); cerr << i << " " << j << " " << ad << endl; } } } cerr << ad << endl; cout << fixed << setprecision(10) << res + ad << "\n"; return 0; }/* 8 8 1 7 7 1 2 3 2 0 2 1 1 2 1 2 0 0 0 0 0 1 */