#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7;
const int inf = (int)1e9 + 123;
const long long infl = (long long)1e18 + 123;
const double eps = 1e-10;
const int MAX_N = (int)3e5 + 123;
int n, m, k;
char a[1005][1005];
set<pair<int, int> > q;
bool u[1005][1005];
int dist[1005][1005];
queue<pair<int, int> > Q;
vector<pair<int, int> > st;
int moves[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int ans = 0;
void go1() {
  for (auto i : q) {
    Q.push(i);
    dist[i.first][i.second] = 0;
  }
  st.clear();
  while (!Q.empty()) {
    pair<int, int> v = Q.front();
    Q.pop();
    st.push_back(v);
    if (dist[v.first][v.second] == k) continue;
    for (int i = 0; i < 4; i++) {
      pair<int, int> nw =
          make_pair(v.first + moves[i][0], v.second + moves[i][1]);
      if (nw.first >= 0 && nw.first < n && nw.second >= 0 && nw.second < m &&
          a[nw.first][nw.second] == '.' && !u[nw.first][nw.second]) {
        dist[nw.first][nw.second] = dist[v.first][v.second] + 1;
        u[nw.first][nw.second] = 1;
        Q.push(nw);
      }
    }
  }
  q.clear();
  for (auto i : st) {
    bool have = 0;
    for (int it = 0; it < 4; it++) {
      pair<int, int> nw =
          make_pair(i.first + moves[it][0], i.second + moves[it][1]);
      if (nw.first < 0 || nw.first >= n || nw.second < 0 || nw.second >= m ||
          !u[nw.first][nw.second])
        have = 1;
    }
    if (have) q.insert(i);
  }
}
void go2() {
  for (auto i : q) {
    Q.push(i);
    dist[i.first][i.second] = 0;
  }
  st.clear();
  while (!Q.empty()) {
    pair<int, int> v = Q.front();
    Q.pop();
    st.push_back(v);
    if (dist[v.first][v.second] == k) continue;
    for (int i = 0; i < 4; i++) {
      pair<int, int> nw =
          make_pair(v.first + moves[i][0], v.second + moves[i][1]);
      if (nw.first >= 0 && nw.first < n && nw.second >= 0 && nw.second < m &&
          a[nw.first][nw.second] == '#') {
        dist[nw.first][nw.second] = dist[v.first][v.second] + 1;
        a[nw.first][nw.second] = '.';
        Q.push(nw);
      }
    }
  }
}
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 0; i < n; i++) {
    getchar();
    for (int j = 0; j < m; j++) a[i][j] = getchar();
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] == 'S') {
        q.insert(make_pair(i, j));
      }
  while (1) {
    for (auto i : q)
      if (!i.first || i.first == n - 1 || !i.second || i.second == m - 1) {
        printf("%d\n", ans);
        return 0;
      }
    ans++;
    go1();
    go2();
  }
  return 0;
}
