//#pragma GCC optimize(3)
//#pragma comment(linker,"/stack:16777216")
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<functional>
#include<utility>
#include<algorithm>
#include<sstream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<exception>
#include<climits>
#include<cfloat>
#include<iomanip>
#include<bitset>
#include<streambuf>
#include<cstdlib>
#include<ctime>
//#include"functions.h"
//#include"highprecision.h"
#define pb push_back
#define pf push_front
#define ppb pop_back
#define mp make_pair
#define np next_permutation
#define fi first
#define sc second
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int> >
#define vpi vector<pair<int,int> >
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pff pair<float,float>
#define pdd pair<double,double>
#define psi pair<string,int>
#define pis pair<int,string>
#define ppi pair<pii,int>
#define pdi pair<pii,pii>
#define LN 84757
#define INF 1e9
#define LLINF 1e18
#define MAXINT 0x7fffffff
#define MAXLL 0x7fffffffffffffff
#define EPS 1e-5
#define DEPS 1e-9
#define LDEPS 1e-13
#define MOD 1000000007
#define E 2.7182818284
#define PI 3.1415926535
#define rep(a,b) for(int a=0;a<b;a++)
#define forn(a,n) for(int a=1;a<=n;a++)
#define forr(a,n) for(int a=n;a>=0;a--)
#define repn(a,n,p) for(int a=0;a<n;a+=p)
#define foreach(a,b) for(__typeof(b.begin())a=b.begin();a!=b.end();a++)
#define fillemp(a) memset(a,0,sizeof(a))
#define fillinf(a) memset(a,0x3f,sizeof(a))
#define frestream freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define stdf(a,b) freopen(a,"r",stdin);freopen(b,"w",stdout);
#define inputf(a) freopen(a,"r",stdin);
#define outputf(a) freopen(a,"w",stdout);
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
using namespace std;

int h,w,k;
string s[810];
int main(){
	frestream;
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>h>>w>>k;
	rep(i,h){
		cin>>s[i];
	}
	int sx,sy;
	rep(i,h)rep(j,w){
		if(s[i][j]=='S'){
			sx=i;sy=j;
		}
	}
	int to1=sx,to2=sy,to3=h-sx-1,to4=w-sy-1,mto=min(to1,min(to2,min(to3,to4)));
	string vv[4];
	if(mto==to1){
		int cx=sx,cy=sy;
		while(cx>=0&&cy>=0&&cx<h&&cy<w){
			vv[0].pb(s[cx][cy]);
			cx--;
		}
	}
	if(mto==to2){
		int cx=sx,cy=sy;
		while(cx>=0&&cy>=0&&cx<h&&cy<w){
			vv[1].pb(s[cx][cy]);
			cy--;
		}
	}
	if(mto==to3){
		int cx=sx,cy=sy;
		while(cx>=0&&cy>=0&&cx<h&&cy<w){
			vv[2].pb(s[cx][cy]);
			cx++;
		}
	}
	if(mto==to4){
		int cx=sx,cy=sy;
		while(cx>=0&&cy>=0&&cx<h&&cy<w){
			vv[3].pb(s[cx][cy]);
			cy++;
		}
	}
	int minn=INF;
	string v;a
	rep(i,4){
		int now=0;
		rep(j,vv[i].size()){
			if(vv[i][j]=='#')now++;
		}
		if(now<minn){
			minn=now;
			v=vv[i];
		}
	}
	int ans=0;
	if(v[0]=='S')v[0]='.';
	//cout<<v<<endl;
	int cur=0,cnt=0;
	bool st=0;
	while(cur!=v.size()-1){
		rep(i,k){
			if(cur==v.size()-1||v[cur+1]=='#')break;
			cur++;
		}
		for(int i=cur;i<v.size();i++){
			int cc=0;
			if(v[i]=='#'){
				v[i]='.';cc++;
			}
			if(cc==k){
				break;
			}
		}
		ans++;
	}
	cout<<ans<<endl;
    return 0;
}