#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
typedef long long ll ;
#define rep(i, a, b) for (int i = a; i <= b; ++ i)
const int N = 805, inf = 1e9 + 7 ;
const int flg[4][2] = {{1, 0}, {- 1, 0}, {0, 1}, {0, - 1}} ;
using namespace std ;
int n, m, k, f[N][N] ;
char a[N][N] ;
struct poi {
int x, y ;
} opt[N * N] ;
bool check(int x, int y) {
return (a[x][y] != '#' && x >= 1 && y >= 1 && x <= n && y <= m && f[x][y] == inf) ;
}
int main() {
scanf("%d%d%d", &n, &m, &k) ;
rep(i, 1, n) scanf("%s", a[i] + 1) ;
int x, y ;
rep(i, 1, n) rep(j, 1, m) if (a[i][j] == 'S') x = i, y = j ;
int he = 0, ta = 1 ;
rep(i, 1, n) rep(j, 1, m) f[i][j] = inf ;
opt[1].x = x, opt[1].y = y, f[x][y] = 0 ;
for ( ; he != ta; ) {
++ he ;
int x = opt[he].x, y = opt[he].y ;
rep(i, 0, 3) {
int xx = x + flg[i][0], yy = y + flg[i][1] ;
if (check(xx, yy)) f[xx][yy] = f[x][y] + 1, ++ ta, opt[ta].x = xx, opt[ta].y = yy ;
}
}
int ans = inf ;
rep(i, 1, n) rep(j, 1, m) if (f[i][j] <= k) ans = min(ans, (min(i - 1, min(n - i, min(j - 1, m - j))) + k - 1) / k + 1) ;
printf("%d\n", ans) ;
return 0 ;
}