#define ll long long
#include<bits/stdc++.h>
using namespace std;
const ll N = 805;
const ll range = 1e9;
ll a[N][N];
ll s[N][N],b[N][N];
int main()
{
   ll n,k,l=0,mid,r=range,i,j;

   cin>>n>>k;

   for(i=0;i<n;i++)
   {
       for(j=0;j<n;j++)cin>>a[i][j];
   }

   ll lim = ((k*k)/2) + 1;

   while(l<=r)
   {
       mid = (l+r)/2;

       for(i=0;i<n;i++)
       {
           s[i][0] = 0;
           s[0][i] = 0;
           for(j=0;j<n;j++)
           {
               if(a[i][j]>mid)b[i][j] = 1;

               else
                b[i][j] = 0;
           }
       }

       for(i=1;i<=n;i++)
       {
           for(j=1;j<=n;j++)
           {
               s[i][j] = s[i-1][j] + s[i][j-1] + b[i-1][j-1] - s[i-1][j-1];
           }
       }

       bool flag = false;

       for(i=1;i<=n-k+1;i++)
       {
           for(j=1;j<=n-k+1;j++)
           {
               if(s[i+k-1][j+k-1]-s[i+k-1][j-1]-s[i-1][j+k-1]+s[i-1][j-1]<=lim-1)flag = true;
           }
       }

       if(l==r)break;

       else{

       if(flag)r = mid;

       else
          l = mid+1;
       }

   }

   cout<<mid<<endl;
}

#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>

#define LL long long
#define Maxn 200005

using namespace std;

LL read() {
	LL x = 0, f = 1;
	char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') f = -1;
		c = getchar();
	}
	while('0' <= c && c <= '9') {
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * f;
}

int n, q, fa[Maxn], siz[Maxn];
map <int, int> S[Maxn];

#define IT map <int, int> ::iterator

int find(int x) {
	if(fa[x] == x) return x;
	else return find(fa[x]);
}

int main() {
	n = read(); q = read();
	int c;
	for(int i = 1; i <= n; ++i) {
		c = read(); fa[i] = i; siz[i] = 1;
		S[i][c] = 1;
	}
	int opt, x, y;
	for(int i = 1; i <= q; ++i) {
		opt = read(); x = read(); y = read();
		if(opt == 1) {
			int anx = find(x), any = find(y);
			if(anx == any) continue;
			if(siz[anx] < siz[any]) swap(anx, any);
			fa[any] = anx;
			siz[anx] += siz[any];
			for(IT it = S[any].begin(); it != S[any].end(); ++it) S[anx][(*it).first] += (*it).second;
		}
		if(opt == 2) {
			int anx = find(x);
			printf("%d\n", S[anx][y]);
		}
	}
	return 0;
}