var p:array[1..1024,1..1024,0..9]of integer;
n,m,i,j,k,t,p2,r:integer;
f:integer;
begin
assign(input,'plantatie.in'); reset(input);
assign(output,'plantatie.out'); rewrite(output);
readln(n,m);
for i:=1 to n do begin
for j:=1 to n do read(p[i,j,0]);
readln;
end;
for k:=1 to 9 do begin
f:=1 shl (k-1);
for i:=1 to n do
for j:=1 to n do begin
p[i,j,k]:=p[i,j,k-1];
if p[i,j+f,k-1]>p[i,j,k] then p[i,j,k]:=p[i,j+f,k-1];
if p[i+f,j,k-1]>p[i,j,k] then p[i,j,k]:=p[i+f,j,k-1];
if p[i+f,j+f,k-1]>p[i,j,k] then p[i,j,k]:=p[i+f,j+f,k-1];
end;
end;
for t:=1 to m do begin
readln(i,j,k);
p2:=trunc(ln(k)/ln(2)); f:=1 shl p2;
r:=p[i,j,p2];
if p[i,j+k-f,p2]>r then r:=p[i,j+k-f,p2];
if p[i+k-f,j,p2]>r then r:=p[i+k-f,j,p2];
if p[i+k-f,j+k-f,p2]>r then r:=p[i+k-f,j+k-f,p2];
writeln(r);
end;
close(input); close(output);
end.