Under construction

Still being built. Things may change

Maze

Time limit2sMemory limit1024 MB

Summary
Given an R by C grid with black cells, find the fewest N by N stamps that turn black cells white so a white path joins start and goal.
Level

Medium7 of 10

Topics
Shortest path, BFS, Matrix
Solved
No attempts yet

Problem

President K loves solving mazes. He found a grid of cells from which he may create a maze. The grid has RR horizontal rows and CC vertical columns. Each cell is colored either white or black. The cell in the ii-th row (1≤i≤R1 \le i \le R) from the top and the jj-th column (1≤j≤C1 \le j \le C) from the left is called Cell (i,j)(i, j).

President K solves the maze under the condition that he can pass white cells but cannot pass black cells. He solves it in the following way.

  1. He chooses, among the white cells, the starting cell (Sr,Sc)(S_r, S_c) and the goal cell (Gr,Gc)(G_r, G_c).
  2. He can move from the current cell to an adjacent white cell in one of the four directions (up, down, left, or right). He repeats this until he finds a path from the starting cell to the goal cell.

The starting cell and the goal cell are already fixed. Depending on the colors, there may be no path from the starting cell to the goal cell made of white cells only. President K has a stamp of size N×NN \times N. He performs the following operation several times so that a path made of white cells only exists from the starting cell to the goal cell.

Operation: He chooses a square region of N×NN \times N cells and paints every cell in it white. Concretely, he chooses integers aa and bb with 1≤a≤R−N+11 \le a \le R - N + 1 and 1≤b≤C−N+11 \le b \le C - N + 1, and paints white every Cell (i,j)(i, j) with a≤i≤a+N−1a \le i \le a + N - 1 and b≤j≤b+N−1b \le j \le b + N - 1.

Using the stamp makes his hands dirty, so he wants as few operations as possible. Given the colors of the cells, the stamp size, and the positions of the starting and goal cells, write a program that computes the minimum number of operations needed to create a path of white cells from the starting cell to the goal cell.

Input

The first line contains RR, CC, and NN.

The second line contains SrS_r and ScS_c.

The third line contains GrG_r and GcG_c.

The next RR lines each contain a string AiA_i of length CC made of . and #. The jj-th character of AiA_i is . if Cell (i,j)(i, j) is white and # if it is black.

Output

Print one line containing the minimum number of operations President K has to perform so that a path of white cells from the starting cell to the goal cell exists.

Constraints

  • 1≤N≤R≤C1 \le N \le R \le C.
  • R×C≤6 000 000R \times C \le 6\,000\,000.
  • 1≤Sr≤R1 \le S_r \le R, 1≤Sc≤C1 \le S_c \le C, 1≤Gr≤R1 \le G_r \le R, 1≤Gc≤C1 \le G_c \le C.
  • (Sr,Sc)≠(Gr,Gc)(S_r, S_c) \ne (G_r, G_c).
  • AiA_i (1≤i≤R1 \le i \le R) is a string of length CC consisting of . or #.
  • Cell (Sr,Sc)(S_r, S_c) and Cell (Gr,Gc)(G_r, G_c) are white.
  • RR, CC, NN, SrS_r, ScS_c, GrG_r, GcG_c are integers.

Examples4

  1. Example 1

    Input
    2 4 2
    1 1
    2 4
    .###
    ###.
    
    Expected output
    1
    
  2. Example 2

    Input
    6 6 1
    1 6
    6 1
    ..#.#.
    ##.###
    ####.#
    ...###
    ##.##.
    .#.###
    
    Expected output
    4
    
  3. Example 3

    Input
    6 7 6
    6 4
    3 1
    ..#.#.#
    ##.##..
    .######
    #..#.#.
    .######
    ..#.##.
    
    Expected output
    1
    
  4. Example 4

    Input
    1 15 1
    1 15
    1 1
    ...............
    
    Expected output
    0