#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
long long a[555][555];
int t, ii, j;
long long sum[555][555];

struct pravougaonik {
    int x1, y1, x2, y2, mark;
} niz[111111];

int main() {
    FILE *ff = fopen("mrvice.in", "r");
    FILE *gg = fopen("mrvice.out", "w");
    int res, n, q, koji, k = 1;
    fscanf(ff, "%d%d", &n, &q);
    niz[1].x1 = 1;
    niz[1].y1 = 1;
    niz[1].x2 = n;
    niz[1].y2 = n;
    niz[1].mark = 1;


    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) fscanf(ff, "%lld", &a[i][j]);
    sum[1][1] = a[1][1];
    for (int i = 0; i <= n; i++) sum[0][i] = sum[i][0] = 0;
    for (int i = 2; i <= n; i++) sum[i][1] = sum[i-1][1] + a[i][1];
    for (int j = 2; j <= n; j++) sum[1][j] = sum[1][j-1] + a[1][j];
    for (int i = 2; i <= n; i++) {
        for (int j = 2; j <= n; j++) {
            sum[i][j] = sum[i][j-1];
            for (int k = 1; k <= i; k++) sum[i][j] += a[k][j];
        }
    }


    for (int zzz = 1; zzz <= q; zzz++) {
        fscanf(ff, "%d%d%d", &t, &ii, &j);
        if (t == 1) {
            if (ii == 0) {
                koji = 1;
                while (!((niz[koji].y1 <= j) && (niz[koji].y2 > j)) || (niz[koji].mark == 0)) koji++;
                for (int i = koji+1; i <= k; i++) {
                    if ((niz[i].y1 <= j) && (niz[i].y2 > j) && (niz[i].mark != 0) && (niz[i].x1 < niz[koji].x1)) koji = i;
                }
                niz[koji].mark = 0;
                niz[k+1].x1 = niz[koji].x1;
                niz[k+1].y1 = niz[koji].y1;
                niz[k+1].x2 = niz[koji].x2;
                niz[k+1].y2 = j;
                niz[k+2].x1 = niz[koji].x1;
                niz[k+2].y1 = j+1;
                niz[k+2].x2 = niz[koji].x2;
                niz[k+2].y2 = niz[koji].y2;
                niz[k+1].mark = niz[k+2].mark = 1;
                k += 2;
            }
            if (ii == 1) {
                koji = 1;
                while (!((niz[koji].x1 <= j) && (niz[koji].x2 > j)) || (niz[koji].mark == 0)) koji++;
                for (int i = koji+1; i <= k; i++) {
                    if ((niz[i].x1 <= j) && (niz[i].x2 > j) && (niz[i].mark != 0) && (niz[i].y1 > niz[koji].y1)) koji = i;
                }
                niz[koji].mark = 0;
                niz[k+1].x1 = niz[koji].x1;
                niz[k+1].y1 = niz[koji].y1;
                niz[k+1].x2 = j;
                niz[k+1].y2 = niz[koji].y2;
                niz[k+2].x1 = j+1;
                niz[k+2].y1 = niz[koji].y1;
                niz[k+2].x2 = niz[koji].x2;
                niz[k+2].y2 = niz[koji].y2;
                niz[k+1].mark = niz[k+2].mark = 1;
                k += 2;
            }
            if (ii == 2) {
                koji = 1;
                while (!((niz[koji].y1 <= j) && (niz[koji].y2 > j)) || (niz[koji].mark == 0)) koji++;
                for (int i = koji+1; i <= k; i++) {
                    if ((niz[i].y1 <= j) && (niz[i].y2 > j) && (niz[i].mark != 0) && (niz[i].x1 > niz[koji].x1)) koji = i;
                }
                niz[koji].mark = 0;
                niz[k+1].x1 = niz[koji].x1;
                niz[k+1].y1 = niz[koji].y1;
                niz[k+1].x2 = niz[koji].x2;
                niz[k+1].y2 = j;
                niz[k+2].x1 = niz[koji].x1;
                niz[k+2].y1 = j+1;
                niz[k+2].x2 = niz[koji].x2;
                niz[k+2].y2 = niz[koji].y2;
                niz[k+1].mark = niz[k+2].mark = 1;
                k += 2;
            }
            if (ii == 3) {
                koji = 1;
                while (!((niz[koji].x1 <= j) && (niz[koji].x2 > j)) || (niz[koji].mark == 0)) koji++;
                for (int i = koji+1; i <= k; i++) {
                    if ((niz[i].x1 <= j) && (niz[i].x2 > j) && (niz[i].mark != 0) && (niz[i].y1 < niz[koji].y1)) koji = i;
                }
                niz[koji].mark = 0;
                niz[k+1].x1 = niz[koji].x1;
                niz[k+1].y1 = niz[koji].y1;
                niz[k+1].x2 = j;
                niz[k+1].y2 = niz[koji].y2;
                niz[k+2].x1 = j+1;
                niz[k+2].y1 = niz[koji].y1;
                niz[k+2].x2 = niz[koji].x2;
                niz[k+2].y2 = niz[koji].y2;
                niz[k+1].mark = niz[k+2].mark = 1;
                k += 2;
            }
        }

        long long vre;
        if (t == 2) {
            res = 0;
            for (int i = 1; i <= k; i++) {
                vre = sum[niz[i].x2][niz[i].y2] - sum[niz[i].x1 - 1][niz[i].y2] - sum[niz[i].x2][niz[i].y1 - 1] + sum[niz[i].x1 - 1][niz[i].y1 - 1];
                if ((ii <= vre) && (vre <= j) && (niz[i].mark != 0)) res++;
            }
            fprintf(gg, "%d\n", res);
        }

    }
    //printf("\n\n");
    //for (int i = 1; i <= k; i++) printf("%d %d %d %d %d\n", niz[i].x1, niz[i].y1, niz[i].x2, niz[i].y2, niz[i].mark);
}
