#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<b?(a=b,1):0)
#define chmin(a,b) (a>b?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int p[10][10];
int id[10][10];
int side[100];
int size[100];
int share[100][100];
bool visited[10][10];


int main() {
  int w,h;
  while(cin>>w>>h,w||h) {
    int fx,fy,fq;
    cin>>fx>>fy>>fq;
    REP(i,h) REP(j,w) cin >> p[i][j];
    REP(i,100) side[i]=size[i]=0;
    memset(id,-1,sizeof(id));
    memset(share,0,sizeof(share));
    memset(visited,0,sizeof(visited));
    int num = 0;
    const int dy[4] = {-1,0,1,0};
    const int dx[4] = {0,1,0,-1};
    vector<pair<int, pii> > v;
    REP(i,h) REP(j,w) if (p[i][j]) v.push_back(make_pair(p[i][j], pii(i,j)));
    sort(ALL(v));
    FOR(it, v) {
      pii a = it->second;
      int i = a.first, j = a.second;
      if (id[i][j] == -1) {
        queue<pii> Q;
        Q.push(pii(i,j));
        while(!Q.empty()) {
          pii now = Q.front(); Q.pop();
          id[now.first][now.second] = num;
          size[num]++;
          REP(k,4) {
            int y = now.first + dy[k];
            int x = now.second + dx[k];
            if (valid(y,x,h,w)) {
              if (id[y][x] == -1 && p[y][x] == p[i][j] && !visited[y][x]) {
                visited[y][x] = 1;
                Q.push(pii(y,x));
              } else if (p[y][x] < p[i][j]) {
                if (p[y][x] > 0) share[num][id[y][x]]++;
                side[num]++;
              }
            } else {
              side[num]++;
            }
          }
        }
        num++;
      }       
    }
    // REP(i,h) {
    //   REP(j,w) {
    //     cout << id[i][j] << " ";
    //   }
    //   cout << endl;
    // }
    // REP(i,num) {
    //   REP(j,num) {
    //     cout << share[i][j] << " " ;
    //   }
    //   cout << endl;
    // }
    // REP(i,num) cout << side[i] << " "; cout << endl;
    int L;
    cin >> L;
    REP(i,L) {
      int t, x, y;
      cin >> t >> x >> y;
      double tmp[100] = {};
      tmp[id[fy][fx]] = t*fq;
      for (int idx=num-1; idx>=0; --idx) {
        if (side[idx] == 0) continue;
        double flow = max(0.0, tmp[idx]-30*30*30*size[idx]);
        flow /= side[idx];
        REP(j,num) {
          tmp[j] += flow * share[idx][j];
        }
      }
      double hei = min((double)30, tmp[id[y][x]] / (30*30*size[id[y][x]]));
      cout << int(hei + EPS) << endl;
    }
  }
}