https://atcoder.jp/contests/agc014/standings#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include <fstream>
#include <time.h>
#include <iterator>


#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define m0(x) memset(x,0,sizeof(x))
#define print(x) cout<<x<<endl;
#define pe(x) cout<<x<<" ";
#define lb(v,n) lower_bound(v.begin(), v.end(), n);
#define ub(v,n) upper_bound(v.begin(), v.end(), n);
#define int long long
#define all(x) (x).begin(), (x).end()

using namespace std;

int MOD = (ll)1000000000 + 7;
const ll INF = 1e17;
const double pi = acos(-1);
const double EPS = 1e-10;
typedef pair<int, int>P;
const int MAX = 200020;

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

bool visited[1000][1000];
char A[1000][1000];
int H, W,K;
int dfs(int x,int y,int n) {
	visited[x][y] = true;
	if (n == 0) {
		return min({ x,H - x - 1,y,W - y- 1 });
	}
	int res = min({ x,H - x - 1,y,W - y-1 });
	REP(i, 4) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx >= 0 && nx < H && ny >= 0 && ny < W) {
			if (visited[nx][ny])continue;
			if (A[nx][ny] != '#') {
				res=min(res,dfs(nx, ny, n - 1));
			}
		}
	}
	return res;
}

void solve() {
	cin >> H >> W>>K;
	int sx, sy;
	REP(i, H) {
		REP(j, W) {
			cin >> A[i][j];
			if (A[i][j] == 'S') {
				sx = i, sy = j;
			}
		}
	}
	int rem = dfs(sx, sy, K);
	int ans = 1;
	ans += (rem + K - 1) / K;
	print(ans);
}


signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	solve();
}
