{
TASK:wappo
LANG:pascal
}


type mon = record
            cap : boolean;
            wait : longint;
            r, c : longint;
           end;


var used, a, b : array[0..7, 0..7] of longint;

    out : array[0..100] of longint;
    i, j, k, l, o, p, m, n : longint;
    fl, vr, t, r, r1, r2, c, c1, c2 : longint;
    won : boolean;
    mon1, mon2, mon3 : mon;
    hero_i, hero_j : longint;

label 1, 2, 3, 4;


procedure init;
 begin
  readln(n, r, c, r1, c1, r2, c2, t);
  for i := 1 to t do
    begin
     readln(k, l);
     b[k, l] := 1;
    end;
  for i := 1 to n do
    begin
     for j := 1 to n do
       read(a[i, j]);
     readln;
    end;
 end;

function check(i, j, k : longint) : boolean;
 begin
  if k = 4 then
    begin
     if (a[i, j] and 4 = 4) or (a[i - 1, j] and 8 = 8) then check := false
     else check := true;
    end;
  if k = 8 then
    begin
     if (a[i, j] and 8 = 8) or (a[i + 1, j] and 4 = 4) then check := false
     else check := true;
    end;
  if k = 2 then
    begin
     if (a[i, j] and 2 = 2) or (a[i, j + 1] and 1 = 1) then check := false
     else check := true;
    end;
  if k = 1 then
    begin
     if (a[i, j] and 1 = 1) or (a[i, j - 1] and 2 = 2) then check := false
     else check := true;
    end;
 end;





procedure move_mon_1(var mon1 : mon; tt : longint);
var k, l : longint;
  begin
   if mon1.wait > 0 then
     begin
      dec(mon1.wait);
      mon1.cap := false;
     end
   else
     begin
      mon1.cap := true;
      if hero_j > mon1.c then k := 2
                         else k := 1;
      if hero_i > mon1.r then l := 4
                         else l := 8;
      if (mon1.c <> hero_j) and (check(mon1.r, mon1.c, k)) then
        begin
         if k = 2 then inc(mon1.c)
                  else dec(mon1.c);
         if b[mon1.r, mon1.c] = 1 then
           begin
            if tt = 2 then mon1.wait := 6
                      else mon1.wait := 5;

           end;
        end
      else if (mon1.r <> hero_i) and (check(mon1.r, mon1.c, l)) then
        begin
         if l = 4 then inc(mon1.r)
                  else dec(mon1.r);
         if b[mon1.r, mon1.c] = 1 then
           begin
            if tt = 2 then mon1.wait := 6
                      else mon1.wait := 5;

           end;
        end
     end;
 end;


procedure move_mon_2(var mon2 : mon; tt : longint);
var k, l : longint;
  begin
   if mon2.wait > 0 then
     begin
      dec(mon2.wait);
      mon2.cap := false;
     end
   else
     begin
      mon2.cap := true;
      if hero_j > mon2.c then k := 2
                         else k := 1;
      if hero_i > mon2.r then l := 4
                         else l := 8;
      if (mon2.c <> hero_j) and (check(mon2.r, mon2.c, k)) then
        begin
         if k = 2 then inc(mon2.c)
                  else dec(mon2.c);
         if b[mon2.r, mon2.c] = 1 then
           begin
            if tt = 2 then mon2.wait := 6
                      else mon2.wait := 5;
           end;
        end
      else if (mon2.r <> hero_i) and (check(mon2.r, mon2.c, l)) then
        begin
         if l = 4 then inc(mon2.r)
                  else dec(mon2.r);
         if b[mon2.r, mon2.c] = 1 then
           begin
            if tt = 2 then mon2.wait := 6
                      else mon2.wait := 5;

           end;
        end
     end;
 end;


procedure move_pow_mon(var mon3 : mon);
var k, l : longint;
  begin
      mon3.cap := true;
      if hero_j > mon3.c then k := 2
                         else k := 1;
      if hero_i > mon3.r then l := 4
                         else l := 8;
      if (mon3.c <> hero_j) and (check(mon3.r, mon3.c, k)) then
        begin
         if k = 2 then inc(mon3.c)
                  else dec(mon3.c);
        end
      else if (mon3.r <> hero_i) and (check(mon3.r, mon3.c, l)) then
        begin
         if l = 4 then inc(mon3.r)
                  else dec(mon3.r);
        end
 end;


