#include <bits/stdc++.h>
using namespace std;
const int IN = 1e9;
const long long m = 1e9 + 7;
const pair<int, int> INF = {1e9, 1e9};
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  string s[h];
  for (int i = 0; i < h; i++) cin >> s[i];
  int sx, sy;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      if (s[i][j] == 'S') sx = i, sy = j;
  }
  int px[4] = {1, -1, 0, 0};
  int py[4] = {0, 0, 1, -1};
  pair<int, int> an[h][w];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) an[i][j] = INF;
  an[sx][sy] = {0, k};
  priority_queue<pair<pair<int, int>, pair<int, int>>,
                 vector<pair<pair<int, int>, pair<int, int>>>,
                 greater<pair<pair<int, int>, pair<int, int>>>>
      que;
  que.push({{0, k}, {sx, sy}});
  while (!que.empty()) {
    pair<int, int> point = que.top().first;
    int nx = que.top().second.first;
    int ny = que.top().second.second;
    que.pop();
    if (point > an[nx][ny]) continue;
    if (point.second == k)
      point.first++, point.second = 0;
    else
      point.second++;
    for (int i = 0; i < 4; i++) {
      int tx = nx + px[i], ty = ny + py[i];
      if (point.first != 1 || s[tx][ty] == '.') {
        if (an[tx][ty] > point) {
          an[tx][ty] = point;
          if (tx != 0 && tx != h - 1 && ty != 0 && ty != w - 1)
            que.push({point, {tx, ty}});
        }
      } else {
        pair<int, int> point2 = {2, 0};
        if (an[tx][ty] > point2) {
          an[tx][ty] = point2;
          if (tx != 0 && tx != h - 1 && ty != 0 && ty != w - 1)
            que.push({point2, {tx, ty}});
        }
      }
    }
  }
  int answer = 1e9;
  for (int i = 0; i < h; i++) {
    answer = min(answer, an[i][0].first);
    answer = min(answer, an[i][w - 1].first);
  }
  for (int j = 0; j < w; j++) {
    answer = min(answer, an[0][j].first);
    answer = min(answer, an[w - 1][j].first);
  }
  cout << answer << endl;
}
