program wp07zad1;

const
    MAX_X = 40;
    MAX_Y = 20;

var swiat : array [1 .. MAX_X, 1 .. MAX_Y] of Char;
    nowex, nowey, liczbaGeneracji, i, j, ile : Integer;
    wspx, wspy, dx, dy : array [1..MAX_X * MAX_Y] of Integer;

begin
    ile := 0;
    readln(liczbaGeneracji);
    for j := 1 to MAX_Y do begin
        for i := 1 to MAX_X do begin
            read(swiat[i, j]);
            if ('0' <= swiat[i, j]) and (swiat[i, j] <= '3') then begin
                ile := ile + 1;
                wspx[ile] := i;
                wspy[ile] := j;
                case swiat[i, j] of
                    '0' : begin
                        dx[ile] := 1;
                        dy[ile] := -1
                    end;
                    '1' : begin
                        dx[ile] := 1;
                        dy[ile] := 1
                    end;
                    '2' : begin
                        dx[ile] := -1;
                        dy[ile] := 1
                    end;
                    '3' : begin
                        dx[ile] := -1;
                        dy[ile] := -1
                    end
                end;
                swiat[i, j] := '*'
            end
        end;
        readln
    end;
    for liczbaGeneracji := liczbaGeneracji downto 1 do begin
        for j := 1 to MAX_Y do begin
            for i := 1 to MAX_X do begin
                write(swiat[i, j]);
                if swiat[i, j] = '*' then
                    swiat[i, j] := ' '
            end;
            writeln
        end;
        i:=1;
        while i <= ile do begin
            nowex := wspx[i] + dx[i];
            nowey := wspy[i] + dy[i];
            if (nowex < 1) or (nowex > MAX_X) or (nowey < 1) or (nowey > MAX_Y) then begin
                wspx[i] := wspx[ile];
                wspy[i] := wspy[ile];
                dx[i] := dx[ile];
                dy[i] := dy[ile];
                ile := ile - 1
            end else begin
                case swiat[nowex, nowey] of
                    '+': begin
                        dx[i] := -dx[i];
                        dy[i] := -dy[i]
                    end;
                    '-':
                        dy[i] := -dy[i];
                    '|':
                        dx[i] := -dx[i];
                    ' ', '*': begin
                        wspx[i] := nowex;
                        wspy[i] := nowey
                    end
                end;
                swiat[wspx[i], wspy[i]] := '*';
                i := i + 1
            end
        end
    end
end.
