/*
05:46 - 06:33 テゥツ」ツッテ、ツシツ妥ヲツ?ゥ
06:54 - テ・ツッツ敕ィツ青ステ」ツ?。
 */
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<deque>
#include<cassert>
#include<climits>
#include<set>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define IINF (INT_MAX)
#define MAX 20

using namespace std;

typedef pair<int,int> ii;

struct Data{
  int cur;
  double remain;
  Data(int cur=IINF,double remain=IINF):cur(cur),remain(remain){}
};

int w,h,fx,fy,l,px,py,V;
double fq,t;
double p[MAX][MAX],divide[MAX*MAX],tank[MAX*MAX],cap[MAX*MAX],area[MAX*MAX],coef[MAX*MAX][MAX*MAX];
vector<int> G[MAX*MAX];
int Index[MAX][MAX];
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};

inline bool isValid(int x,int y){ return ( 0 <= x && x < w && 0 <= y && y < h ); }

void dfs(int x,int y,int index){
  if(Index[y][x] != -1)return;
  Index[y][x] = index;
  area[index]++;
  rep(i,4){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(!isValid(nx,ny))continue;
    if(p[y][x] != p[ny][nx])continue;
    dfs(nx,ny,index);
  }
}

void INIT(){
  rep(i,h)rep(j,w)G[j+i*w].clear(),Index[i][j] = -1;
  rep(i,h*w)divide[i] = cap[i] = -1, area[i] = 0;
  rep(i,h*w)rep(j,h*w)coef[i][j] = 0;
  int dex = 0;
  rep(y,h)rep(x,w){
    if(Index[y][x] != -1)continue;
    if(p[y][x] == 0)continue;
    dfs(x,y,dex);
    cap[dex] = 30.0 * 30.0 * 30.0 * area[dex];
    dex++;
  }
  V = dex;
  set<int> edge[dex];
  set<ii> cost[dex];
  set<ii> COEF[V][V];
  int tmp=IINF;
  rep(y,h){
    rep(x,w){
      if(p[y][x] == 0)continue;
      rep(k,4){
	int nx = x + dx[k];
	int ny = y + dy[k];
	if(!isValid(nx,ny)){
	  cost[Index[y][x]].insert(ii(tmp,tmp));
	  tmp--;
	  continue;
	}
	if(p[ny][nx] == 0){
	  cost[Index[y][x]].insert(ii(tmp,tmp));
	  tmp--;
	  continue;
	}
	if(Index[ny][nx] == Index[y][x])continue;
	if(p[y][x] <= p[ny][nx])continue;
	COEF[Index[y][x]][Index[ny][nx]].insert(ii(tmp,tmp));
	tmp--;
	cost[Index[y][x]].insert(ii(tmp,tmp));
	tmp--;
	edge[Index[y][x]].insert(Index[ny][nx]);
      }
    }
  }

  rep(from,V){
    for(int to : edge[from]){
      G[from].push_back(to);
    }
    rep(i,G[from].size()){
      assert(!COEF[from][G[from][i]].empty());
      coef[from][G[from][i]] = (double)COEF[from][G[from][i]].size();
    }
    divide[from] = (double)cost[from].size();
  }
}

int main(){
  while(cin >> w >> h,w|h){
    cin >> fx >> fy >> fq;
    rep(i,h)rep(j,w)cin >> p[i][j];

    INIT();

    cin >> l;
    rep(_,l){
      cin >> t >> px >> py;
      if(p[py][px] == 0){
	cout << 0 << endl;
	continue;
      }
      assert(p[py][px] != 0);
      deque<Data> deq;
      deq.push_back(Data(Index[fy][fx],fq*t));
      double store[V];
      rep(i,V)store[i] = tank[i] = 0;
      while(true){
	if(deq.empty()){
	  bool update = false;
	  rep(i,V){
	    if(store[i] == 0)continue;
	    if(store[i]+tank[i] <= cap[i]){
	      tank[i] += store[i];
	    } else {
	      if(G[i].empty()){
		tank[i] = cap[i];
		continue;
	      }
	      update = true;
	      double cost = (store[i]+tank[i]-cap[i])/divide[i];
	      tank[i] = cap[i];
	      rep(j,G[i].size()){
		int next = G[i][j];
		assert(coef[i][next] != 0);
		deq.push_back(Data(next,cost*coef[i][next]));
	      }
	    }
	  }
	  if(!update)break;
	  rep(i,V)store[i] = 0; 
	}

	Data data = deq.front(); deq.pop_front();

	double remain = data.remain;
	double limit = cap[data.cur] - tank[data.cur];
	store[data.cur] += remain;
      }
      cout << floor(tank[Index[py][px]]/(30.0*30.0)/area[Index[py][px]]) << endl;
    }
  }
  return 0;
}

/*
Test Case :
5 5
1 1 3000
2 2 2 2 2
2 9 9 9 2
2 9 4 9 2
2 9 9 9 2
2 2 2 2 2
5
100 0 0
100 1 1
100 2 2
100 3 3
100 4 4
0 0
Ans : 
4
30
23
30
4
 */

Measured memory_bytes: 2160