Problem:
Score : 700 points

Problem Statement

Takahashi is locked within a building.

This building consists of H×W rooms, arranged in H rows and W columns.
We will denote the room at the i-th row and j-th column as (i,j). The state of this room is represented by a character A_{i,j}. If A_{i,j}= #, the room is locked and cannot be entered; if A_{i,j}= ., the room is not locked and can be freely entered.
Takahashi is currently at the room where A_{i,j}= S, which can also be freely entered.

Each room in the 1-st row, 1-st column, H-th row or W-th column, has an exit.
Each of the other rooms (i,j) is connected to four rooms: (i-1,j), (i+1,j), (i,j-1) and (i,j+1).

Takahashi will use his magic to get out of the building. In one cast, he can do the following:

Move to an adjacent room at most K times, possibly zero. Here, locked rooms cannot be entered.

Then, select and unlock at most K locked rooms, possibly zero. Those rooms will remain unlocked from then on.

His objective is to reach a room with an exit. Find the minimum necessary number of casts to do so.

It is guaranteed that Takahashi is initially at a room without an exit.

Constraints

3 ≤ H ≤ 800

3 ≤ W ≤ 800

1 ≤ K ≤ H×W

Each A_{i,j} is # , . or S.

There uniquely exists (i,j) such that A_{i,j}= S, and it satisfies 2 ≤ i ≤ H-1 and 2 ≤ j ≤ W-1.

Input

Input is given from Standard Input in the following format:

H W K
A_{1,1}A_{1,2}...A_{1,W}
:
A_{H,1}A_{H,2}...A_{H,W}

Output

Print the minimum necessary number of casts.

Sample Input 1

3 3 3
#.#
#S.
###

Sample Output 1

1

Takahashi can move to room (1,2) in one cast.

Sample Input 2

3 3 3
###
#S#
###

Sample Output 2

2

Takahashi cannot move in the first cast, but can unlock room (1,2).
Then, he can move to room (1,2) in the next cast, achieving the objective in two casts.

Sample Input 3

7 7 2
#######
#######
##...##
###S###
##.#.##
###.###
#######

Sample Output 3

2

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1 {
    class Program {
        static int h, w, k;
        static int sx, sy;
        static int min,ans;
        static string[] a;
        static int[,] map;
        static int INF = 114514;
        static int[] dx = new int[] { 1, 0, -1, 0 };
        static int[] dy = new int[] { 0, 1, 0, -1 };
        static void Main(string[] args) {
            //入力処理
            var inp = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
            h = inp[0];
            w = inp[1];
            k = inp[2];
            a = new string[h];
            for (int i = 0; i < h; i++) {
                a[i] = Console.ReadLine();
            }
            Pair<int, int> tp = SearchStart();
            sx = tp.First;
            sy = tp.Second;
            //初期化
            map = new int[h, w];
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    map[i, j] = INF;
                }
            }
            map[sx, sy] = 0;
            Queue<Pair<int, int>> que = new Queue<Pair<int, int>>();
            que.Enqueue(tp);
            //処理開始
            //初回移動可能地点を列挙
            List<Pair<int, int>> li = new List<Pair<int, int>>();
            li.Add(tp);
            while (que.Count > 0) {
                Pair<int, int> nowPair = que.Dequeue();
                for (int i = 0; i < 4; i++) {
                    int nx = nowPair.First + dx[i];
                    int ny = nowPair.Second + dy[i];
                    if (0 <= nx && nx < w && 0 <= ny && ny < h && map[nx, ny] == INF&&a[nx][ny]!='#') {
                        Pair<int, int> addPair = new Pair<int, int>(nx, ny);
                        li.Add(addPair);
                        map[nx, ny] = map[nowPair.First, nowPair.Second]+1;
                        que.Enqueue(addPair);
                    }
                }
            }
            //壁までの最短を求める
            min = h * w;
            for(int i = 0; i < li.Count; i++) {
                int minX = Math.Min(li[i].First, w - 1 - li[i].First);
                int minY = Math.Min(li[i].Second, h - 1 - li[i].Second);
                int minXY = Math.Min(minX, minY);
                if (min > minXY) {
                    min = minXY;
                    
                }
                //debug
                //Console.WriteLine("x:" + li[i].First + " y:" + li[i].Second + " min:" + minXY);
            }
            //結果表示
            if (min % k == 0) {
                ans = min / k+1;
            }
            else {
                ans = min / k + 2;
            }
            Console.WriteLine(ans);

        }

        static Pair<int, int> SearchStart() {
            Pair<int, int> start = new Pair<int, int>(-1, -1);
            bool flag = false;
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    if (a[i][j] == 'S') {
                        start.First = i;
                        start.Second = j;
                        flag = true;
                        break;
                    }
                }
                if (flag) break;
            }
            return start;
        }
    }
    public class Pair<T, U> {
        public Pair(T first, U second) {
            First = first;
            Second = second;
        }
        public T First { get; set; }
        public U Second { get; set; }
    }
}