// 二维 rmq .
1
//
author : gssn
2 // date : 5.17
3 #include < iostream >
4 #include < cmath >
5 using namespace std;
6 int maxx[ 255 ][ 255 ][ 8 ],minn[ 255 ][ 255 ][ 8 ],f[ 255 ][ 255 ];
7 int n,b,kk;
8 void init()
9 {
10 int len = int (log( double (n)) / log( 2.0 ));
11 for ( int k = 1 ;k <= len;k ++ )
12 {
13 for ( int i = 1 ;i <= n;i ++ )
14 {
15 for ( int j = 1 ;j + ( 1 << (k - 1 )) - 1 <= n;j ++ )
16 {
17 maxx[i][j][k] = max(maxx[i][j][k - 1 ],maxx[i][j + ( 1 << (k - 1 ))][k - 1 ]);
18 minn[i][j][k] = min(minn[i][j][k - 1 ],minn[i][j + ( 1 << (k - 1 ))][k - 1 ]);
19 }
20 }
21 }
22 }
23 int query_( int a, int c, int len)
24 {
25 int x =- 1 ,y = 1000 ;
26 for ( int i = a;i < a + b;i ++ ){
27 x = max(x,max(maxx[i][c][len],maxx[i][c + b - ( 1 << len)][len]));
28 y = min(y,min(minn[i][c][len],minn[i][c + b - ( 1 << len)][len]));
29 }
30 return x - y;
31 }
32 int main()
33 {
34 int a,c;
35 cin >> n >> b >> kk;
36 for ( int i = 1 ;i <= n;i ++ )
37 for ( int j = 1 ;j <= n;j ++ )
38 {
39 cin >> f[i][j];
40 maxx[i][j][ 0 ] = minn[i][j][ 0 ] = f[i][j];
41 }
42 int len = int (log( double (b)) / log( 2.0 ));
43 init();
44 while (kk -- )
45 {
46 cin >> a >> c;
47 cout << query_(a,c,len) << endl;
48 }
49 return 0 ;
50 }
2 // date : 5.17
3 #include < iostream >
4 #include < cmath >
5 using namespace std;
6 int maxx[ 255 ][ 255 ][ 8 ],minn[ 255 ][ 255 ][ 8 ],f[ 255 ][ 255 ];
7 int n,b,kk;
8 void init()
9 {
10 int len = int (log( double (n)) / log( 2.0 ));
11 for ( int k = 1 ;k <= len;k ++ )
12 {
13 for ( int i = 1 ;i <= n;i ++ )
14 {
15 for ( int j = 1 ;j + ( 1 << (k - 1 )) - 1 <= n;j ++ )
16 {
17 maxx[i][j][k] = max(maxx[i][j][k - 1 ],maxx[i][j + ( 1 << (k - 1 ))][k - 1 ]);
18 minn[i][j][k] = min(minn[i][j][k - 1 ],minn[i][j + ( 1 << (k - 1 ))][k - 1 ]);
19 }
20 }
21 }
22 }
23 int query_( int a, int c, int len)
24 {
25 int x =- 1 ,y = 1000 ;
26 for ( int i = a;i < a + b;i ++ ){
27 x = max(x,max(maxx[i][c][len],maxx[i][c + b - ( 1 << len)][len]));
28 y = min(y,min(minn[i][c][len],minn[i][c + b - ( 1 << len)][len]));
29 }
30 return x - y;
31 }
32 int main()
33 {
34 int a,c;
35 cin >> n >> b >> kk;
36 for ( int i = 1 ;i <= n;i ++ )
37 for ( int j = 1 ;j <= n;j ++ )
38 {
39 cin >> f[i][j];
40 maxx[i][j][ 0 ] = minn[i][j][ 0 ] = f[i][j];
41 }
42 int len = int (log( double (b)) / log( 2.0 ));
43 init();
44 while (kk -- )
45 {
46 cin >> a >> c;
47 cout << query_(a,c,len) << endl;
48 }
49 return 0 ;
50 }