#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 3;
const int X[] = {0, 1, 0, -1};
const int Y[] = {1, 0, -1, 0};
int m, n, k;
int a[N][N];
int dp[N][N][41];
vector<pair<int, int> > color[41];
bool vs[41];
int q;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> m >> n >> k;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      color[a[i][j]].push_back(pair<int, int>{i, j});
      for (int t = 1; t <= k; t++) {
        dp[i][j][t] = m * n + 3;
      }
    }
  }
  for (int t = 1; t <= k; t++) {
    queue<pair<int, int> > qu;
    for (pair<int, int> top : color[t]) {
      int i = top.first;
      int j = top.second;
      dp[i][j][t] = 0;
      qu.push(pair<int, int>{i, j});
    }
    for (int i = 1; i <= k; i++) vs[i] = false;
    while (qu.size()) {
      int i = qu.front().first;
      int j = qu.front().second;
      qu.pop();
      for (int nxt = 0; nxt <= 3; nxt++) {
        int u = i + X[nxt];
        int v = j + Y[nxt];
        if (u >= 1 && v >= 1 && u <= m && v <= n) {
          if (dp[u][v][t] > dp[i][j][t] + 1) {
            dp[u][v][t] = dp[i][j][t] + 1;
            qu.push(pair<int, int>{u, v});
          }
        }
      }
      int cl = a[i][j];
      if (vs[cl]) continue;
      vs[cl] = true;
      for (pair<int, int> top : color[cl]) {
        int u = top.first;
        int v = top.second;
        if (dp[u][v][t] > dp[i][j][t] + 1) {
          dp[u][v][t] = dp[i][j][t] + 1;
          qu.push(pair<int, int>{u, v});
        }
      }
    }
  }
  cin >> q;
  while (q--) {
    int r1, c1, r2, c2;
    cin >> r1 >> c1 >> r2 >> c2;
    if (r1 == r2 && c1 == c2) {
      cout << 0 << '\n';
      continue;
    }
    int res = 0;
    int cur = abs(r1 - r2) + abs(c1 - c2);
    for (int t = 1; t <= k; t++) {
      res = dp[r1][c1][t] + dp[r2][c2][t] + 1;
      cur = min(cur, res);
    }
    cout << cur << '\n';
  }
}
