import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum INF = 10L^^18; // sqrt(499 * 100) enum L = 224; int H, W; int GX, GY; long[][] A; int Q; int[] X, Y; long[] K; void main() { try { for (; ; ) { H = readInt(); W = readInt(); GX = readInt() - 1; GY = readInt() - 1; A = new long[][](H, W); foreach (x; 0 .. H) foreach (y; 0 .. W) { A[x][y] = readLong(); } Q = readInt(); X = new int[Q]; Y = new int[Q]; K = new long[Q]; foreach (q; 0 .. Q) { X[q] = readInt() - 1; Y[q] = readInt() - 1; K[q] = readLong(); } // small auto dist = new long[][][](L, H, W); foreach (k; 0 .. L) { const k2 = 1L * k * k; alias Node = Tuple!(long, "c", int, "x", int, "y"); BinaryHeap!(Array!Node, "a > b") q; foreach (x; 0 .. H) foreach (y; 0 .. W) { dist[k][x][y] = INF; } dist[k][GX][GY] = k2 + A[GX][GY]; q.insert(Node(dist[k][GX][GY], GX, GY)); for (; !q.empty; ) { const c = q.front.c; const x = q.front.x; const y = q.front.y; q.removeFront; if (dist[k][x][y] == c) { void update(int xx, int yy) { const cc = c + k2 + A[xx][yy]; if (chmin(dist[k][xx][yy], cc)) { q.insert(Node(cc, xx, yy)); } } if (x + 1 < H) update(x + 1, y); if (y + 1 < W) update(x, y + 1); if (x - 1 >= 0) update(x - 1, y); if (y - 1 >= 0) update(x, y - 1); } } } // large auto dp = new long[][](H, W); foreach (x; 0 .. H) foreach (y; 0 .. W) { dp[x][y] = INF; } dp[GX][GY] = A[GX][GY]; foreach (x; GX .. H) foreach (y; GY .. W) { if (x > GX) chmin(dp[x][y], dp[x - 1][y] + A[x][y]); if (y > GY) chmin(dp[x][y], dp[x][y - 1] + A[x][y]); } foreach (x; GX .. H) foreach_reverse (y; 0 .. GY + 1) { if (x > GX) chmin(dp[x][y], dp[x - 1][y] + A[x][y]); if (y < GY) chmin(dp[x][y], dp[x][y + 1] + A[x][y]); } foreach_reverse (x; 0 .. GX + 1) foreach (y; GY .. W) { if (x < GX) chmin(dp[x][y], dp[x + 1][y] + A[x][y]); if (y > GY) chmin(dp[x][y], dp[x][y - 1] + A[x][y]); } foreach_reverse (x; 0 .. GX + 1) foreach_reverse (y; 0 .. GY + 1) { if (x < GX) chmin(dp[x][y], dp[x + 1][y] + A[x][y]); if (y < GY) chmin(dp[x][y], dp[x][y + 1] + A[x][y]); } foreach (q; 0 .. Q) { long ans; if (K[q] < L) { ans = dist[cast(int)(K[q])][X[q]][Y[q]]; } else { ans = 1L * K[q] * K[q] * (1 + abs(X[q] - GX) + abs(Y[q] - GY)) + dp[X[q]][Y[q]]; } writeln(ans); } } } catch (EOFException e) { } }