#include <bits/stdc++.h>
using namespace std;
int a[1003][1003];
int cost[43][1003][1003];
vector<vector<pair<int, int>>> COL(43);
int n, m, k;
int di1[4] = {0, 1, 0, -1};
int di2[4] = {1, 0, -1, 0};
void bfs(int col) {
  vector<bool> vis(43, 0);
  queue<pair<int, int>> q;
  for (int i = 0; i < COL[col].size(); i++) {
    cost[col][COL[col][i].first][COL[col][i].second] = 0;
    q.push({COL[col][i].first, COL[col][i].second});
  }
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    if (!vis[a[x][y]]) {
      vis[a[x][y]] = 1;
      for (int i = 0; i < COL[a[x][y]].size(); i++) {
        int nx = COL[a[x][y]][i].first;
        int ny = COL[a[x][y]][i].second;
        if (cost[col][nx][ny] == -1) {
          cost[col][nx][ny] = 1 + cost[col][x][y];
          q.push({nx, ny});
        }
      }
    }
    for (int i = 0; i < 4; i++) {
      int nx = x + di1[i];
      int ny = y + di2[i];
      if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && cost[col][nx][ny] == -1) {
        cost[col][nx][ny] = 1 + cost[col][x][y];
        q.push({nx, ny});
      }
    }
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      COL[a[i][j]].push_back({i, j});
    }
  }
  memset(cost, -1, sizeof(cost));
  for (int i = 1; i <= k; i++) {
    bfs(i);
  }
  int q;
  cin >> q;
  while (q--) {
    int sx, sy, ex, ey;
    cin >> sx >> sy >> ex >> ey;
    int ans = abs(ex - sx) + abs(ey - sy);
    for (int i = 1; i <= k; i++) {
      ans = min(ans, 1 + cost[i][sx][sy] + cost[i][ex][ey]);
    }
    cout << ans << "\n";
  }
}
