Under construction

Still being built. Things may change

Elegant Showroom

Interview

Time limit2sMemory limit512 MB

Summary
A car on a grid needs a path to a border door; every car cell on the path must be moved, so find the path with the fewest car cells.
Level

Medium6 of 10

Topics
Graph, Shortest path, Heap, BFS
Solved
No attempts yet

Problem

A car showroom keeps its cars packed indoors. To take one car out, every car standing in its way has to be moved somewhere else first.

The showroom is given as an R×CR \times C grid. Each cell holds a wall, a car, a door, or empty floor. A car moves only to an orthogonally adjacent empty cell and never diagonally. A car turns in place, so the direction it faces does not matter. A door is wide enough for a car, so a car drives straight through a cell that holds a door.

The first row, the last row, the first column and the last column of the grid hold only walls and doors. A car that reaches a door on this border leaves the building.

You are given the position of one car. Once you fix a route for that car, every car sitting on the route has to be moved somewhere else first. The number of cars you move is the number of cars on the route plus the car you are taking out. Find the smallest such number.

Input

The first line contains two integers RR and CC, the number of rows and columns of the showroom. (3≤R,C≤4003 \le R, C \le 400)

Each of the next RR lines contains a string of length CC. The characters mean:

  • #: a wall
  • c: a car
  • D: a door
  • .: empty floor

Every character of the first row and of the last row, and the first and last character of every row, is # or D.

The last line contains two integers rr and cc, the position of the car to take out. (1<r<R1 < r < R, 1<c<C1 < c < C) The top left cell has coordinates 1 1, rr is the row number and cc is the column number. Row rr, column cc holds a car.

At least one route out of the building exists for that car.

Output

Print the smallest number of cars that are moved for the chosen car to leave the building, counting that car itself.

Examples2

  1. Example 1

    Input
    4 5
    #####
    #cDc#
    #c#cD
    #####
    3 2
    
    Expected output
    4
    
  2. Example 2

    Input
    10 10
    ##########
    #cc#ccccc#
    #cc#cccccD
    #cccccccc#
    ########c#
    #cccccccc#
    ###cccccc#
    #c#cccccc#
    #cccccccc#
    ##########
    2 2
    
    Expected output
    11