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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <queue>
#include <iostream>
#include <string.h>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <cstdint>
#include <climits>
#include <unordered_set>
#include <sstream>

using namespace std;

#define ll long long int
#define pii pair<int,int>
#define ti3 tuple<int,int,int>

struct BIT
{
    std::vector<int> bit;
    
    BIT(int size) : bit(size, 0){ }
    
    void add(int k, int x) {
        while (k < bit.size()) {
            bit[k] += x;
            k += k &-k;
        }
    }
    int sum(int k) {
        int res = 0;
        while (k > 0) {
            res += bit[k];
            k -= k&-k;
        }
        return res;
    }
};

struct UnionFind {
    vector<int> data;
    
    UnionFind(int size) : data(size, -1) { }
    
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y)
        {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

int h, w, k;
vector<string> wall;

int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};

pii start()
{
    for(int y = 0;y < h;y++)
    {
        for(int x = 0;x < w;x++)
        {
            if(wall[y][x] == 'S')
            {
                return make_pair(x,y);
            }
        }
    }
    return make_pair(0,0);
}

bool movable(int x, int y)
{
    return wall[y][x] != '#';
}


bool goal(int x, int y)
{
    if(x <= 0 || x >= w - 1)
    {
        return true;
    }
    if(y <= 0 || y >= h - 1)
    {
        return true;
    }
    return false;
}

vector<vector<int>> buf;

int dist2(int a)
{
    auto v = a / k;
    if(a % k > 0)
    {
        v++;
    }
    return v;
}

int dist(int x, int y)
{
    auto left = dist2(x);
    auto right = dist2(w - 1 - x);
    auto top = dist2(y);
    auto bottom = dist2(h - 1 - y);
    return min(left, min(right, min(top, bottom)));
}

int main(void)
{
    cin >> h >> w >> k;
    for(int y = 0;y < h;y++)
    {
        string s;
        cin >> s;
        wall.push_back(s);
    }
    for(int y = 0;y < h;y++)
    {
        buf.emplace_back(vector<int>(w, -1));
    }
    auto s = start();
    queue<ti3> q;
    q.push(make_tuple(s.first, s.second, k));
    buf[s.second][s.first] = k;
    bool first = false;
    while(!q.empty())
    {
        auto t = q.front();
        q.pop();
        int x,y,c;
        tie(x,y,c) = t;
        auto nc = c - 1;
        if(nc < 0)
        {
            continue;
        }
        for(int i = 0;i < 4;i++)
        {
            auto nx = x + dx[i];
            auto ny = y + dy[i];
            if(goal(nx, ny))
            {
                first = true;
                continue;
            }
            if(buf[ny][nx] < nc)
            {
                if(movable(nx, ny))
                {
                    //cout << nx << "," << ny << endl;
                    buf[ny][nx] = nc;
                    q.push(make_tuple(nx,ny,nc));
                }
            }
        }
    }
    if(first)
    {
        cout << 1 << endl;
        return 0;
    }
    int minDist = 100000;
    for(int y = 0;y < h;y++)
    {
        for(int x = 0;x < w;x++)
        {
            if(buf[y][x] >= 0)
            {
                //cout << x << "," << y << endl;
                minDist = min(minDist, dist(x,y) + 1);
            }
        }
    }
    cout << minDist << endl;
    return 0;
}