//by spj_58
#include <bits/stdc++.h>
using namespace std;

#define REP(i,N) for(int (i)=0;i<(int)(N);i++)

template <class T> bool minn(T &a,T b) { return (a > b ? (a = b, 1) : 0); }
template <class T> bool maxn(T &a,T b) { return (a < b ? (a = b, 1) : 0); }

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

int dist[801][801];
string f[801];

int nceil(int a,int b)
{
	return (a+b-1)/b;
}
int main(void)
{
	int N,M,K;

	cin >> N >> M >> K;

	int sx=-1,sy=-1;
	REP(i,N);
	{
		cin>>f[i];
		REP(j,M)
			if(f[i][j]=='S')
			{
				sx=i;
				sy=j;
			}
	}

	vector<pair<int,int> > q;
	REP(i,N)
		REP(j,M)
			dis[i][j] = 1e9;
	dis[sx][sy] = 0;

	q.push_back(make_pair(sx,sy));
	REP(cue,q.size())
	{
		int x=q[cur].first, y=q[cur].second;
		int ncur=dis[x][y];

		REP(d,4)
		{
			int xx = x+dx[d],yy = y+dy[d];
			if(min(xx,yy) < 0 || min(N-xx,M-yy) <= 0 || dis[xx][yy] < 1e8 || f[xx][yy] == '#') continue;
			dis[xx][yy] = ncur+1;
			q.push_back(make_pair(xx,yy));
		}
	}

	int ans=1e9;
	REP(i,N)
		REP(j,M)
		{
			if(dis[i][j] > K) continue;
			int res = 1e9;
			minn(res,nceil(i,K));
			minn(res,nceil(j,K));
			minn(res,nceil(N-i-1,K));
			minn(res,nceil(M-j-1,K));
			minn(ans,res+1);
		}

	cout << ans << endl;

	return 0;
}