{ Sprendim… bei testus pareng‚ Justas Kranmauskas.
  ’aidimas su labirintu.
  1) labirinto sukiojim… reikia tiesiog imituoti rutuliuko jud‚jimo krypties
     keitimu;
  2) kadangi maksimalus labirintas 1000x1000 netilps Ť dinaminŠ atmintŤ, jei
     kiekvien… labirinto laukelŤ ‘ym‚sime baitu, o laukelis gali tur‚ti tik
     dvi skirtingas reikžmes (siena/pra‚jimas), laukelio reikžmŠ reikia
     saugoti vienu bitu;
}
{$A+,B-,D+,E-,F+,G-,I+,L+,N+,O+,P+,Q+,R+,S+,T-,V+,X+,Y+}
{$M 16384,0,655360}
program labirintas;
  const maxN = 1000;
        n8 = 125; {1000 div 8}
        maxK = 5000;
        inName = 'labir.dat';
        outName = 'labir.rez';
        bitai : array [0..7] of byte = (1, 2, 4, 8, 16, 32, 64, 128);
        kryptys : array [0..3, 0..1] of shortint =
          ((0, 1) {desinen}, (1, 0)  {virsun},
          (0, -1)  {kairen}, (-1, 0) {apacion});
  type bituEilute = array [0..n8 - 1] of byte;
       plokste = array [0..maxN - 1] of ^bituEilute; {labirintas}
       komandos = array [0..maxK - 1] of boolean;    {komand— seka}
  var k : komandos;
      pl : plokste;
      fin, fout : text;
      n, x, y, kiekK, newX, newY : integer;
      c : char;
      i, ii : integer;
      kryptis : byte;
  procedure padekBita (var bitEil : bituEilute; vieta : integer);
  {nurodytam labirinto laukeliui priskiria reikžmŠ - siena}
  begin
    bitEil[vieta div 8] := bitEil[vieta div 8] or bitai[vieta mod 8];
  end;
  function testBit (x, y : integer) : boolean;
  {patikrina, ar nurodytame labirinto laukelyje siena, ar pra‚jimas}
  begin
    x := n - x - 1;
    if pl[x]^[y div 8] and bitai[y mod 8] > 0
      then testBit := true
      else testBit := false;
  end;
  function ploksteleje (x, y : integer) : boolean;
  {patikrina, ar koordinat‚s neižeina iž labirinto rib—}
  begin
    ploksteleje := (x >= 0) and (y >= 0) and (x < n) and (y < n);
  end;
begin
  assign (fin, inName);
  reset (fin);
  {nuskaitome plokžtel‚s dydŤ ir rutuliuko koordinates}
  readln (fin, n, x, y);
  x := x - 1;
  y := y - 1;
  {nuskaitome komand— sek…}
  kiekK := -1;
  while not eoln (fin) do
    begin
      read (fin, c);
      kiekK := kiekK + 1;
      if upcase (c) = 'K'
        then k[kiekK] := true
        else k[kiekK] := false;
    end;
  {nuskaitome plokžtel‚s labirint…}
  for i := 0 to n - 1 do
    begin
      new (pl[i]);
      fillchar (pl[i]^, sizeof (pl[i]^), 0);
      readln (fin);
      for ii := 0 to n - 1 do
        begin
          read (fin, c);
          if c = '1'
            then padekBita(pl[i]^, ii);
        end;
    end;
  close (fin);
  {sukiojame labirint…, tiksliau kaitaliojame rutuliuko kritimo kryptŤ}
  kryptis := 3; {apacion}
  for i := 0 to kiekK do
    begin
      if not k[i] {plokstele sukama desinen}
        then kryptis := (kryptis + 1) mod 4
        else if kryptis > 0
          then kryptis := kryptis - 1
          else kryptis := 3;
      newX := x + kryptys[kryptis, 0];
      newY := y + kryptys[kryptis, 1];
      while ploksteleje (newX, newY) and not testBit (newX, newY) do
      {kol rutuliukas plokžtel‚s viduje ir po juo nieko n‚ra, jis krenta
      nurodyta kryptimi}
        begin
          x := newX;
          y := newY;
          newX := x + kryptys[kryptis, 0];
          newY := y + kryptys[kryptis, 1];
        end;
    end;
  {ižvedame rezultatus}
  assign (fout, outName);
  rewrite (fout);
  write (fout, x + 1, ' ', y + 1);
  close (fout);
  {atlaisviname dinaminŠ atmintŤ}
  for i := 0 to n - 1 do
    dispose (pl[i]);
end.