#include <bits/stdc++.h>
using namespace std;
const int N = 1009, K = 41, Mod = 1e9 + 7;
int n, m, q, k, C[N][N], dp[K][N][N];
bool M[K][K];
int gx[4] = {-1, 1, 0, 0};
int gy[4] = {0, 0, -1, 1};
vector<pair<int, int> > V[K];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> C[i][j], dp[C[i][j]][i][j] = 0, V[C[i][j]].push_back({i, j});
  for (int c = 1; c <= k; c++) {
    queue<pair<int, int> > qu;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        if (C[i][j] == c) dp[C[i][j]][i][j] = 0, qu.push({i, j});
    while (qu.size()) {
      int a = qu.front().first;
      int b = qu.front().second;
      qu.pop();
      if (!M[c][C[a][b]]) {
        M[c][C[a][b]] = 1;
        for (auto X : V[C[a][b]])
          if (dp[c][X.first][X.second] == -1)
            dp[c][X.first][X.second] = dp[c][a][b] + 1,
            qu.push({X.first, X.second});
      }
      for (int h = 0; h < 4; h++) {
        int ta = a + gx[h], tb = b + gy[h];
        if (ta < 1 || ta > n || tb < 1 || tb > m || dp[c][ta][tb] != -1)
          continue;
        dp[c][ta][tb] = dp[c][a][b] + 1;
        qu.push({ta, tb});
      }
    }
  }
  cin >> q;
  for (; q; q--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int Mn = abs(a - c) + abs(b - d);
    for (int h = 1; h <= k; h++) Mn = min(Mn, dp[h][a][b] + dp[h][c][d] + 1);
    printf("%d\n", Mn);
  }
  return 0;
}
