#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define repi(a,b) for (int i=a;i<=b;i++)
#define repj(a,b) for (int j=a;j<=b;j++)
#define oo 123456789

int n,m,k,x,y,ans=oo,ansx,ansy;
char a[805][805];
int d[805][805];
int used[805][805];
int dir[4][2]={1,0,0,1,-1,0,0,-1};

int Min(int a,int b,int c,int d)
{
	return min(a,min(b,min(c,d)));
}

void pre()
{
	repi(1,n)
		repj(1,m){
			if (a[i][j]=='S') x=i,y=j;
			d[i][j]=Min(i-1,j-1,n-i,n-j);
		}
}

void dfs(int x,int y,int time,int minn)
{
	//cout<<x<<' '<<y<<' '<<time<<' '<<minn<<endl;
	
	if (time==k){
		if (minn<ans){
			ans=minn,ansx=x,ansy=y;//cout<<"*@";
		}
		return ;
	}
	
	int i,tx,ty,flag=0;
	for (i=0;i<4;i++)
	{
		tx=x+dir[i][0];
		ty=y+dir[i][1];
		if (tx>=1 && tx<=n && ty>=1 && ty<=m && a[tx][ty] == '.' && used[tx][ty] == 0)
		{
			flag=1;
			used[tx][ty]=1;
			dfs(tx,ty,time+1,min(minn,d[tx][ty]));
			used[tx][ty]=0;
		}
	}
	
	if (!flag) dfs(x,y,k,minn);
}

int main()
{
	cin>>n>>m>>k;
	repi(1,n) scanf ("%s",a[i]+1);
	
	pre();

	used[x][y]=1;
	ans=d[x][y],ansx=x,ansy=y;
	dfs(x,y,0,d[x][y]);
	
	//cout<<'*'<<ans<<' '<<ansx<<' '<<ansy<<' '<<endl;
	
	if (x==n || x==1 || y==1 || y==n) cout<<1<<endl;
	else{
		if (ans%k==0) cout<<ans/k+1<<endl;
		else cout<<ans/k+2<<endl;
	}
}