{$I-,Q-,R-,S-}
CONST
FIN = 'plantatie.in';
FOUT = 'plantatie.out';
NMAX = 500;
KMAX = 9;
type
matrix = array[ 1..NMAX, 1..NMAX, 0..KMAX ] of longint;
var
A : matrix;
put : array[ 0..NMAX ] of longint;
f, g : text;
N, M, i, j, x, y, k, pow : longint;
function MAX( a, b, c, d : longint ) : longint;
var max1 : longint;
begin
if a > b then max1 := a else max1 := b;
if c > max1 then max1 := c;
if d > max1 then max1 := d;
MAX := max1;
end;
begin
assign( f, FIN ); reset( f ); readln( f, N, M );
assign( g, FOUT ); rewrite( g );
for i := 1 to N do
for j := 1 to N do read( f, A[i][j][0] );
for K := 1 to KMAX do
for i := 1 to N do
for j := 1 to N do
if ( i + ( 1 shl k ) < N + 2 ) and ( j + ( 1 shl k ) < N + 2 ) then
A[i][j][k] := MAX( A[ i, j, k - 1 ],
A[ i, j + ( 1 shl (k-1) ), k - 1 ],
A[ i + ( 1 shl (k-1) ), j, k - 1 ],
A[ i + ( 1 shl (k-1) ), j + ( 1 shl (k-1) ), k - 1 ] );
put[1] := 0;
for i := 1 to kmax do
if ( 1 shl i <= N ) then put[ 1 shl i ] := i;
for i := 3 to N do
if put[i] = 0 then put[i] := put[ i - 1 ];
for i := 1 to M do
begin
readln( f, x, y, k );
pow := put[k];
writeln( g, MAX( A[ x, y, pow ],
A[ x, y + k - ( 1 shl pow ), pow ],
A[ x + k - ( 1 shl pow ), y, pow ],
A[ x + k - ( 1 shl pow ), y + k - ( 1 shl pow ), pow ]
));
end;
close( f );
close( g );
end.