#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
using namespace std;

#define pb push_back
#define ll long long

#define MaxN 510

struct Tabela
{
    int r1,c1,r2,c2;

    Tabela() {}
    Tabela( int _r1, int _c1, int _r2, int _c2 ) {
        r1 = _r1;
        c1 = _c1;
        r2 = _r2;
        c2 = _c2;
    }
};

int n;
ll dp[MaxN][MaxN];
ll a[MaxN][MaxN];

int o,l,h,q;

vector<Tabela> v;
int size;

int main()
{
    freopen("mrvice.in","r",stdin);
    freopen("mrvice.out","w",stdout);

    scanf("%d%d",&n,&q);
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            scanf("%lld",&a[i][j]);

    dp[0][0] = a[0][0];
    for (int i = 1; i < n; ++i)
        dp[0][i] = dp[0][i-1] + a[0][i];
    for (int i = 1; i < n; ++i)
        dp[i][0] = dp[i-1][0] + a[i][0];
    for (int i = 1; i < n; ++i)
        for (int j = 1; j < n; ++j)
            dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1];

    v.pb( Tabela( 0, 0, n-1, n-1 ) );
    size = 1;

    for (int j = 0; j < q; ++j) {
        scanf("%d%d%d",&o,&l,&h);

        if ( o == 1 ) {
            switch(l) {
                case 0:
                        for (int i = 0; i < size; ++i) {
                            int r1 = v[i].r1;
                            int c1 = v[i].c1;
                            int r2 = v[i].r2;
                            int c2 = v[i].c2;

                            if ( r1 == 0 && c1 <= h && h <= c2 ) {
                                v.erase( v.begin()+i );

                                v.pb( Tabela( r1, c1, r2, h-1 ) );
                                v.pb( Tabela( r1, h, r2, c2 ) );
                                size++;
                                break;
                            }
                        }
                        break;
                case 1:
                        for (int i = 0; i < size; ++i) {
                            int r1 = v[i].r1;
                            int c1 = v[i].c1;
                            int r2 = v[i].r2;
                            int c2 = v[i].c2;

                            if ( c2 == n-1 && r1 <= h && h <= r2 ) {
                                v.erase( v.begin()+i );

                                v.pb( Tabela( r1, c1, h-1, c2 ) );
                                v.pb( Tabela( h, c1, r2, c2 ) );
                                size++;
                                break;
                            }
                        }
                        break;
                case 2:
                        for (int i = 0; i < size; ++i) {
                            int r1 = v[i].r1;
                            int c1 = v[i].c1;
                            int r2 = v[i].r2;
                            int c2 = v[i].c2;

                            if ( r2 == n-1 && c1 <= h && h <= c2 ) {
                                v.erase( v.begin()+i );

                                v.pb( Tabela( r1, c1, r2, h-1 ) );
                                v.pb( Tabela( r1, h, r2, c2 ) );
                                size++;
                                break;
                            }
                        }
                        break;
                case 3:
                        for (int i = 0; i < size; ++i) {
                            int r1 = v[i].r1;
                            int c1 = v[i].c1;
                            int r2 = v[i].r2;
                            int c2 = v[i].c2;

                            if ( c1 == 0 && r1 <= h && h <= r2 ) {
                                v.erase( v.begin()+i );

                                v.pb( Tabela( r1, c1, h-1, c2 ) );
                                v.pb( Tabela( h, c1, r2, c2 ) );
                                size++;
                                break;
                            }
                        }
                        break;
            }
        }
        else {
            int ret = 0;
            for (int i = 0; i < size; ++i) {
                int r1 = v[i].r1;
                int c1 = v[i].c1;
                int r2 = v[i].r2;
                int c2 = v[i].c2;

                ll zbir = dp[r2][c2];
                if ( c1 > 0 ) zbir -= dp[r2][c1-1];
                if ( r1 > 0 ) zbir -= dp[r1-1][c2];
                if ( c1 > 0 && r1 > 0 ) zbir += dp[r1-1][c1-1];

                if ( l <= zbir && zbir <= h ) ret++;
            }
            printf("%d\n",ret);
        }

    }

    return 0;
}
