#include <bits/stdc++.h>
using namespace std;
const int N = 1010 ;
int a[ N] [ N] , dis[ 50 ] [ N] [ N] , vis[ N] [ N] , used[ 50 ] ;
int n, m, k;
int mov[ 4 ] [ 2 ] = { { 0 , 1 } , { 0 , - 1 } , { 1 , 0 } , { - 1 , 0 } } ;
struct node{
int x, y;
node ( int _x, int _y) {
x= _x, y= _y;
}
} ;
queue< node> q;
vector< node> v[ 50 ] ;
void bfs ( int id) {
memset ( vis, 0 , sizeof ( vis) ) ;
memset ( used, 0 , sizeof ( used) ) ;
while ( ! q. empty ( ) ) {
q. pop ( ) ;
}
for ( int i= 0 ; i< v[ id] . size ( ) ; i++ ) {
int x= v[ id] [ i] . x, y= v[ id] [ i] . y;
q. push ( node ( x, y) ) ;
vis[ x] [ y] = 1 ;
dis[ id] [ x] [ y] = 0 ;
}
used[ id] = 1 ;
while ( ! q. empty ( ) ) {
node u= q. front ( ) ;
q. pop ( ) ;
int x= u. x, y= u. y, col= a[ x] [ y] ;
if ( ! used[ col] ) {
used[ col] = 1 ;
for ( int i= 0 ; i< v[ col] . size ( ) ; i++ ) {
int xx= v[ col] [ i] . x, yy= v[ col] [ i] . y;
if ( vis[ xx] [ yy] ) continue ;
q. push ( node ( xx, yy) ) ;
vis[ xx] [ yy] = 1 ;
dis[ id] [ xx] [ yy] = dis[ id] [ x] [ y] + 1 ;
}
}
for ( int i= 0 ; i< 4 ; i++ ) {
int xx= x+ mov[ i] [ 0 ] , yy= y+ mov[ i] [ 1 ] ;
if ( xx> 0 && xx<= n&& yy> 0 && yy<= m&& ! vis[ xx] [ yy] ) {
q. push ( node ( xx, yy) ) ;
vis[ xx] [ yy] = 1 ;
dis[ id] [ xx] [ yy] = dis[ id] [ x] [ y] + 1 ;
}
}
}
}
int main ( ) {
cin>> n>> m>> k;
for ( int i= 1 ; i<= n; i++ ) {
for ( int j= 1 ; j<= m; j++ ) {
scanf ( "%d" , & a[ i] [ j] ) ;
v[ a[ i] [ j] ] . push_back ( node ( i, j) ) ;
}
}
for ( int i= 1 ; i<= k; i++ ) {
bfs ( i) ;
}
int t;
cin>> t;
while ( t-- ) {
int r1, c1, r2, c2;
scanf ( "%d%d%d%d" , & r1, & c1, & r2, & c2) ;
int ans= abs ( r1- r2) + abs ( c1- c2) ;
for ( int i= 1 ; i<= k; i++ ) {
ans= min ( ans, dis[ i] [ r1] [ c1] + dis[ i] [ r2] [ c2] + 1 ) ;
}
printf ( "%d\n" , ans) ;
}
return 0 ;
}