{
TASK: KAL
LANG: PASCAL
}
program kaladeles;

  const PRF = 'KAL.DAT';
        RZF = 'KAL.REZ';
        MAXL = 100; { maksimalus lentos dydis }
        MAX_EJ = 10000;
  type Tlenta = array [1..MAXL, 1..MAXL] of integer;
       Tejimai = array [1..MAX_EJ] of integer;
       Tlang = record
                 e, s: integer;
               end;
  { globalieji }
  var N, M: integer;  { lentos dydis }
      l: Tlenta;
      k1sk, k2sk: integer; { skaièiai ant duotøjø kaladëliø }


  procedure stumti (e1, e2, s1, s2: integer;
                   { staëiakamp lentos dalis, kurioje stumdomos kaladëlës }
                    var nul, kal1: Tlang; { kur po perstûmimo atsidûrë ðios kalad }
                    var l: Tlenta; { lenta }
                    var K: integer; var ejim: Tejimai);
  { "pastumiama" tuscia kaladele }
  { stumiama ten kur galima prieð laikrodþio rodyklæ }
     var nl: Tlang;
  begin
    nl := nul;
    if (nl.s = s1) and (nl.e < e2)
       then nl.e := nl.e + 1  { zemyn }
    else if (nl.e = e2) and (nl.s < s2) { deðinën }
            then nl.s := nl.s + 1
    else if (nl.s = s2) and (nl.e > e1) { aukðtyn }
            then nl.e := nl.e - 1
    else nl.s := nl.s - 1; { kairen }
    { atnaujinama ëjimø seka ir situacija lentoje }
    K := K + 1;
    ejim[K] := l[nl.e, nl.s];
    { gal pasikeitë pirmosios kaladëlës padëtis }
    if ejim[K] = k1sk
       then kal1 := nul;
    l[nul.e, nul.s] := l[nl.e, nl.s];
    l[nl.e, nl.s] := 0;
    nul := nl;
  end; { stumti }

  function min (x, y: integer): integer;
  begin
    if x < y then min := x
             else min := y;
  end; { min }

  function max (x, y: integer): integer;
  begin
    if x > y then max := x
             else max := y;
  end; { max }

  procedure spresti (nul, kal1, kal2: Tlang;
                     var K: integer; var ejim: Tejimai);
    var e1, e2, s1, s2: integer;
  begin
    K := 0;
    { tuðèia kaladëlë nustumiama á tà paèià eilutæ (ar stulpelá)
      kuriame yra pirmoji arba antroji kaladëlës
      stumdoma visoje lentoje }
    while (nul.e <> kal1.e) and (nul.e <> kal2.e) and
          (nul.s <> kal1.s) and (nul.s <> kal2.s) do
      stumti (1, 1, N, M, nul, kal1, l, K, ejim);
   { antros kaladëlës koordinaèiø neatnaujiname, nes svarbu ne kur ji
    yra dabar, o kur ji buvo pradiniu momentu }
   { gal atsitiktinai radome sprendiná }
   if (kal1.e = kal2.e) and (kal1.s = kal2.s)
       then exit;
   { randamos lentos dalies, kurioje bus stumdoma, koordinates }
   e1 := min (min (nul.e, kal1.e), kal2.e);
   e2 := max (max (nul.e, kal1.e), kal2.e);
   s1 := min (min (nul.s, kal1.s), kal2.s);
   s2 := max (max (nul.s, kal1.s), kal2.s);
   if e1 = e2 then if e2 < N
                      then e2 := e2 + 1
                      else e1 := e1 - 1;
   if s1 = s2 then if s2 < M
                      then s2 := s2 + 1
                      else s1 := s1 - 1;
   while (kal1.e <> kal2.e) or (kal1.s <> kal2.s) do
      stumti (e1, e2, s1, s2, nul, kal1, l, K, ejim);
  end; { spræsti }

  procedure skaityti (var N, M, ka1, ka2: integer; var l: Tlenta;
                      var nul, kal1, kal2: Tlang);
    var f: text;
        i, j: integer;
  begin
    assign (f, PRF);
    reset (f);
    readln (f, N, M);
    readln (f, ka1, ka2);
    for i := 1 to N do
      for j := 1 to M do
        begin
           read (f, l[i, j]);
           if l[i, j] = 0
               then begin
                      nul.e := i;
                      nul.s := j;
                    end
           else if l[i, j] = ka1
               then begin
                      kal1.e := i;
                      kal1.s := j;
                    end
           else if l[i, j] = ka2
               then begin
                      kal2.e := i;
                      kal2.s := j;
                    end
        end;
    close (f);
  end; { skaityti }

  procedure rasyti (K: integer; var ejim: Tejimai);
    var f: text;
        i: integer;
  begin
    assign (f, RZF);
    rewrite (f);
    writeln (f, K);
    for i := 1 to K do
      writeln (f, ejim[i]);
    close (f);
  end; { raðyti }

  var K: integer;
      nul, kal1, kal2: Tlang;
      ejim: Tejimai;
begin
  skaityti (N, M, k1sk, k2sk, l, nul, kal1, kal2);
  spresti (nul, kal1, kal2, K, ejim);
  rasyti (K, ejim);
end.
