#include <bits/stdc++.h>
using namespace std;
void JIZZ(string output = "") {
  cout << output;
  exit(0);
}
const long double PI = 3.14159265358979323846264338327950288;
const long double eps = 1e-10;
const long long mod = 1e9 + 7;
int mp[808][808];
int dis[808][808];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  int sx = -1, sy = -1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      char c;
      cin >> c;
      if (c == 'S') sx = i, sy = j;
      if (c == '#')
        mp[i][j] = -1;
      else if (c == 'S')
        mp[i][j] = 1;
      else
        mp[i][j] = 0;
    }
  for (int t = 1;; ++t) {
    memset((dis), (0x3f), sizeof(dis));
    queue<int> q;
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j)
        if (mp[i][j] == 1) dis[i][j] = 0, q.push(i), q.push(j);
    while (q.size()) {
      int nx = q.front(), ny = (q.pop(), q.front());
      q.pop();
      for (int d = 0; d < 4; ++d) {
        int tx = nx + dx[d], ty = ny + dy[d];
        if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
        if (dis[tx][ty] != 0x3f3f3f3f) continue;
        if (mp[tx][ty] != 0) continue;
        dis[tx][ty] = dis[nx][ny] + 1;
        mp[tx][ty] = 1;
        if (dis[tx][ty] < k) q.push(tx), q.push(ty);
      }
    }
    memset((dis), (0x3f), sizeof(dis));
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j)
        if (mp[i][j] != -1) dis[i][j] = 0, q.push(i), q.push(j);
    while (q.size()) {
      int nx = q.front(), ny = (q.pop(), q.front());
      q.pop();
      for (int d = 0; d < 4; ++d) {
        int tx = nx + dx[d], ty = ny + dy[d];
        if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
        if (dis[tx][ty] != 0x3f3f3f3f) continue;
        if (mp[tx][ty] != -1) continue;
        dis[tx][ty] = dis[nx][ny] + 1;
        mp[tx][ty] = 0;
        if (dis[tx][ty] < k) q.push(tx), q.push(ty);
      }
    }
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j)
        if (i == 1 || i == n || j == 1 || j == m)
          if (mp[i][j] == 1) {
            exit((cout << t << endl, 0));
          }
  }
}
