Under construction

Still being built. Things may change

More Common Tile Coloring

Interview

Time limit2sMemory limit1024 MB

Summary
Given an N by M letter grid, find the fewest cells to repaint so every K by K block matches one shared K by K pattern, and print the result.
Level

Medium4 of 10

Topics
Array, Implementation, Greedy
Solved
No attempts yet

Problem

An N×MN\times M tile is given. The cell at row ii, column jj is initially painted with color d_i,jd\_{i,j}. Each color is a single uppercase English letter.

Split the tile into non-overlapping K×KK\times K small tiles. Repaint some cells so that every small tile has the same color layout. The color used to repaint any cell must also be a single uppercase English letter.

Find the minimum number of cells that must be repainted, and print any one tile layout that satisfies this.

Input

The first line contains three integers NN, MM, and KK separated by spaces. (1≤N,M,K≤500;(1\le N,M,K\le 500; NN and MM are multiples of KK.))

The next NN lines contain a string d_id\_i of length MM, which is row ii of the tile colors. d_id\_i consists only of uppercase English letters.

Output

The first line prints the minimum number of cells that must be repainted.

The next NN lines print the repainted tile, where line ii is a string of length MM for row ii. The output strings also consist only of uppercase English letters.

Examples1

  1. Example 1

    Input
    4 6 2
    ABCBAB
    BBACCA
    BPAZBB
    BBAABB
    
    Expected output
    11
    ABABAB
    BBBBBB
    ABABAB
    BBBBBB