program Mrvice;
type
   Upit = record
      t : Longint;
      a : Int64;
      b : Int64;
   end;
var
   M : Array[0..1000,0..1000] of Longint;
   V : Array[0..1000,0..1000] of Boolean;
   Qx,Qy : Array[1..1000000] of Longint;
   Upiti : Array[1..100000] of Upit;
   N,U,i,j : Longint;

   procedure LoadInput;                        //
   var
      i,j : Longint;
      F : Text;
   begin
      Assign(F,'mrvice.in');
      Reset(F);
      Readln(F,N,U);
      for i:=0 to 2*N do begin
         M[0,i]:=-1;
         M[2*N,i]:=-1;
         M[i,0]:=-1;
         M[i,2*N]:=-1;
      end;
      for i:=1 to N do
         for j:=1 to N do Read(F,M[j*2-1,i*2-1]);
      for i:=1 to U do Readln(F,Upiti[i].t,Upiti[i].a,Upiti[i].b);
   end;

   procedure CutTheCake(Kako : Upit);
   var
      x,y : Longint;
   begin
               if Kako.a=0 then begin          //
         x:=2*Kako.b;
         y:=0;
         while (M[x,y]=-1)and(y<2*N) do Inc(y);
         if y<2*N then while (M[x,y]=0) do begin
            M[x,y]:=-1;
            Inc(y);
         end;
      end else if Kako.a=1 then begin          //
         x:=2*N;
         y:=2*Kako.b;
         while (M[x,y]=-1)and(x>0) do Dec(x);
         if x>0 then while (M[x,y]=0) do begin
            M[x,y]:=-1;
            Dec(x);
         end;
      end else if Kako.a=2 then begin          //
         x:=2*Kako.b;
         y:=2*N;
         while (M[x,y]=-1)and(y>0) do Dec(y);
         if y>0 then while (M[x,y]=0) do begin
            M[x,y]:=-1;
            Dec(y);
         end;
      end else if Kako.a=3 then begin          //
         x:=0;
         y:=2*Kako.b;
         while (M[x,y]=-1)and(x<2*N) do Inc(x);
         if x<2*N then while (M[x,y]=0) do begin
            M[x,y]:=-1;
            Inc(x);
         end;
      end;
   end;

   procedure ResetV;
   var
      i,j : Longint;
   begin
      for i:=0 to 2*N do
         for j:=0 to 2*N do V[i,j]:=False;
   end;

   function Upit2Bfs(Kako : Upit):Longint;
   var
      qstart,qend,newpts,mecez,i,j,qbr : Longint;
      Suma : Int64;
   begin
      mecez:=0;
      for i:=1 to N do
         for j:=1 to N do if not V[2*i-1,2*j-1] then begin
            qstart:=1;
            qend:=1;
            Qx[1]:=2*i-1;
            Qy[1]:=2*j-1;
            Suma:=M[Qx[1],Qy[1]];
            V[2*i-1,2*j-1]:=true;
            repeat
               newpts:=0;
               for qbr:=qstart to qend do begin
                  if (not V[Qx[qbr]+1,Qy[qbr]])and(M[qx[qbr]+1,qy[qbr]]>-1)
                  then begin                      //+1,0
                     Inc(newpts);
                     Qx[newpts+qend]:=Qx[qbr]+1;
                     Qy[newpts+qend]:=Qy[qbr];
                     V[Qx[qbr]+1,Qy[qbr]]:=true;
                     Suma:=Suma+M[qx[qbr]+1,qy[qbr]];
                  end;
                  if (not V[Qx[qbr]-1,Qy[qbr]])and(M[qx[qbr]-1,qy[qbr]]>-1)
                  then begin                      //-1,0
                     Inc(newpts);
                     Qx[newpts+qend]:=Qx[qbr]-1;
                     Qy[newpts+qend]:=Qy[qbr];
                     V[Qx[qbr]-1,Qy[qbr]]:=true;
                     Suma:=Suma+M[qx[qbr]-1,qy[qbr]];
                  end;
                  if (not V[Qx[qbr],Qy[qbr]+1])and(M[qx[qbr],qy[qbr]+1]>-1)
                  then begin                      //0,+1
                     Inc(newpts);
                     Qx[newpts+qend]:=Qx[qbr];
                     Qy[newpts+qend]:=Qy[qbr]+1;
                     V[Qx[qbr],Qy[qbr]+1]:=true;
                     Suma:=Suma+M[qx[qbr],qy[qbr]+1];
                  end;
                  if (not V[Qx[qbr],Qy[qbr]-1])and(M[qx[qbr],qy[qbr]-1]>-1)
                  then begin                      //0,-1
                     Inc(newpts);
                     Qx[newpts+qend]:=Qx[qbr];
                     Qy[newpts+qend]:=Qy[qbr]-1;
                     V[Qx[qbr],Qy[qbr]-1]:=true;
                     Suma:=Suma+M[qx[qbr],qy[qbr]-1];
                  end;
               end;
               qstart:=qend+1;
               qend:=qend+newpts;
            until newpts=0;
            if (Kako.a<=Suma)and(Suma<=Kako.b) then Inc(mecez);
         end;
      Upit2BFS:=Mecez;
   end;

   procedure SolveAndOutput;
   var
      F : Text;
      i,j : Longint;
   begin
      Assign(F,'mrvice.out');
      Rewrite(F);
      for i:=1 to U do begin
         if Upiti[i].t = 1 then CutTheCake(Upiti[i])
         else begin
            Writeln(F,Upit2BFS(Upiti[i]));
            ResetV;
         end;
      end;
      Close(F);
   end;
begin
   Writeln('Start');
   LoadInput;
   SolveAndOutput;
   Writeln('End');
end.


