#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int H, W, K;
vector<string> cells;
vector<vector<int>> reach;
const int MAX = 10000;
set<P> find_poses(int turn) {
  set<P> poses;
  for (int h = 0; h < (int)(H); ++h)
    for (int w = 0; w < (int)(W); ++w) {
      if (reach[h][w] == turn) poses.insert(make_pair(w, h));
    }
  return poses;
}
bool can_go(int w, int h, bool break_room) {
  if (!(0 <= w && w < W && 0 <= h && h < H)) return false;
  if (!break_room)
    return cells[h][w] != '#';
  else
    return true;
}
void debug_print_reach() {
  cout << endl;
  for (int h = 0; h < (int)(H); ++h) {
    for (int w = 0; w < (int)(W); ++w) {
      cout << reach[h][w] << "\t";
    }
    cout << endl;
  }
  cout << endl;
}
void fill_room(int turn, bool break_room = false) {
  int dh[5] = {1, -1, 0, 0, 0};
  int dw[5] = {0, 0, 1, -1, 0};
  set<P> poses = find_poses(turn);
  for (int k = 0; k < (int)(K); ++k) {
    set<P> new_poses = find_poses(turn);
    for (auto& pos : poses) {
      auto w = pos.first;
      auto h = pos.second;
      auto cur_d = reach[h][w];
      for (int d = 0; d < (int)(5); ++d) {
        auto w2 = w + dw[d];
        auto h2 = h + dh[d];
        if (!can_go(w2, h2, break_room)) {
          continue;
        } else {
          if (reach[h2][w2] > turn + 1) {
            new_poses.insert(make_pair(w2, h2));
          }
          if (break_room == false) {
            reach[h2][w2] = turn + 1;
          } else {
            if (reach[h2][w2] > turn + 1) {
              reach[h2][w2] = turn + 1;
            }
          }
        }
      }
    }
    poses = new_poses;
  }
}
bool reach_goal(void) {
  for (int h = 0; h < (int)(H); ++h) {
    if (reach[h][0] != MAX) return true;
    if (reach[h][W - 1] != MAX) return true;
  }
  for (int w = 0; w < (int)(W); ++w) {
    if (reach[0][w] != MAX) return true;
    if (reach[H - 1][w] != MAX) return true;
  }
  return false;
}
void break_wall(int turn) {
  for (int h = 0; h < (int)(H); ++h) {
    for (int w = 0; w < (int)(W); ++w) {
      if (reach[h][w] == turn) {
        reach[h][w] = MAX;
        cells[h][w] = '.';
      }
    }
  }
}
int main(void) {
  cin.sync_with_stdio(false);
  cin >> H >> W >> K;
  cells.resize(H);
  reach.resize(H);
  for (int h = 0; h < (int)(H); ++h) {
    cin >> cells[h];
    reach[h].resize(W, MAX);
    auto loc = cells[h].find('S');
    if (loc != string::npos) {
      P start = make_pair(loc, h);
      reach[h][loc] = 0;
      cells[h][loc] = '.';
    }
  }
  int turn = 0;
  while (1) {
    fill_room(turn);
    turn += 1;
    if (reach_goal()) {
      cout << (turn) << endl;
      return 0;
    }
    fill_room(turn, true);
    break_wall(turn);
  }
  return 0;
}
