Under construction

Still being built. Things may change

Rotate

Time limit2sMemory limit512 MB

Summary
Given an N by N letter board and Q square regions, print the board after rotating each region 90 degrees counterclockwise in order.
Level

Medium5 of 10

Topics
Matrix, Simulation, Array
Solved
No attempts yet

Problem

There is a board of N×NN \times N cells arranged in NN rows and NN columns. The cell in the ii-th row from the top and the jj-th column from the left is called cell (i,j)(i, j). Cell (i,j)(i, j) contains the letter AijA_{ij}.

Rotate a square region 90 degrees counterclockwise, repeating this operation QQ times. The kk-th rotation takes the Sk×SkS_k \times S_k square region whose top-left cell is (Ik,Jk)(I_k, J_k) and rotates it.

Find the final board.

Input

The first line contains integers NN and QQ, separated by a space. NN is the board size and QQ is the number of rotations.

The next NN lines give the initial board. The jj-th character of the ii-th line is AijA_{ij}. Each string contains only lowercase English letters.

The next QQ lines give the rotations. The kk-th line contains integers IkI_k, JkJ_k, and SkS_k, separated by spaces. This means the kk-th rotation takes the Sk×SkS_k \times S_k square region whose top-left cell is (Ik,Jk)(I_k, J_k) and rotates it. Here 1≤Ik≤N−Sk+11 \le I_k \le N - S_k + 1, 1≤Jk≤N−Sk+11 \le J_k \le N - S_k + 1, and 2≤Sk≤N2 \le S_k \le N.

Output

Print the final board in NN lines. The ii-th line is a string of NN characters, whose jj-th character is the letter in cell (i,j)(i, j) of the final board.

Constraints

  • 2≤N≤10002 \le N \le 1000 (board size)
  • 1≤Q≤20001 \le Q \le 2000 (number of rotations)

Examples1

  1. Example 1

    Input
    4 1
    abcd
    efgh
    ijkl
    mnop
    2 2 2
    
    Expected output
    abcd
    egkh
    ifjl
    mnop