#include <bits/stdc++.h>
using namespace std;
const int N = 1005, M = 41;
int dis[N][N][M], c[N][N], dc[M][M];
int n, m, K, q;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m >> K;
  memset(dis, 0x2f, sizeof dis);
  memset(dc, 0x2f, sizeof dc);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) cin >> c[i][j], dis[i][j][c[i][j]] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      for (int k = 1; k <= K; ++k)
        dis[i][j][k] =
            min({dis[i][j][k], dis[i][j - 1][k] + 1, dis[i - 1][j][k] + 1});
  for (int i = 1; i <= n; ++i)
    for (int j = m; j; --j)
      for (int k = 1; k <= K; ++k)
        dis[i][j][k] =
            min({dis[i][j][k], dis[i][j + 1][k] + 1, dis[i - 1][j][k] + 1});
  for (int i = n; i; --i)
    for (int j = 1; j <= m; ++j)
      for (int k = 1; k <= K; ++k)
        dis[i][j][k] =
            min({dis[i][j][k], dis[i][j - 1][k] + 1, dis[i + 1][j][k] + 1});
  for (int i = n; i; --i)
    for (int j = m; j; --j)
      for (int k = 1; k <= K; ++k)
        dis[i][j][k] =
            min({dis[i][j][k], dis[i][j + 1][k] + 1, dis[i + 1][j][k] + 1});
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      for (int k = 1; k <= K; ++k)
        dc[c[i][j]][k] = min(dc[c[i][j]][k], 1 + dis[i][j][k]);
  for (int k = 1; k <= K; ++k)
    for (int i = 1; i <= K; ++i)
      if (i != k)
        for (int j = 1; j <= K; ++j)
          if (i != j && j != k) dc[i][j] = min(dc[i][j], dc[i][k] + dc[k][j]);
  for (cin >> q; q--;) {
    int r1, c1, r2, c2;
    cin >> r1 >> c1 >> r2 >> c2;
    int ans = abs(r1 - r2) + abs(c1 - c2);
    if (c[r1][c1] == c[r2][c2]) ans = min(ans, 1);
    ans = min(ans, dc[c[r1][c1]][c[r2][c2]] + 1);
    for (int k = 1; k <= K; ++k)
      ans = min({ans, dis[r1][c1][k] + dc[k][c[r2][c2]] + 1,
                 dis[r2][c2][k] + dc[k][c[r1][c1]] + 1,
                 dis[r1][c1][k] + dis[r2][c2][k] + 1});
    for (int k0 = 1; k0 <= K; ++k0)
      for (int k1 = 1; k1 <= K; ++k1)
        ans = min(ans, dis[r1][c1][k0] + dis[r2][c2][k1] + dc[k0][k1] + 1);
    cout << ans << '\n';
  }
  return 0;
}
