// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define pb push_back

#define pf push_front

#define eb emplace_back

#define PF pop_front

#define PB pop_back

#define mp make_pair

#define s second

#define f first

#define mt make_tuple

#define foreach(it, a) for(__typeof(a.begin()) it=a.begin(); it!=a.end(); it++)

#define forn(i, a) for(int i=0; i<a; i++)

#define INF 1e9

#define all(x) x.begin(), x.end()

#define MOD 1e9+7 

 

typedef long long ll;

typedef pair<int,int> pi;

typedef pair<pi,int> pii;

typedef unsigned long long ull;

typedef vector<int> vi;

typedef unsigned int uint;

 

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

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

 

int n, m, stx, sty, step[805][805], mx, res=2e9;

char grid[805][805];

queue<int> q;

 

inline bool out(int x, int y)

{

	return x<0||x==n||y<0||y==m;

}

 

void Init()

{

	cin>>n>>m>>mx;

	memset(step, -1, sizeof(step));

	for(int i=0; i<n; i++)

		for(int j=0; j<m; j++)

		{

			cin>>grid[i][j];

			if(grid[i][j]=='S')

				stx=i, sty=j;

		}

		

	q.push(stx), q.push(sty);

	

	step[stx][sty]=0;

	while(!q.empty())

	{

		int x=q.front();	q.pop();

		int y=q.front();	q.pop();

		if(step[x][y]==mx+1)

			break;

		for(int i=0; i<4; i++)

		{

			int xx=x+dx[i], yy=y+dy[i];

			if(!out(xx, yy)&&step[xx][yy]==-1&&grid[xx][yy]!='#')

			{

				step[xx][yy]=step[x][y]+1;

				q.push(xx);

				q.push(yy);

			}

		}

	}

	while(!q.empty())

		q.pop();

	for(int i=0; i<n; i++)

		for(int j=0; j<m; j++)

			if(step[i][j]<=mx&&step[i][j]!=-1)

				q.push(i), q.push(j);

}

 

int main() 

{

	Init();

	while(!q.empty())

	{

		int x=q.front();	q.pop();

		int y=q.front();	q.pop();

		res=min(res, (min(min(n-x-1, x), min(y, m-y-1))+mx-1)/mx);

	}

	printf("%d", res+1);

	return 0;

}

//

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

const int maxn=8e2+10;

using namespace std;

char a[maxn][maxn];

int n,m,k;

int stmp[maxn][maxn];

int posi[4][2]={1,0,-1,0,0,1,0,-1};

bool check(int x,int y)

{

    if(x>=0&&x<n&&y>=0&&y<m&&stmp[x][y]==0&&a[x][y]=='.')

    {

       return 1;

    }

    return 0;

}

queue<pair<int,int> >po;

int main()

{

    int ans=99999999;

    cin>>n>>m>>k;

    for(int i=0;i<n;i++)

    {

        scanf("%s",&a[i]);

    }

    int sx,sy;

    for(int i=0;i<n;i++)

    {

       for(int j=0;j<m;j++)

       {

          if(a[i][j]=='S')

          {

             sx=i;

             sy=j;

          }

       }

    }

    stmp[sx][sy]=1;

    po.push(make_pair(sx,sy));

    while(!po.empty())

    {

        int x=po.front().first;

        int y=po.front().second;

        ans=min(ans,min(min(x,n-x-1),min(y,m-y-1)));

        po.pop();

        for(int i=0;i<4;i++)

        {

            int xx=x+posi[i][0];

            int yy=y+posi[i][1];

            if(check(xx,yy)&&stmp[x][y]<=k)

            {

               stmp[xx][yy]=stmp[x][y]+1;

               po.push(make_pair(xx,yy));

            }

        }

    }

    int anss;

    if(ans%k==0)

    {

        anss=1+ans/k;

    }

    else

    {

        anss=2+ans/k;

    }

    cout<<anss<<endl;

    return 0;

}


// Fastest known solution for this problem: