program ornamentas;

  const MAX = 20;
  type ornam = array [1..MAX, 1..MAX] of char;

  procedure i_kaire (n, k: integer; var o:ornam);
    var nau: ornam;
        e, s: integer;
  begin
    for e := 1 to n do
      for s := 1 to n do
        if s+k = n
           then nau[e, s] := o[e, n]
           else nau[e, s] := o[e, (s+k) mod n];
    o := nau;
  end; { _kairŠ }

  procedure i_virsu (n, v: integer; var o:ornam);
    var nau: ornam;
        e, s: integer;
  begin
    for e := 1 to n do
      for s := 1 to n do
        if e+v = n
           then nau[e, s] := o[n, s]
           else nau[e, s] := o[(e+v) mod n, s];
    o := nau;
  end; { _virž— }

  procedure keisti (n: integer; kom: string;
                    var o: ornam);
    var k, v, d, a: integer;
  begin
    { suskai‡iuojame kiek post–mi— reikia atlikti }
    k := 0; v := 0; d := 0; a := 0;
    while kom <> '' do
      begin
        case kom[1] of
          'K': k := k + 1;
          'D': d := d + 1;
          'V': v := v + 1;
          'A': a := a + 1;
        end;
        delete (kom, 1, 1);
      end;
    k := k mod n; d := d mod n; v := v mod n; a := a mod n;
    k := (n + k - d) mod n; v := (n + v - a) mod n;
    { atliekame post–mius }
    if k <> 0 then i_kaire (n, k, o);
    if v <> 0 then i_virsu (n, v, o);
  end; { keisti }

  procedure skaityti (var n: integer; var kom: string;
                      var o: ornam);
    var f: text;
        i, j: integer;
  begin
    assign (f, 'ORNAM.DAT');
    reset (f);
    readln (f, n);
    readln (f, kom);
    for i := 1 to n do
      begin
        for j := 1 to n do
          read (f, o[i, j]);
        readln (f);
      end;
    close (f);
  end; { skaityti }

  procedure rasyti (n: integer; var o: ornam);
    var f: text;
        i, j: integer;
  begin
    assign (f, 'ORNAM.REZ');
    rewrite (f);
    for i := 1 to n do
      begin
        for j := 1 to n do
          write (f, o[i, j]);
        writeln (f);
      end;
    close (f);
  end; { ražyti }

  var n: integer;
      kom: string;
      o: ornam;
begin
  skaityti (n, kom, o);
  keisti (n, kom, o);
  rasyti (n, o);
end.