import java.util.*; class Main { static double distance; static int[][] heights; static int currentHeight; public static void main(String[] args) { Scanner in = new Scanner(System.in); int w = in.nextInt(); int h = in.nextInt(); int sx = in.nextInt(); int sy = in.nextInt(); int ex = in.nextInt(); int ey = in.nextInt(); heights = new int[w/2][h/2]; for (int y = 0; y < h/2; y++) { for (int x = 0; x < w/2; x++) { int input = in.nextInt(); heights[x][y] = input; } } currentHeight = heights[sx/2][sy/2]; distance = Math.sqrt((ey - sy) * (ey - sy) + (ex - sx) * (ex - sx)); raytrace(sx/2, sy/2, ex/2, ey/2); System.out.println(distance); } static void visit(int x, int y) { distance += Math.abs(heights[x][y] - currentHeight); currentHeight = heights[x][y]; } static void point(int x1, int y1, int x2, int y2) { int height = Math.min(heights[x1][y1], heights[x2][y2]); if (currentHeight < height) { distance += Math.abs(height - currentHeight); currentHeight = height; } } //source: http://playtechs.blogspot.com/2007/03/raytracing-on-grid.html, edited static void raytrace(int x0, int y0, int x1, int y1) { int dx = Math.abs(x1 - x0); int dy = Math.abs(y1 - y0); int x = x0; int y = y0; int n = 1 + dx + dy; int x_inc = (x1 > x0) ? 1 : -1; int y_inc = (y1 > y0) ? 1 : -1; int error = dx - dy; dx *= 2; dy *= 2; for (; n > 0; --n) { visit(x, y); if (error == 0) { n--; if (n == 0) break; point(x + x_inc, y, x, y + y_inc); x += x_inc; y += y_inc; error -= dy; error += dx; continue; } if (error > 0) { x += x_inc; error -= dy; } else { y += y_inc; error += dx; } } } }