#include #define ll long long using namespace std; ll w, h; ll sx, sy, ex, ey; vector> heights; vector> inter; ll sgn(ll x) { if (x < 0) return -1; else if (!x) return 0; return 1; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> w >> h; cin >> sy >> sx >> ey >> ex; heights = vector>(h / 2, vector(w / 2)); for (int i = 0; i < h / 2; i++) { for (int j = 0; j < w / 2; j++) { cin >> heights[i][j]; } } ll dx = ex - sx; ll dy = ey - sy; if (dx && dy) { for (ll i = sy + sgn(dy); i != ey + sgn(dy); i += 2 * sgn(dy)) inter.push_back({(i - sy) * dx * sgn(dx) * sgn(dy), 0}); for (ll i = sx + sgn(dx); i != ex + sgn(dx); i += 2 * sgn(dx)) inter.push_back({(i - sx) * dy * sgn(dx) * sgn(dy), 1}); } else if (dy) { for (ll i = sy + sgn(dy); i != ey + sgn(dy); i += 2 * sgn(dy)) inter.push_back({(i - sy) * sgn(dy), 0}); } else { for (ll i = sx + sgn(dx); i != ex + sgn(dx); i += 2 * sgn(dx)) inter.push_back({(i - sx) * sgn(dx), 1}); } sort(inter.begin(), inter.end()); ll x = sx / 2, y = sy / 2; ll h = heights[x][y]; ll ans = 0; for (int i = 0; i < inter.size(); i++) { // cout << inter[i].first << " " << inter[i].second << endl; ll newh; if (i < inter.size() - 1 && inter[i].first == inter[i + 1].first) { x += sgn(dx); y += sgn(dy); newh = heights[x][y]; ll ma = max(h, newh); ma = max(ma, heights[x - sgn(dx)][y]); ma = max(ma, heights[x][y - sgn(dy)]); ans += abs(ma - h); ans += abs(ma - newh); i++; } else if (inter[i].second) { x += sgn(dx); newh = heights[x][y]; ans += abs(h - newh); } else { y += sgn(dy); newh = heights[x][y]; ans += abs(h - newh); } // cout << h << " " << newh << endl; h = newh; } // cout << ans << endl; cout << fixed << setprecision(10) << ans + sqrt(dx * dx + dy * dy) << endl; return 0; }