PivotOJ

Maze

시간 제한: 2000ms메모리 제한: 1024MB출처: JOI 2022-2023 본선BOJ 27538

문제

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

President K will solve the maze under the condition that he can pass the white cells, but he cannot pass the black cells. More precisely, he will solve the maze in the following way.

  1. Among the white cells, he will choose Cell (Sr,Sc)(S_r, S_c), which is the starting cell of the maze, and Cell (Gr,Gc)(G_r, G_c), which is the goal cell of the maze.
  2. It is possible to move from one cell to another white cell which is adjacent to the current cell in one of the four directions (top, bottom, left, or right). By repeating this, he will find a path from the starting cell to the goal cell.

President K already fixed the starting cell and the goal cell. However, he noticed that in some situations of the colors of the cells, there might not be a path from the starting cell to the goal cell consisting of white cells only. He has a stamp of size N×NN \times N. He will perform the following Operations several times so that there will be a path from the starting cell to the goal cell consisting of white cells only.

Operation He chooses a square region of N×NN \times N cells, and paint the cells in the region white. More precisely, he chooses integers aa, bb satisfying 1 ≤ a ≤ R - N + 1 and 1 ≤ b ≤ C - N + 1, and for every pair (i,j)(i, j) of integers satisfying a ≤ i ≤ a + N - 1 and b ≤ j ≤ b + N - 1, he paints Cell (i,j)(i, j) white.

Since his hands becomes dirty if he uses the stamp, he wants to minimize the number of operations. Given information of the colors of the cells, the size of the stamp, and the locations of the starting cell and the goal cell, write a program which calculates the minimum number of operations he has to perform so that there will be a path from the starting cell to the goal cell consisting of white cells only.

입력

Read the following data from the standard input.

RR CC NN

SrS_r ScS_c

GrG_r GcG_c

A1A_1

A2A_2

\vdots

ARA_R

AiA_i (1 ≤ i ≤ R) is a string of length CC consisting of . or #. The jj-th character (1 ≤ j ≤ C) of AiA_i represents the color of Cell (i,j)(i, j). Its color is white if the character is ., and its color is black if the character is #.

출력

Write one line to the standard output. The output should contain the minimum number of operations President K has to perform so that there will be a path from the starting cell to the goal cell consisting of white cells only.

예제

예제 1

입력
2 4 2
1 1
2 4
.###
###.
출력
1

예제 2

입력
6 6 1
1 6
6 1
..#.#.
##.###
####.#
...###
##.##.
.#.###
출력
4

예제 3

입력
6 7 6
6 4
3 1
..#.#.#
##.##..
.######
#..#.#.
.######
..#.##.
출력
1

예제 4

입력
1 15 1
1 15
1 1
...............
출력
0
코드를 제출하려면 로그인하세요.