Under construction

Still being built. Things may change

Flipping letters

Interview

Time limit1sMemory limit1024 MB

Summary
Given an N by N grid of d, b, q, and p, print the grid after flipping every letter vertically or horizontally.
Level

Easy1 of 10

Topics
Implementation, String
Solved
No attempts yet

Statement

The lowercase letters d, b, q, and p have a special property: flipping one vertically or horizontally turns it into one of d, b, q, or p. The rules are as follows.

  • Flipping d vertically gives q, and flipping it horizontally gives b.
  • Flipping b vertically gives p, and flipping it horizontally gives d.
  • Flipping q vertically gives d, and flipping it horizontally gives p.
  • Flipping p vertically gives b, and flipping it horizontally gives q.

Each cell of an NN by NN grid contains one of d, b, q, or p. Find the result of flipping every letter in the grid in the given direction.

Input

The first line contains two integers N(1≤N≤10)N(1\le N\le 10) and D(1≤D≤2)D(1\le D\le 2), separated by a space. D=1D=1 means flip vertically, and D=2D=2 means flip horizontally.

The next NN lines each contain a string of length NN. The jj-th character of the ii-th line is the letter in row ii, column jj of the grid. Each cell contains one of the lowercase letters d, b, q, or p.

Output

Print the grid after flipping every letter in it in the given direction.

Examples4

  1. Example 1

    Input
    2 1
    db
    qp
    
    Expected output
    qp
    db
    
  2. Example 2

    Input
    2 1
    qp
    db
    
    Expected output
    db
    qp
    
  3. Example 3

    Input
    3 2
    bdb
    dbd
    bdb
    
    Expected output
    dbd
    bdb
    dbd
    
  4. Example 4

    Input
    3 2
    ddd
    bbb
    ddd
    
    Expected output
    bbb
    ddd
    bbb