#include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair P; #define fi first #define se second #define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++) #define rep(i,n) repl(i,0,n) #define all(x) (x).begin(),(x).end() #define dbg(x) cout<<#x"="<y?x:y) #define mmin(x,y) (x b.d; } struct state2{ ll i,j; P p; }; bool operator<(const state2& a,const state2& b){ return a.p > b.p; } ll H,W; ll gi,gj; ll A[250][250]; ll Q; ll dist[300][250][250]; P dist2[250][250]; ll di[]={-1,0,+1,0}; ll dj[]={0,-1,0,+1}; const ll Kmax=280; int main(){ cin.tie(0); ios::sync_with_stdio(false); cin>>H>>W>>gi>>gj; gi--;gj--; rep(i,H)rep(j,W){ cin>>A[i][j]; } repl(k,1,Kmax){ rep(i,H)rep(j,W)dist[k][i][j]=INF; priority_queue que; que.push((state){gi,gj,k*k+A[gi][gj]}); while(que.size()){ state s=que.top(); que.pop(); if(dist[k][s.i][s.j]!=INF)continue; dist[k][s.i][s.j]=s.d; rep(dir,4){ ll ni=s.i+di[dir],nj=s.j+dj[dir]; if(ni<0||ni>=H||nj<0||nj>=W)continue; que.push((state){ni,nj,s.d+k*k+A[ni][nj]}); } } } { rep(i,H)rep(j,W)dist2[i][j]=P(INF,INF); priority_queue que2; que2.push((state2){gi,gj,P(1,A[gi][gj])}); while(que2.size()){ state2 s=que2.top(); que2.pop(); if(dist2[s.i][s.j]!=P(INF,INF))continue; dist2[s.i][s.j]=s.p; rep(dir,4){ ll ni=s.i+di[dir],nj=s.j+dj[dir]; if(ni<0||ni>=H||nj<0||nj>=W)continue; que2.push((state2){ni,nj,P(s.p.fi+1,s.p.se+A[ni][nj])}); } } } cin>>Q; rep(_,Q){ ll x,y,k; cin>>x>>y>>k; x--;y--; if(k