#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<long long,pair<int,int>>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
const int dx[] = { 1,0,-1,0 };
const int dy[] = { 0,-1,0,1 };
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int h, w; cin >> h >> w;
	int gx, gy; cin >> gx >> gy;
	gx--, gy--;
	vector a(h, vector<int>(w));
	rep(i, h)rep(j, w)cin >> a[i][j];
	vector dp(250, vector(h, vector<long long>(w, LINF)));
	rep(i, 250) {
		long long k = i;
		dp[i][gx][gy] = k * k + a[gx][gy];
		priority_queue<P, vector<P>, greater<P>> q;
		q.push({ dp[i][gx][gy], {gx, gy} });
		while (!q.empty()) {
			auto tmp = q.top();
			q.pop();
			long long cost = tmp.first;
			int x = tmp.second.first;
			int y = tmp.second.second;
			if (cost > dp[i][x][y]) continue;
			rep(j, 4) {
				int nx = x + dx[j];
				int ny = y + dy[j];
				if ((nx < 0) || (nx >= h) || (ny < 0) || (ny >= w))continue;
				long long ncost = cost + k * k + a[nx][ny];
				if (chmin(dp[i][nx][ny], ncost))q.push({ ncost, { nx, ny } });
			}
		}
	}
	vector dp2(h, vector<long long>(w, LINF));
	{
		set<pair<int, int>>st;
		dp2[gx][gy] = a[gx][gy];
		st.insert({ gx,gy });
		while (!st.empty()) {
			set<pair<int, int>>st2;
			for (auto e : st) {
				int x = e.first;
				int y = e.second;
				rep(j, 4) {
					int nx = x + dx[j];
					int ny = y + dy[j];
					if ((nx < 0) || (nx >= h) || (ny < 0) || (ny >= w))continue;
					if (LINF == dp2[nx][ny])st2.insert({ nx, ny });
					if (!st2.count({ nx,ny })) continue;
					long long ncost = dp2[x][y] + a[nx][ny];
					chmin(dp2[nx][ny],ncost);
				}
			}
			swap(st2, st);
		}
	}
	int q; cin >> q;
	while (q--) {
		int x, y; cin >> x >> y;
		x--, y--;
		long long k; cin >> k;
		if (k > 250) {
			long long ans = dp2[x][y];
			long long d = abs(gx - x) + abs(gy - y) + 1;
			ans += k * k * d;
			cout << ans << endl;
		}
		else {
			cout << dp[k][x][y] << endl;
		}
	}
	return 0;
}