#include <stdio.h>
#define NMAX (1<<9)
#define MAX(a,b) (((a)>(b))?(a):(b))
int A[2*NMAX][2*NMAX], L1, L2, C1, C2, N, M, Vmax, Val;
void queryc(int,int,int,int);
void queryl(int p1, int p2, int nod)
{
int mij = (p1+p2)>>1, x = nod<<1;
if (L1<=p1 && p2<=L2)
{
queryc(0, NMAX-1, nod, 1);
return;
}
if ((p1<=L1 && L1<=p2) || (p1<=L2 && L2<=p2))
{
queryl(p1, mij, x);
queryl(mij+1, p2, x+1);
}
}
void queryc(int p1, int p2, int nodx, int nod)
{
int mij = (p1+p2)>>1, x = nod<<1;
if (C1 <= p1 && p2 <= C2)
{
Vmax = MAX(Vmax, A[nodx][nod]);
return;
}
if ((p1<=C1 && C1<=p2) || (p1<=C2 && C2<=p2))
{
queryc(p1, mij, nodx, x);
queryc(mij+1, p2, nodx, x+1);
}
}
void updatec(int,int,int);
void updatel(int l, int c)
{
int nod = NMAX+l;
updatec(l, c, nod);
while (nod > 0)
{
nod = nod>>1;
updatec(l, c, nod);
}
}
void updatec(int l, int c, int nod)
{
int nodc = NMAX+c, x;
A[nod][nodc] = Val;
while (nodc > 0)
{
nodc = nodc>>1;
x = nodc<<1;
A[nod][nodc] = A[nod][x];
if (A[nod][nodc]<A[nod][x+1]) A[nod][nodc] = A[nod][x+1];
}
}
int main()
{
int i, j, c;
freopen("plantatie.in", "r", stdin);
scanf("%d%d", &N, &M);
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
{
scanf("%d", &Val);
updatel(i,j);
}
freopen("plantatie.out", "w", stdout);
if (M==0) printf("0\n");
while (M--)
{
scanf("%d%d%d", &i, &j, &c); i--; j--;
L1 = i; L2 = i+c-1; C1 = j; C2 = j+c-1; Vmax = -1;
if (L2 >= N) L2 = N-1; if (C2 >= N) C2 = N-1;
queryl(0, NMAX-1, 1);
printf("%d\n", Vmax);
}
return 0;
}