Program cel;
Type treiD=array[1..8,1..8,0..3] of longint;
var f,g:text;
b:treiD;
m,a,max,p,w:longint;
n,i,j,k,ex:integer;
procedure comparare(var max:longint;i,j,k:integer);
Begin
If max<b[i,j,k] then max:=b[i,j,k];
end;
Begin
Assign(f,'plantatie.in');Reset(f);
Assign(g,'plantatie.out');Rewrite(g);
Readln(f,n,m);
k:=0;
p:=1;
While p<=n do
Begin
i:=1;
While i<=n-p+1 do
Begin
j:=1;
While j<=n-p+1 do
Begin
If k=0 then Begin
Read(f,a);
b[i,j,k]:=a;
end
else Begin
max:=b[i,j,k-1];
comparare(max,i+p div 2,j,k-1);
comparare(max,i,j+p div 2,k-1);
comparare(max,i+p div 2,j+p div 2,k-1);
b[i,j,k]:=max;
end;
inc(j);
end;
If k=0 then Readln(f);
inc(i);
end;
inc(k);
p:=p*2;
end;
For w:=1 to m do
Begin
Readln(f,i,j,k);
p:=1;
ex:=0;
While p*2<=k do
Begin
inc(ex);
p:=p*2;
end;
If p-k=0 then Writeln(g,b[i,j,ex])
else Begin
max:=b[i,j,ex];
comparare(max,i+p div 2,j,ex);
comparare(max,i,j+p div 2,ex);
comparare(max,i+p div 2,j+p div 2,ex);
Writeln(g,max);
end;
end;
Close(f);
Close(g);
end.