#include <bits/stdc++.h>
using namespace std;
template <typename U, typename V>
void Min(U &a, const V &b) {
  if (a > b) a = b;
}
template <typename U, typename V>
void Max(U &a, const V &b) {
  if (a < b) a = b;
}
template <typename U, typename V>
void add(U &a, const V &b) {
  a = (a + b) % 1000000007;
}
template <typename U>
U gcd(U a, U b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a >= b)
    return gcd(a % b, b);
  else
    return gcd(a, b % a);
}
int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % 1000000007;
    a = 1LL * a * a % 1000000007;
    b >>= 1;
  }
  return ans;
}
int pow(int a, int b, int c) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % c;
    a = 1LL * a * a % c;
    b >>= 1;
  }
  return ans;
}
bool in(int first, int second, int n, int m) {
  return first >= 0 && first < n && second >= 0 && second < m;
}
int a[1005][1005], d[1005][1005];
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
vector<pair<int, int> > v[41];
int vis[41];
struct node {
  int x1, y1, x2, y2;
};
node q[100005];
int ans[100005];
void cal(int n, int m, int K, int k) {
  queue<pair<int, int> > q;
  memset(d, -1, sizeof(d));
  for (int l = 0; l < K; l++) vis[l] = 0;
  vis[k] = 1;
  for (auto &o : v[k]) {
    q.push(o);
    d[o.first][o.second] = 0;
  }
  while (!q.empty()) {
    pair<int, int> e = q.front();
    q.pop();
    int first = a[e.first][e.second];
    for (int l = 0; l < 4; l++) {
      int nx = e.first + dx[l], ny = e.second + dy[l];
      if (in(nx, ny, n, m) && a[nx][ny] != first) {
        if (d[nx][ny] == -1) {
          d[nx][ny] = d[e.first][e.second] + 1;
          q.push({nx, ny});
        }
      }
    }
    if (!vis[first]) {
      vis[first] = 1;
      for (auto &o : v[first]) {
        if (d[o.first][o.second] < 0) {
          d[o.first][o.second] = d[e.first][e.second] + 1;
          q.push(o);
        }
      }
    }
  }
}
int main() {
  int T, n, m, K, i, j, k;
  scanf("%d%d%d", &n, &m, &K);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      scanf("%d", &a[i][j]), a[i][j]--, v[a[i][j]].push_back({i, j});
  }
  scanf("%d", &T);
  int x1, y1, x2, y2;
  for (int i = 0; i < T; i++) {
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    x1--, y1--, x2--, y2--;
    ans[i] = abs(x1 - x2) + abs(y1 - y2);
    q[i] = {x1, y1, x2, y2};
  }
  for (int k = 0; k < K; k++) {
    cal(n, m, K, k);
    for (int i = 0; i < T; i++) {
      Min(ans[i], d[q[i].x1][q[i].y1] + d[q[i].x2][q[i].y2] + 1);
    }
  }
  for (int i = 0; i < T; i++) printf("%d\n", ans[i]);
}
