#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <utility>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <istream>
#include <ostream>

#include <cstdlib>
#include <cmath>
#include <cstdio>

using namespace std;

#define fi first
#define se second
#define mkp make_pair
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define rep(i,n) for(ll i=0; i < (n); ++i)
#define rrep(i,n) for(ll i=((n)-1); i >= 0; --i)

#define OPLT(T) bool operator<(const T & lop_, const T & rop_)
#define OPEQ(T) bool operator==(const T & lop_, const T & rop_)

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

istream& operator>>(istream& istr, __float128& obj) { double d; istr >> d; obj = d; return istr; };
ostream& operator<<(ostream& ostr, __float128& obj) { ostr << static_cast<double>(obj); return ostr; };

const int INF = 1e9;
int cost[2][810][810];
bool B[810][810];
int main() {
	int H, W, K;
	cin >> H >> W >> K;
	vector<string> b(H);
	rep(i,H) cin >> b[i];
	rep(i,2)rep(j,810)rep(k,810) cost[i][j][k] = INF;

	queue<pair<pii,int> > q;
	rep(i,H) {
		rep(j,W) {
			if(b[i][j] == 'S') {
				b[i][j] = '.';
				cost[0][i][j] = 0;
				q.push(pair<pii,int>(pii(i,j),0));
			}
			B[i][j] = b[i][j] == '#';
		}
	}
	int res = INF;
	for(int i = 0; i < 401; i++) {
		queue<pair<pii,int> > nex;
		while(q.size()) {
			pair<pii,int> a = q.front();
			q.pop();
			const int dxy[] = {1,0,-1,0,1};
			rep(j,4) {
				const int ny = a.fi.fi+dxy[j], nx = a.fi.se+dxy[j+1];
				if(ny<0 || nx<0 || ny >= H || nx >= W) continue;
				if(B[ny][nx] && a.se+1 < cost[(i^1)&1][ny][nx]) {
					cost[(i^1)&1][ny][nx] = a.se+1;
					nex.push(pair<pii,int>(pii(ny,nx),a.se+1));
				}
				else if(!B[ny][nx] && a.se+1 < cost[i&1][ny][nx]) {
					cost[i&1][ny][nx] = a.se+1;
					cost[(i^1)&1][ny][nx] = a.se+1;
					q.push(pair<pii,int>(pii(ny,nx),a.se+1));
				}
			}
		}
		q = nex;
		rep(j,W)
			res = min(res, (i+K-1)/K + (cost[i&1][0][j]+K-1)/K);
		rep(j,H) {
			res = min(res, (i+K-1)/K + min((cost[i&1][j][0]+K-1)/K, (cost[i&1][j][W-1]+K-1)/K));
		}
		rep(j,W)
			res = min(res, (i+K-1)/K + (cost[i&1][H-1][j]+K-1)/K);
	}
	cout << res << endl;
	return 0;
}
