#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
const int M = 1e9 + 7;
const ll MLL = 1e18L + 9;
#pragma unused(M)
#pragma unused(MLL)
template <class... T>
void printl(T&&...) {}
template <class... T>
void printc(T&&...) {}
template <class... T>
void prints(T&&...) {}
template <class... T>
void printd(T&&...) {}
template <class... T>
void printArr(T&&...) {}
struct Elem {
  int y, x, blocks;
  bool operator<(const Elem& e) const {
    return y != e.y ? y < e.y : x != e.x ? x < e.x : blocks < e.blocks;
  }
};
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<char>> as(h, vector<char>(w));
  set<Elem> q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> as[i][j];
      if (as[i][j] == 'S') {
        as[i][j] = '.';
        q.insert({i, j, 0});
      }
    }
  }
  map<pair<int, int>, int> visited;
  for (int i = 0; i < 800; i++) {
    for (int j = 0; j < k; j++) {
      set<Elem> next;
      for (const Elem& e : q) {
        if (visited.count({e.y, e.x}) && visited[{e.y, e.x}] < e.blocks) {
          continue;
        }
        visited[{e.y, e.x}] = e.blocks;
        auto f = [&](int y, int x, int b) {
          int nb = b + (as[y][x] == '#');
          if (nb <= k * i) {
            prints(y, x, b);
            if (y == 0 || y == h - 1 || x == 0 || x == w - 1) {
              cout << i + 1 << '\n';
              exit(0);
            }
            next.insert({y, x, nb});
          }
        };
        f(e.y, e.x, e.blocks);
        f(e.y + 1, e.x, e.blocks);
        f(e.y - 1, e.x, e.blocks);
        f(e.y, e.x + 1, e.blocks);
        f(e.y, e.x - 1, e.blocks);
      }
      next.swap(q);
    }
  }
  return 0;
}
