#include <bits/stdc++.h>
#pragma warning(disable : 4996)
const long long MOD = 1000000007;
using namespace std;
char str[800][805];
int main(int argc, char* argv[]) {
  int h, w, K;
  scanf("%d%d%d", &h, &w, &K);
  vector<int> vis(h * w);
  int si = -1, sj = -1;
  int i, j;
  for (i = 0; i < h; i++) {
    scanf("%s", str[i]);
    for (j = 0; j < w; j++) {
      if (str[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  assert(si >= 0 && sj >= 0);
  int dx[4] = {1, -1, 0, 0};
  int dy[4] = {0, 0, 1, -1};
  queue<pair<int, int> > que0, que1;
  que0.push(make_pair(((si)*w + (sj)), 0));
  int t;
  for (t = 0; t < 1000; t++) {
    while (!que0.empty()) {
      auto tmp = que0.front();
      int curr = tmp.first;
      int dist = tmp.second;
      que0.pop();
      int i = curr / h;
      int j = curr % h;
      vis[((i)*w + (j))] = 1;
      if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
        printf("%d\n", t + 1);
        return 0;
      }
      int p;
      for (p = 0; p < 4; p++) {
        int i2 = i + dx[p];
        int j2 = j + dy[p];
        int dist2 = dist + 1;
        if (i2 < 0 || i2 >= h || j2 < 0 || j2 >= w) continue;
        if (dist < K && str[i2][j2] == '.' && !vis[((i2)*w + (j2))]) {
          int dist2 = dist + 1;
          que0.push(make_pair(((i2)*w + (j2)), dist2));
        }
        if (str[i2][j2] == '#' && !vis[((i2)*w + (j2))]) {
          que1.push(make_pair(((i2)*w + (j2)), 0));
        }
      }
    }
    while (!que1.empty()) {
      auto tmp = que1.front();
      int curr = tmp.first;
      int dist = tmp.second;
      que1.pop();
      int i = curr / h;
      int j = curr % h;
      vis[((i)*w + (j))] = 1;
      que0.push(make_pair(((i)*w + (j)), 0));
      int p;
      for (p = 0; p < 4; p++) {
        int i2 = i + dx[p];
        int j2 = j + dy[p];
        int dist2 = dist + 1;
        if (i2 < 0 || i2 >= h || j2 < 0 || j2 >= w) continue;
        if (dist < K && str[i2][j2] == '#' && !vis[((i2)*w + (j2))]) {
          int dist2 = dist + 1;
          que1.push(make_pair(((i2)*w + (j2)), dist2));
        }
        if (str[i2][j2] == '.' && !vis[((i2)*w + (j2))]) {
          que0.push(make_pair(((i2)*w + (j2)), 0));
        }
      }
    }
  }
  return 0;
}