procedure hero(i, j, vr, who : longint; mon1, mon2, mon3 : mon; power : boolean);
 begin
  hero_i := i;
  hero_j := j;
  if ((mon1.r = i) and (mon1.c = j)) or ((mon2.r = i) and (mon2.c = j)) or ((mon3.r = i) and (mon3.c = j)) then exit;
  if (i < 1) or (j < 1) or (i > n) or (j > n) then
    begin
     won := true;
     m := vr;
     exit;
    end;
  if (won = false) then
    begin
     if who = 1 then
       begin

        if power = true then move_pow_mon(mon3);
        if ((mon1.r = i) and (mon1.c = j)) or ((mon2.r = i) and (mon2.c = j)) or ((mon3.r = i) and (mon3.c = j)) then exit;
        if power = true then move_pow_mon(mon3);
        if ((mon1.r = i) and (mon1.c = j)) or ((mon2.r = i) and (mon2.c = j)) or ((mon3.r = i) and (mon3.c = j)) then exit;
        if power = true then move_pow_mon(mon3);
        if ((mon1.r = i) and (mon1.c = j)) or ((mon2.r = i) and (mon2.c = j)) or ((mon3.r = i) and (mon3.c = j)) then exit;

        if power = false then move_mon_1(mon1, 1);
        if power = false then move_mon_2(mon2, 1);
        if (mon1.r = mon2.r) and (mon1.c = mon2.c) then
          begin
           mon3 := mon2;
           power := true;
          end;
        if ((mon1.r = i) and (mon1.c = j)) or ((mon2.r = i) and (mon2.c = j)) or ((mon3.r = i) and (mon3.c = j)) then exit;
        if power = false then move_mon_1(mon1, 2);
        if power = false then move_mon_2(mon2, 2);
        if ((mon1.r = i) and (mon1.c = j)) or ((mon2.r = i) and (mon2.c = j)) or ((mon3.r = i) and (mon3.c = j)) then exit;
        if (mon1.r = mon2.r) and (mon1.c = mon2.c) then
          begin
           power := true;
           mon3 := mon2;
          end;
        hero(i, j, vr, 0, mon1, mon2, mon3, power);
       end
     else
     begin
     if (used[i, j + 1] = 0) and (b[i, j + 1] = 0) and (a[i, j] and 2 <> 2) and (a[i, j + 1] and 1 <> 1) then
       begin
        out[vr] := 3;
        used[i, j + 1] := 1;
        hero(i, j + 1, vr + 1, 1, mon1, mon2, mon3, power);
        if won = true then exit;
        used[i, j + 1] := 0;
       end;

     if (used[i - 1, j] = 0) and (b[i - 1, j] = 0) and ((a[i, j] and 4) <> 4) and ((a[i - 1, j] and 8) <> 8) then
       begin
           out[vr] := 0;
           used[i - 1, j] := 1;
           hero(i - 1, j, vr + 1, 1, mon1, mon2, mon3, power);
           if won = true then exit;
           used[i - 1, j] := 0;
       end;
     if (used[i, j - 1] = 0) and (b[i, j - 1] = 0) and (a[i, j] and 1 <> 1) and (a[i, j - 1] and 2 <> 2) then
       begin
        out[vr] := 2;
        used[i, j - 1] := 1;
        hero(i, j - 1, vr + 1, 1, mon1, mon2, mon3, power);
        if won = true then exit;
        used[i, j - 1] := 0;
        end;

     if (used[i + 1, j] = 0) and (b[i + 1, j] = 0) and (a[i, j] and 8 <> 8) and (a[i + 1, j] and 4 <> 4) then
       begin
        out[vr] := 1;
        used[i + 1, j] := 1;
        hero(i + 1, j, vr + 1, 1, mon1, mon2, mon3, power);
        if won = true then exit;
        used[i + 1, j] := 0;
       end;
      end;
   end;
 end;






begin
 init;
 won := false;
 mon1.cap := true;
 mon1.wait := 0;
 mon1.r := r1;
 mon1.c := c1;
 mon2.cap := true;
 mon2.wait := 0;
 mon2.r := r2;
 mon2.c := c2;
 fl := 0;
 used[r, c] := 1;
 hero(r, c, 1, 0, mon1, mon2, mon3, false);
 writeln(m - 1);
 for i := 1 to m - 1 do
   writeln(out[i]);
end.
