{
TASK:WAPPO
LANG:Pascal
}
var
   n,xg,yg,x1,y1,x2,y2,t : longint;
   a : array[1..6,1..6] of longint;
   tr : array[1..36] of record x,y : longint; end;
   ans : array[1..36] of byte;
   res1,res2,t1,t2 : longint;

procedure readfile;
var
   f : text;
   i,j : longint;
begin
   assign(f,'');
   reset(F);
   readln(f,n,xg,yg,x1,y1,x2,y2,t);
   for i:=1 to t do readln(f,tr[i].y,tr[i].x);
   for i:=1 to n do
      begin
         for j:=1 to n-1 do read(f,a[i,j]);
         readln(f,a[i,n]);
      end;
   close(f);
end;
function IsTrap(x,y : longint) : boolean;
var
   i : longint;
begin
   for i:=1 to t do if (tr[i].x=x)and(tr[i].y=y) then
      begin IsTrap:=true; exit; end;
   IsTrap:=false;
end;

procedure solve;
var
   ex : record x,y : longint; end;
   i,j,br : longint;
   ch : char;
begin
   br:=0;
   for i:=1 to n do if a[i,1] and 1 = 0 then
       begin
          ex.x:=1;
          ex.y:=i;
          ch:='2'
       end;
   for i:=1 to n do if a[i,n] and 2 = 0 then
       begin
          ex.x:=n;
          ex.y:=i;
          ch:='3'
       end;
   for i:=1 to n do if a[1,i] and 4 = 0 then
       begin
          ex.x:=i;
          ex.y:=1;
          ch:='0';
       end;
   for i:=1 to n do if a[n,i] and 8 = 0 then
       begin
          ex.x:=i;
          ex.y:=n;
          ch:='1';
       end;
   while (xg<>ex.x)or(yg<>ex.y) do
      if (IsTrap(xg-1,yg-1))and(a[xg,yg] and 1 = 0)and(not IsTrap(xg-1,yg)) then
         begin
            dec(xg);
            inc(br);ans[br]:=3;
         end
      else
      if (IsTrap(xg+1,yg-1))and(a[xg,yg] and 2 =0 )and(not IsTrap(xg+1,yg)) then
         begin
            inc(xg);
            inc(br);ans[br]:=2;
         end
      else
      if (IsTrap(xg-1,yg+1))and(a[xg,yg] and 1 =0 )and(not IsTrap(xg-1,yg)) then
         begin
            dec(xg);
            inc(br);ans[br]:=3;
         end
      else
      if (IsTrap(xg+1,yg+1))and(a[xg,yg] and 2 =0 )and(not IsTrap(xg+1,yg)) then
         begin
            inc(xg);
            inc(br);ans[br]:=2;
         end
      else
         begin
            if (ex.y<yg)and(a[xg,yg] and 4 = 0)and(not IsTrap(xg,yg-1)) then
                begin
                   dec(yg);
                   inc(br);ans[br]:=0;
                end
            else
            if (ex.y>yg)and(a[xg,yg] and 8 = 0)and(not IsTrap(xg,yg+1)) then
                begin
                   inc(yg);
                   inc(br);ans[br]:=1;
                end
            else
            if (a[xg,yg] and 2 =0 )and(not IsTrap(xg+1,yg)) then
               begin
                  inc(xg);
                  inc(br);ans[br]:=2;
               end
            else
            if (a[xg,yg] and 1 =0 )and(not IsTrap(xg-1,yg)) then
               begin
                  dec(xg);
                  inc(br);ans[br]:=3;
               end
      end;
   inc(br);
   ans[br]:=byte(ch)-byte('0');
   writeln(br);
   for i:=1 to br do writeln(ans[i]);
end;
begin
   readfile;
   solve;
end.
