This page is still under construction.

Parts of this page are still being built. What you see may change.

Flowerbed Redecoration

Time limit3sMemory limit1024 MB

Summary
Given a grid and a sliding d by d window that rotates 90 degrees clockwise at each stop, output the grid after the full scan order is applied.
Level

Hard8 of 10

Topics
Simulation, Matrix, Implementation
Solved
No attempts yet

Problem

Joon-Pyo decorated a flowerbed in front of his home. The flowerbed is an n×mn \times m grid, and one flower is planted in each cell. There are 26 colors, one for each uppercase letter from A to Z. Suddenly, he wanted to redecorate the flowerbed.

The flowerbed is too large to adjust the flowers one by one. He rented equipment that can lift and rotate a square plot of land with side length dd. He planned the construction in the following order, expecting the flowerbed to be properly redecorated.

  1. Place the equipment so that exactly the flowers in the first dd rows and the first dd columns are inside.
  2. Rotate the d×dd \times d square inside the equipment 90∘90^\circ clockwise. If this square contains flowers from the last dd rows and the last dd columns, the construction is finished. Otherwise, if this square does not contain flowers in the last dd columns, move the equipment xx squares to the right. Otherwise, move the equipment down by yy squares and all the way to the left so it contains flowers from the first dd columns.
  3. Repeat step 2 until construction is finished.

The equipment never goes out of the flowerbed, since xx, yy, and dd are carefully determined before construction begins.

He cannot start construction without knowing the outcome. Write a program that outputs the result.

Input

The first line contains five integers nn, mm, yy, xx, and dd (1≤n×m≤1061 \leq n \times m \leq 10^6, 1≤y≤n1 \leq y \leq n, 1≤x≤m1 \leq x \leq m, 1≤d≤min⁡(n,m)1 \leq d \leq \min(n, m), n≡d(mody)n \equiv d \pmod y, m≡d(modx)m \equiv d \pmod x). Each of the next nn lines contains exactly mm uppercase letters, which is the current flowerbed.

Output

Output nn lines, each containing exactly mm uppercase letters, the flowerbed after the planned construction.

Hint

In the first example, the flowerbed changes as follows:

Examples2

  1. Example 1

    Input
    4 4 1 1 2
    AAAA
    BBBB
    AAAA
    BBBB
    
    Expected output
    BAAA
    ABBB
    BAAA
    BBBA
    
  2. Example 2

    Input
    6 5 1 2 3
    RBRCY
    YBPBR
    PBRCY
    CYPBR
    PBRCY
    CYPBR
    
    Expected output
    PYRBR
    CRCBB
    PPBPY
    CRCYB
    YRBCY
    PYRBR