#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int w, h;
map<int, double> g[1000];
double weight[1000];
double water[1000];
double zs[30][30];
int mapto[30][30];
vector<vector<int> > connect;

int fx, fy;
double fq;
bool visit[30][30];
vector<pair<int, int> > order;

int l;
double ans[100];
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };


double calc(int t, int target) {
  MEMSET(water, 0);
  water[mapto[fy][fx]] = t * fq;
  FORIT(it, order) {
    int index = it->second;
    double rest = max(0.0, water[index] - 30.0 * 30.0 * 30.0 * connect[index].size());
    FORIT(it, g[index]) {
      water[it->first] += rest * it->second;
    }
  }
  return min(30.1, water[target] / (30.0 * 30.0 * connect[target].size()) + EPS);
}


int main() {
  while (scanf("%d %d", &w, &h), w|h) {
    REP(i, 1000) { g[i].clear(); }
    MEMSET(weight, 0);
    MEMSET(water, 0);
    MEMSET(zs, 0);
    MEMSET(mapto, 0);
    connect.clear();
    MEMSET(visit, false);
    order.clear();
    {
      scanf("%d %d %lf", &fx, &fy, &fq);
      REP(y, h) {
        REP(x, w) {
          scanf("%lf", &zs[y][x]);
        }
      }

      // create connect group
      connect.push_back(vector<int>());
      REP(sy, h) {
        REP(sx, w) {
          if (visit[sy][sx]) { continue; }
          if (zs[sy][sx] == 0) {
            mapto[sy][sx] = 0;
            connect[0].push_back(sy * w + sx);
            continue;
          }
          connect.push_back(vector<int>());
          queue<int> que;
          que.push(sy * w + sx);
          int index = connect.size() - 1;
          order.push_back(make_pair(zs[sy][sx], index));
          while (!que.empty()) {
            int x = que.front() % w;
            int y = que.front() / w;
            que.pop();
            if (visit[y][x]) { continue; }
            visit[y][x] = true;
            connect.back().push_back(y * w + x);
            mapto[y][x] = index;
            REP(dir, 4) {
              int nx = x + dx[dir];
              int ny = y + dy[dir];
              if (nx < 0 || nx >= w || ny < 0 || ny >= h || zs[ny][nx] < zs[y][x]) {
                weight[index]++;
                continue;
              }
              if (zs[ny][nx] != zs[y][x]) { continue; }
              que.push(ny * w + nx);
            }
          }
        }
      }
      sort(order.rbegin(), order.rend());

      // add edges
      REP(sy, h) {
        REP(sx, w) {
          int from = mapto[sy][sx];
          if (from == 0) { continue; }
          REP(dir, 4) {
            int nx = sx + dx[dir];
            int ny = sy + dy[dir];
            if (nx < 0 || nx >= w || ny < 0 || ny >= h) { continue; }
            if (zs[ny][nx] >= zs[sy][sx]) { continue; }
            int to = mapto[ny][nx];
            g[from][to] += 1.0 / weight[from];
          }
        }
      }
    }

    scanf("%d", &l);
    REP(i, l) {
      double t;
      int x, y;
      scanf("%lf %d %d", &t, &x, &y);
      ans[i] = calc(t, mapto[y][x]);
    }

    REP(i, l) {
      printf("%d\n", (int)floor(ans[i] + EPS));
    }
  }
}

Measured memory_bytes: 0