Universal Cup Judging System

Universal Cup

時間限制: 1 s 記憶體限制: 1024 MB 總分: 100
统计

You are now piloting a UFO in an $n \times n$ grid formed by points $(x,y)$ where $1 \le x,y \le n$. Some points are impassable (*) and others are passable (.).

Initially, you are at point $(1,1)$, and you aim to reach $(n,n)$ as quickly as possible. When you are at point $(x,y)$, you can teleport to $(x+1,y)$, $(x,y+1)$, $(x-1,y)$, $(x,y-1)$, or $f^i(x,y)$ for any non-negative integer $i \le k$ in one second. The function $f^i(x,y)$ is defined as:

$$ f^i(x,y)=\begin{cases}(x,y)&(i=0),\\f^{i-1}(y+1,x)&(i>0).\end{cases} $$

You cannot teleport if the target location is outside the grid or if the target location is impassable.

Find the minimum time required to reach $(n,n)$. If you can never reach $(n,n)$, print -1.

Input

The first line of the input contains two integers $n$ and $k$ ($1 \le n,k \le 5000$).

Each of the next $n$ lines contains $n$ characters, representing the grid.

It is guranteed that points $(1,1)$ and $(n,n)$ are passable.

Output

One integer in a line representing the minimum time to reach $(n,n)$, or -1 if it is unreachable.

Examples

Input 1

3 2
.*.
.*.
...

Output 1

3

Input 2

3 3
.*.
.*.
...

Output 2

2

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#157EditorialOpen题解jianglyView

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.