#include <bits/stdc++.h>
using namespace std;
int n, m, k;
string s[1001];
int d[1001][1001];
int minx = 1e9;
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int in(int x, int y) { return (x >= 0 && y >= 0 && x < n && y < m); }
pair<int, int> st;
int dfs() {
  for (int i = 0; i < 1000; i++) {
    for (int j = 0; j < 1000; j++) d[i][j] = 1e9;
  }
  d[st.first][st.second] = 0;
  queue<pair<int, int> > q;
  q.push(st);
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    int R1, R2, R3, R4;
    int h[4] = {0};
    h[0] = u.first;
    h[1] = n - u.first + 1;
    h[2] = u.second;
    h[3] = m - u.second + 1;
    R1 = h[0] / k;
    if (h[0] % k != 0) R1++;
    R2 = h[1] / k;
    if (h[1] % k != 0) R2++;
    R3 = h[2] / k;
    if (h[2] % k != 0) R3++;
    R4 = h[3] / k;
    if (h[3] % k != 0) R4++;
    minx = min(minx, R1 + 1);
    minx = min(minx, R2 + 1);
    minx = min(minx, R3 + 1);
    minx = min(minx, R4 + 1);
    for (int j = 0; j < 4; j++) {
      int X = u.first + dx[j];
      int Y = u.second + dy[j];
      if (in(X, Y)) {
        if (s[X][Y] == '.') {
          if (d[X][Y] == 1e9) {
            d[X][Y] = d[u.first][u.second] + 1;
            if (d[X][Y] <= k) q.push(make_pair(X, Y));
            if (X == 0 || Y == 0 || X == n - 1 || Y == m - 1) {
              minx = 1;
            }
          }
        }
      }
    }
  }
}
int main() {
  cin >> n >> m >> k;
  cin.ignore();
  for (int i = 0; i < n; i++) {
    char c[1001];
    scanf("%s", c);
    s[i] = (string)c;
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'S') {
        st = make_pair(i, j);
      }
    }
  }
  dfs();
  cout << minx;
}
