{
  Marko Bakovic
  Mrvice
}

type point=record
          x1,y1,x2,y2:integer;
     end;

var i,j,n,q,k,stacksize,c,nbr,kx1,kx2,ky1,ky2:longint;
    l,h,sum:int64;
    b:array[0..505,0..505] of int64;
    stack1,stack2:array[1..3000] of point;
    ulaz,izlaz:text;

function vecex(tacka1,tacka2:point):boolean;
var temp:boolean;
begin
if tacka1.x1<tacka2.x1 then temp:=true
else if (tacka1.x1=tacka2.x1) and (tacka1.y1<tacka2.y1) then temp:=true
else temp:=false;
vecex:=temp;
end;

function vecey(tacka1,tacka2:point):boolean;
var temp:boolean;
begin
if tacka1.y1<tacka2.y1 then temp:=true
else if (tacka1.y1=tacka2.y1) and (tacka1.x1<tacka2.x1) then temp:=true
else temp:=false;
vecey:=temp;
end;

procedure sortx(l,r:integer);
var i,j:integer;
    pivot,temp2:point;
begin
i:=l;
j:=r;
pivot:=stack1[(i+j) div 2];
while i<=j do
  begin
    while vecex(stack1[i],pivot) do inc(i);
    while vecex(pivot,stack1[j]) do dec(j);
    if i<=j then
      begin
        temp2:=stack1[i];
        stack1[i]:=stack1[j];
        stack1[j]:=temp2;
        inc(i);
        dec(j);
      end;
  end;
if j>l then sortx(l,j);
if i<r then sortx(i,r);
end;

procedure sorty(l,r:integer);
var i,j:integer;
    pivot,temp2:point;
begin
i:=l;
j:=r;
pivot:=stack2[(i+j) div 2];
while i<=j do
  begin
    while vecey(stack2[i],pivot) do inc(i);
    while vecey(pivot,stack2[j]) do dec(j);
    if i<=j then
      begin
        temp2:=stack2[i];
        stack2[i]:=stack2[j];
        stack2[j]:=temp2;
        inc(i);
        dec(j);
      end;
  end;
if j>l then sorty(l,j);
if i<r then sorty(i,r);
end;

begin
assign(ulaz,'mrvice.in');
assign(izlaz,'mrvice.out');
reset(ulaz);
rewrite(izlaz);
readln(ulaz,n,q);
for i:=1 to n do
  begin
    for j:=1 to n do
      begin
        read(ulaz,b[i,j]);
        b[i,j]:=b[i,j]+b[i-1,j]+b[i,j-1]-b[i-1,j-1];
      end;
    readln(ulaz);
  end;
stacksize:=1;
stack1[1].x1:=1;
stack1[1].y1:=1;
stack1[1].x2:=n;
stack1[1].y2:=n;
stack2[1].x1:=1;
stack2[1].y1:=1;
stack2[1].x2:=n;
stack2[1].y2:=n;
for i:=1 to q do
  begin
    readln(ulaz,c,l,h);
    if c=1 then
      begin
        if l=0 then
          begin
            for j:=1 to stacksize do
              if (stack2[j].x1<=h) and (stack2[j].x2>=h+1) then
                begin
                  inc(stacksize);
                  for k:=1 to stacksize-1 do
                    if (stack1[k].x1=stack2[j].x1) and
                       (stack1[k].y1=stack2[j].y1) and
                       (stack1[k].x2=stack2[j].x2) and
                       (stack1[k].y2=stack2[j].y2) then
                      begin
                        stack1[k].x2:=h;
                        break;
                      end;
                  for k:=stacksize-1 downto j do stack2[k+1]:=stack2[k];
                  stack2[j].x2:=h;
                  stack2[j+1].x1:=h+1;
                  stack1[stacksize]:=stack2[j+1];
                  sortx(1,stacksize);
                  break;
                end;
          end
        else if l=2 then
          begin
            for j:=stacksize downto 1 do
              if (stack2[j].x1<=h) and (stack2[j].x2>=h+1) then
                begin
                  inc(stacksize);
                  for k:=1 to stacksize-1 do
                    if (stack1[k].x1=stack2[j].x1) and
                       (stack1[k].y1=stack2[j].y1) and
                       (stack1[k].x2=stack2[j].x2) and
                       (stack1[k].y2=stack2[j].y2) then
                      begin
                        stack1[k].x2:=h;
                        break;
                      end;
                  for k:=stacksize-1 downto j do stack2[k+1]:=stack2[k];
                  stack2[j].x2:=h;
                  stack2[j+1].x1:=h+1;
                  stack1[stacksize]:=stack2[j+1];
                  sortx(1,stacksize);
                  break;
                end;
          end
        else if l=3 then
          begin
            for j:=1 to stacksize do
              if (stack1[j].y1<=h) and (stack1[j].y2>=h+1) then
                begin
                  inc(stacksize);
                  for k:=1 to stacksize-1 do
                    if (stack1[j].x1=stack2[k].x1) and
                       (stack1[j].y1=stack2[k].y1) and
                       (stack1[j].x2=stack2[k].x2) and
                       (stack1[j].y2=stack2[k].y2) then
                      begin
                        stack2[k].y2:=h;
                        break;
                      end;
                  for k:=stacksize-1 downto j do stack1[k+1]:=stack1[k];
                  stack1[j].y2:=h;
                  stack1[j+1].y1:=h+1;
                  stack2[stacksize]:=stack1[j+1];
                  sorty(1,stacksize);
                  break;
                end;
          end
        else if l=1 then
          begin
            for j:=stacksize downto 1 do
              if (stack1[j].y1<=h) and (stack1[j].y2>=h+1) then
                begin
                  inc(stacksize);
                  for k:=1 to stacksize-1 do
                    if (stack1[j].x1=stack2[k].x1) and
                       (stack1[j].y1=stack2[k].y1) and
                       (stack1[j].x2=stack2[k].x2) and
                       (stack1[j].y2=stack2[k].y2) then
                      begin
                        stack2[k].y2:=h;
                        break;
                      end;
                  for k:=stacksize-1 downto j do stack1[k+1]:=stack1[k];
                  stack1[j].y2:=h;
                  stack1[j+1].y1:=h+1;
                  stack2[stacksize]:=stack1[j+1];
                  sorty(1,stacksize);
                  break;
                end;
          end;
      end
    else
      begin
        nbr:=0;
        for j:=1 to stacksize do
          begin
            kx1:=stack1[j].x1-1;
            ky1:=stack1[j].y1-1;
            kx2:=stack1[j].x2;
            ky2:=stack1[j].y2;
            sum:=b[ky2,kx2]-b[ky1,kx2]-b[ky2,kx1]+b[ky1,kx1];
            if (sum>=l) and (sum<=h) then inc(nbr);
          end;
        writeln(izlaz,nbr);
      end;
  end;
close(ulaz);
close(izlaz);
end.











