Under construction

Still being built. Things may change

Paint bucket

Interview

Time limit2sMemory limit512 MB

Summary
Flood fill a grid from one pixel, repainting all side-connected pixels sharing the clicked color with a new color, then print the grid.
Level

Easy3 of 10

Topics
Graph, BFS, DFS, Matrix
Solved
No attempts yet

Problem

One of the biggest time savers when you draw on a computer is the paint bucket, also called bucket fill.

When you pick this tool and click a pixel of the image, every pixel that has the same color as the clicked pixel and is connected to it gets painted with the new color. Two pixels are connected when they share a side, or when a path of connected pixels joins them.

Look at the picture below. If you click the orange pixel in the center, the whole region turns orange. Pixels are not connected diagonally, so two corners of the image stay white.

You are given a matrix of digits that represents the pixels. Simulate a bucket fill applied to one given pixel. A color is a single digit from 0 to 9.

Input

The first line contains two integers RR and CC, the number of rows and the number of columns of the image.

Each of the next RR lines contains CC digits, the initial colors of the pixels.

The last line contains three integers YY, XX and KK. YY and XX are the row and the column of the pixel where the fill is applied, counted from 0, and KK is the color to paint with.

The image is smaller than 1000 x 1000 pixels.

Every color is a single digit from 0 to 9.

Output

Print the image after the fill, in the same format as the input: RR lines of CC digits each.

Examples2

  1. Example 1

    Input
    4 7
    0000000
    0111000
    0111010
    0000000
    0 0 3
    
    Expected output
    3333333
    3111333
    3111313
    3333333
    
  2. Example 2

    Input
    9 9
    000000000
    011101110
    011101110
    011011110
    000111000
    011110110
    011101110
    011101110
    000000000
    4 4 2
    
    Expected output
    000000000
    011102220
    011102220
    011022220
    000222000
    022220110
    022201110
    022201110
    000000000