#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1010, KMAX = 50;
const int INF = 0x3f3f3f3f;
const int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int n, m, K, q;
int mp[NMAX][NMAX];
vector<pair<int, int>> color[KMAX];
int cost[KMAX][NMAX][NMAX];
bool vis[KMAX];
int main() {
  memset(cost, 0x3f, sizeof(cost));
  scanf("%d%d%d", &n, &m, &K);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &mp[i][j]);
      color[mp[i][j]].push_back(make_pair(i, j));
    }
  }
  for (int c = 1; c <= K; c++) {
    memset(vis, 0, sizeof(vis));
    queue<pair<int, int>> q;
    int x, y, tox, toy;
    for (int i = 0; i < color[c].size(); i++) {
      q.push(color[c][i]);
      x = color[c][i].first, y = color[c][i].second;
      cost[c][x][y] = 0;
    }
    vis[c] = true;
    while (!q.empty()) {
      x = q.front().first, y = q.front().second;
      q.pop();
      if (!vis[mp[x][y]]) {
        for (int i = 0; i < color[mp[x][y]].size(); i++) {
          tox = color[mp[x][y]][i].first;
          toy = color[mp[x][y]][i].second;
          if (cost[c][tox][toy] == INF) {
            cost[c][tox][toy] = cost[c][x][y] + 1;
            q.push(make_pair(tox, toy));
          }
        }
        vis[mp[x][y]] = true;
      }
      for (int i = 0; i < 4; i++) {
        tox = x + dir[i][0];
        toy = y + dir[i][1];
        if (!(1 <= tox && tox <= n && 1 <= toy && toy <= m)) continue;
        if (cost[c][tox][toy] == INF) {
          cost[c][tox][toy] = cost[c][x][y] + 1;
          q.push(make_pair(tox, toy));
        }
      }
    }
  }
  scanf("%d", &q);
  while (q--) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    int ans = abs(x1 - x2) + abs(y1 - y2);
    for (int i = 1; i <= K; i++) {
      ans = min(ans, 1 + cost[i][x1][y1] + cost[i][x2][y2]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
