#include #define loop(n) for (int ngtkana_is_geneous = 0; ngtkana_is_geneous < n; ngtkana_is_geneous++) #define rep(i, begin, end) for(int i = begin; i < end; i++) #define LOCAL using std::to_string; auto to_string(std::string s) -> std::string { return '"' + s + '"'; } auto to_string(char c) -> std::string { return "'" + std::string{c} + "'"; } auto to_string(const char* s) -> std::string { return to_string((std::string) s); } auto to_string(bool b) -> std::string { return (b ? "true" : "false"); } template auto to_string(std::pair p) -> std::string { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template auto to_string(std::bitset bs) -> std::string { std::string res{}; for (size_t i = 0; i < N; i++) res.insert(res.begin(), bs.test(i) ? '1' : '0'); return res; } template auto to_string(T v) -> std::string { bool flg = false; std::string res = "{"; for (auto const&x : v) { if (flg) res += ", "; else flg = true; res += to_string(x); } res += "}"; return res; } void debug_out() { std::cerr << std::endl; } template void debug_out(Head H, Tail... T) { std::cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif template auto make_vector(size_t sz, T t) { return std::vector(sz, t); } template = nullptr> auto make_higher_vector(size_t sz, U u) { return make_vector(sz, T(u)); } template = nullptr> auto make_higher_vector(size_t sz) { return std::vector(sz); } template = nullptr> auto make_higher_vector(size_t a, Args... args) { return make_vector(a, make_higher_vector(args...)); } template auto& at(T& t, Size_t i) { return t.at(i); } template auto& at(T& t, Size_t i, Args... args) { return at(t.at(i), args...); } template inline auto cmn (T& a, U b) {if (a > b) {a = b; return true;} return false;} template inline auto cmx (T& a, U b) {if (a < b) {a = b; return true;} return false;} int main() { std::cin.tie(0); std::cin.sync_with_stdio(false); int h, w; std::cin >> h >> w; int gx, gy; std::cin >> gx >> gy; gx--, gy--; auto a = make_higher_vector<2, int>(h, w); for (auto & b : a) for (auto & c : b) std::cin >> c; int max = h * w; auto dp = make_higher_vector<2, std::vector>>(h, w); int di[4] = {-1, +1, 0, 0}; int dj[4] = {0, 0, -1, +1}; std::queue> que; que.emplace(gx, gy, 1, at(a, gx, gy)); while (!que.empty()) { int i, j, k, val; std::tie(i, j, k, val) = que.front(); que.pop(); if (k > max) break; auto& crr = at(dp, i, j); if (!crr.empty() && (crr.back().second <= val)) continue; if (!crr.empty() && crr.back().first == k) crr.pop_back(); crr.emplace_back(k, val); for (int l = 0; l < 4; l++) { int nxi = i + di[l]; int nxj = j + dj[l]; if (nxi < 0 || h <= nxi || nxj < 0 || w <= nxj) continue; que.emplace(nxi, nxj, k + 1, val + at(a, nxi, nxj)); } } constexpr int inf = 1 << 30; int q; std::cin >> q; while (q--) { int x, y; long long level; std::cin >> x >> y >> level; x--, y--; long long ret = inf; for (auto pair : at(dp, x, y)) { long long k, val; std::tie(k, val) = pair; cmn(ret, k * level * level + val); } std::cout << ret << std::endl; } return 0; }