#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e3 + 20;
ll d[maxn][maxn] , h , t , res = 1e18 , n , m;
string s[maxn];
pair<ll , ll> q[maxn * maxn];
bool is(int x , int y , bool k = 0)
{
    if(x < 0 || x > n - 1 || y < 0 || y > m - 1)
        return 0;
    if(k)
        return s[x][y] != '#';
    return 1;
}
void bfs(vector<pair<int , int> > src)
{
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
        {
            d[i][j] = -1;
        }
    h = t = 0;
    for(auto x : src)
        d[x.first][x.second] = 0 , q[h++] = {x.first , x.second};
    while(t < h)
    {
        int x = q[t].first , y = q[t++].second;
        if(is(x + 1 , y))
        {
            if(d[x + 1][y] == -1)
            {
                d[x + 1][y] = d[x][y] + 1;
                q[h++] = {x + 1 , y};
            }
        }
        if(is(x - 1 , y))
        {
            if(d[x - 1][y] == -1)
            {
                d[x - 1][y] = d[x][y] + 1;
                q[h++] = {x - 1 , y};
            }
        }
        if(is(x , y + 1))
        {
            if(d[x][y + 1] == -1)
            {
                d[x][y + 1] = d[x][y] + 1;
                q[h++] = {x , y + 1};
            }
        }
        if(is(x , y - 1))
        {
            if(d[x][y - 1] == -1)
            {
                d[x][y - 1] = d[x][y] + 1;
                q[h++] = {x , y - 1};
            }
        }
    }
}
void bffs(int x , int y)
{
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            d[i][j] = -1;
    d[x][y] = 0;
    q[h++] = {x , y};
    while(t < h)
    {
        int x = q[t].first , y = q[t++].second;
        if(is(x + 1 , y , 1))
        {
            if(d[x + 1][y] == -1)
            {
                d[x + 1][y] = d[x][y] + 1;
                q[h++] = {x + 1 , y};
            }
        }
        if(is(x - 1 , y , 1))
        {
            if(d[x - 1][y] == -1)
            {
                d[x - 1][y] = d[x][y] + 1;
                q[h++] = {x - 1 , y};
            }
        }
        if(is(x , y + 1 , 1))
        {
            if(d[x][y + 1] == -1)
            {
                d[x][y + 1] = d[x][y] + 1;
                q[h++] = {x , y + 1};
            }
        }
        if(is(x , y - 1 , 1))
        {
            if(d[x][y - 1] == -1)
            {
                d[x][y - 1] = d[x][y] + 1;
                q[h++] = {x , y - 1};
            }
        }
    }
}
int main()
{
    int k , x1 , y1;
    cin >> n >> m >> k;
    for(int i = 0; i < n; i++)
    {
        cin >> s[i];
        for(int j = 0; j < m; j++)
        {
            if(s[i][j] == 'S')
                x1 = i , y1 = j;
        }
    }
    bffs(x1 , y1);
    vector< pair<int , int> > a;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
        {
            if(d[i][j] != -1 && d[i][j] <= k)
                a.pb({i , j});
        }
    bfs(a);
    for(int i = 0; i < n; i++)
        res = min(res , min(d[i][0] , d[i][m - 1]));
    for(int i = 0; i < m; i++)
        res = min(res , min(d[0][i] , d[n - 1][i]));
    res = max(res , 0LL);
    cout << (res / k + (res % k != 0)) + 1;
}


Measured memory_bytes: 31340