// I need some holiday
// But I don't wish that everyday is holiday
// Because I didn't bear the pain
// Because it wasn't a true freedom
//         ——『イヤホンと蝉時雨』·Orangestar

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned int UI;
typedef pair<int, int> pii;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define MP make_pair
#define fst first
#define snd second
#define SZ(u) ((int) (u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status()
{
	ifstream t("/proc/self/status");
	cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
}

template<typename T> inline int chkmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline int chkmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }
template<typename T> inline T sqr(const T &val) { return val * val; }

namespace fastIO
{
	const int MAX_BUFFER_SIZE = 1 << 16;

	char buffer[MAX_BUFFER_SIZE], *cur = buffer, *ed = buffer;

	inline char getc() { return cur == ed && (ed = (cur = buffer) + fread(buffer, 1, MAX_BUFFER_SIZE, stdin), cur == ed) ? EOF : *cur++; }
}
using fastIO::getc;

template<typename T> inline T read()
{
	register T sum(0), fg(1);
	register char ch(getc());
	for(; !isdigit(ch); ch = getc()) if(ch == '-') fg = -1;
	for(;  isdigit(ch); ch = getc()) sum = sum * 10 - '0' + ch;
	return sum * fg;
}

inline char *read_str(char *s)
{
	register char ch(getc());
	while(!isgraph(ch)) ch = getc();
	for(; isgraph(ch); ch = getc()) *s++ = ch;
	*s = '\0';
	return s;
}

// END tpl.

inline char safe_getc()
{
	register char ch(getc());
	while(!isgraph(ch)) ch = getc();
	return ch;
}

const int MAXN = 800;

int n, m, K;

char a[MAXN + 5][MAXN + 5];

inline void input()
{
	n = read<int>(), m = read<int>(), K = read<int>();
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= m; ++j)
			a[i][j] = safe_getc();
}

struct node
{
	int x, y;

	node() { }
	node(int _x, int _y): x(_x), y(_y) { }
};

int d[MAXN + 5][MAXN + 5];

inline void BFS()
{
	static queue<node> q;
	memset(d, -1, sizeof d);

	for(int i = 1; i <= m; ++i)
	{
		d[1][i] = 0, q.emplace(1, i);
		d[n][i] = 0, q.emplace(n, i);
	}
	for(int i = 1; i <= n; ++i)
	{
		d[i][1] = 0, q.emplace(i, 1);
		d[i][m] = 0, q.emplace(i, m);
	}

	while(!q.empty())
	{
		node u = q.front();
		q.pop();

		static const int D[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
		for(int i = 0; i < 4; ++i)
		{
			node v(u.x + D[i][0], u.y + D[i][1]);
			if(v.x < 1 || v.y < 1 || v.x > n || v.y > m || d[v.x][v.y] != -1) continue;
			d[v.x][v.y] = d[u.x][u.y] + 1, q.push(v);
		}
	}
}

inline void solve()
{
	BFS();

	static int d0[MAXN + 5][MAXN + 5];

	static queue<node> q;
	memset(d0, -1, sizeof d0);

	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= m; ++j)
			if(a[i][j] == 'S') d0[i][j] = 0, q.emplace(i, j);

	while(!q.empty())
	{
		node u = q.front();
		q.pop();

		static const int D[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
		for(int i = 0; i < 4; ++i)
		{
			node v(u.x + D[i][0], u.y + D[i][1]);
			if(v.x < 1 || v.y < 1 || v.x > n || v.y > m || d0[v.x][v.y] != -1 || a[v.x][v.y] == '#') continue;
			d0[v.x][v.y] = d0[u.x][u.y] + 1, q.push(v);
		}
	}

	int ans = INT_MAX;
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= m; ++j)
			if(d0[i][j] != -1 && d0[i][j] <= K) chkmin(ans, (d[i][j] + K - 1) / K + 1);
	printf("%d\n", ans);
}

int main()
{
#ifdef K_ON
	freopen("C.in", "r", stdin);
	freopen("C.out", "w", stdout);
#endif

	input();
	solve();

	return 0;
}




Measured memory_bytes: 6144