{$q+,r+,o-}
uses
    sysutils;
const
    max_n = 10;

var
    n, m, i, j: longint;
    a, b: array [1..max_n, 1..max_n] of char;

procedure t();
var
    i, j, t: longint;
begin
    for i := 1 to n do begin
        for j := 1 to m do begin
            b[j][i] := a[i][j];
        end;
    end;
    a := b;
    t := n; n := m; m := t;
end;

procedure f();
var
    i, j: longint;
begin
    for i := 1 to n do begin
        for j := 1 to m do begin
            b[n - i + 1][j] := a[i][j];
        end;
    end;
    a := b;
end;

var
    z: longint;
    w, l: longint;
begin
    w := strToInt(paramstr(1));
    l := strToInt(paramstr(2));
    for z := 1 to 2 do begin
        readln(n, m);

        for i := 1 to n do begin
            for j := 1 to m do begin
                read(a[i][j]);
            end;
            readln;
        end;

        if z = w then begin
            for i := 1 to l do begin
                if (i mod 2 = 0) then begin
                    f();
                end else begin
                    t();
                end;
            end;
        end;

        writeln(n, ' ', m);

        for i := 1 to n do begin
            for j := 1 to m do begin
                write(a[i][j]);
            end;
            writeln;
        end;
    end;

    close(input);
    close(output);
end.