#include<stdio.h>
#include<algorithm>
using namespace std;

int n,xx[1000111],yy[1000111],a[1111][1111];
bool mark[1111][1111],mark5[1111][1111];
int dx[4]={1,0,-1,0}, dy[4]={0,-1,0,1};

bool izlazi(int x, int y) {
    return (x<1 || x>n || y<1 || y>n);
}

bool lose(int x, int y) {
    return (x<1 || x>n || y<1 || y>n || mark[x][y]);
}

bool lose5(int x, int y) {
    return (x<1 || x>n || y<1 || y>n || mark5[x][y]);
}

void seci(int s, int b) {

    int x,y;

    if (s==0) {
        x=1; y=b;
    }
    if (s==2) {
        x=n; y=b;
    }
    if (s==3) {
        x=b; y=1;
    }
    if (s==1) {
        x=b; y=n;
    }

    //printf("---> %d %d\n", x, y);

    while( mark[x][y] && !izlazi(x,y) ) {
        x+=dx[s];
        y+=dy[s];
    }

    while( !lose(x,y) ) {
        mark[x][y]=true;
        x+=dx[s];
        y+=dy[s];
        //printf("~~~ok~~~\n");
    }
    return;
}

long long bfs(int px, int py) {

    int x,y,s,pc,kr;
    long long t;

    pc=0; kr=1;
    xx[1]=px; yy[1]=py;
    mark5[px][py]=true;
    t=a[px][py];
    while( pc<kr ) {
        pc++;
        //printf("~~~> %d %d\n", xx[pc], yy[pc]);
        for(s=0; s<4; s++) {
            x=xx[pc]+dx[s];
            y=yy[pc]+dy[s];
            if ( !lose5(x,y) ) {
                mark5[x][y]=true;
                kr++;
                xx[kr]=x;
                yy[kr]=y;
                t+=a[x][y];
            }
        }
    }
    return t;
}

int izmedju(long long a, long long b) {

    int i,j,res;
    long long t;

    for(i=0; i<=n; i++)
      for(j=0; j<=n; j++) {
          mark5[i][j]=mark[i][j];
      }

    res=0;
    for(i=1; i<=n; i++)
      for(j=1; j<=n; j++) if (!mark[i][j] && !mark5[i][j]) {
          t=bfs(i,j);
          //printf("---> %I64lld\n", t);
          if (t>=a && t<=b) res++;
      }

    return res;
}

int main() {

    FILE *ff = fopen("mrvice.in", "r"), *gg = fopen("mrvice.out", "w");

    int q,i,j,op,aa,bb,ii,jj;
    long long ll,hh;

    fscanf(ff,"%d%d", &n, &q);
    for(i=0; i<=n; i++)
      for(j=0; j<=n; j++) {
          a[i][j]=0;
          mark[i][j]=false;
      }

    for(i=1; i<=n; i++)
      for(j=1; j<=n; j++) fscanf(ff,"%d", &a[i*2][j*2]);

    n=2*n;
    for(;q>0;q--) {
        fscanf(ff,"%d", &op);
        if (op==1) {
            fscanf(ff,"%d%d", &aa, &bb);
            seci(aa,bb*2+1);
            /*for(ii=0; ii<=n; ii++) {
                for(jj=0; jj<=n; jj++) {
                    if (mark[ii][jj]) printf("1"); else printf("0");
                }
                printf("\n");
            }*/
        } else {
            fscanf(ff,"%lld%lld", &ll, &hh);
            fprintf(gg,"%d\n",izmedju(ll,hh));
        }
    }

    fclose(ff); fclose(gg);
    return 0;
}
