#include <bits/stdc++.h>
using namespace std;
string a[800];
int mincost[800][800];
bool OK[800][800];
int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
signed main() {
  pair<int, int> S;
  int b, c, d;
  cin >> b >> c >> d;
  for (int e = 0; e < b; e++) {
    cin >> a[e];
    for (int f = 0; f < c; f++) {
      if (a[e][f] == 'S') {
        S = {e, f};
      }
    }
  }
  OK[S.first][S.second] = true;
  queue<pair<int, int> > Q;
  memset(mincost, 0x3f, sizeof(mincost));
  Q.push({S.first, S.second});
  mincost[S.first][S.second] = 0;
  while (Q.size()) {
    pair<int, int> t = Q.front();
    Q.pop();
    if (mincost[t.first][t.second] > d) {
      while (Q.size()) Q.pop();
      break;
    }
    OK[t.first][t.second] = true;
    for (int u = 0; u < 4; u++) {
      int n = t.first + dx[u], m = t.second + dy[u];
      if (n >= 0 && n < b && m >= 0 && m < c && mincost[n][m] > 100000 &&
          a[n][m] != '#') {
        mincost[n][m] = mincost[t.first][t.second] + 1;
        Q.push({n, m});
      }
    }
  }
  for (int n = 0; n < b; n++) {
    for (int m = 0; m < c; m++) {
      if ((n == 0 || m == 0 || n + 1 == b || m + 1 == c) && OK[n][m]) {
        cout << 1 << endl;
        return 0;
      }
    }
  }
  for (int i = 2;; i++) {
    memset(mincost, 0x3f, sizeof(mincost));
    for (int n = 0; n < b; n++) {
      for (int m = 0; m < c; m++) {
        if (OK[n][m]) {
          Q.push({n, m});
          mincost[n][m] = 0;
        }
      }
    }
    while (Q.size()) {
      pair<int, int> t = Q.front();
      Q.pop();
      if (mincost[t.first][t.second] > d) {
        while (Q.size()) Q.pop();
        break;
      }
      OK[t.first][t.second] = true;
      for (int u = 0; u < 4; u++) {
        int n = t.first + dx[u], m = t.second + dy[u];
        if (n >= 0 && n < b && m >= 0 && m < c && mincost[n][m] > 100000) {
          mincost[n][m] = mincost[t.first][t.second] + 1;
          Q.push({n, m});
        }
      }
    }
    for (int n = 0; n < b; n++) {
      for (int m = 0; m < c; m++) {
        if (n == 0 || m == 0 || n + 1 == b || m + 1 == c) {
          cout << i << endl;
          return 0;
        }
      }
    }
  }
}
