#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e9;
const long long mod = (long long)1e9 + 7;
const double eps = (double)1e-9;
const double pi = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int N = 1000;
int n, m, k, ans = inf, dp[N][N];
char c[N][N];
queue<pair<int, int> > q;
int first(int X) { return (X + k - 1) / k; }
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) dp[i][j] = inf;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      cin >> c[i][j];
      if (c[i][j] == 'S') {
        dp[i][j] = 0;
        q.push(make_pair(i, j));
      }
    }
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if (x == 1 || y == 1 || x == n || y == m) {
      cout << 1;
      return 0;
    }
    for (int dir = 0; dir < 4; ++dir) {
      int xa = x + dx[dir], ya = y + dy[dir];
      if (c[xa][ya] == '.' && dp[xa][ya] > dp[x][y] + 1) {
        dp[xa][ya] = dp[x][y] + 1;
        q.push(make_pair(xa, ya));
      }
    }
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      if (dp[i][j] <= k) {
        ans = min(ans, min(min(first(i - 1), first(j - 1)),
                           min(first(n - i), first(m - j))) +
                           1);
      }
    }
  cout << ans;
  return 0;
}
