#include <bits/stdc++.h>
using namespace std;
int H, W, K;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
void print(vector<vector<char> > b) {
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      cerr << (int)b[y][x];
    }
    cerr << endl;
  }
  cerr << endl;
}
bool check(int y, int x) {
  return (y == 0 || y == H - 1 || x == 0 || x == W - 1);
}
bool check(pair<int, int> p) { return check(p.first, p.second); }
int main() {
  int a[2];
  cin >> H >> W >> K;
  vector<vector<char> > kabe(H, vector<char>(W, 0));
  vector<vector<char> > s(H, vector<char>(W, 0));
  deque<pair<int, int> > pos;
  pair<int, int> start;
  for (int y = (0); y < (H); ++y) {
    for (int x = (0); x < (W); ++x) {
      char c;
      cin >> c;
      if (c == '#') kabe[y][x] = 1;
      if (c == 'S') {
        s[y][x] = 1;
        start = pair<int, int>(y, x);
        pos.push_back(start);
      }
    }
  }
  print(kabe);
  cerr << endl;
  cerr << "s" << endl;
  print(s);
  vector<vector<char> > ts(H, vector<char>(W, 0));
  for (int y = (0); y < (H); ++y) {
    for (int x = (0); x < (W); ++x) {
      ts[y][x] = s[y][x];
    }
  }
  vector<vector<vector<char> > > kabes = vector<vector<vector<char> > >(
      1, vector<vector<char> >(H, vector<char>(W, 0)));
  for (int y = (0); y < (H); ++y) {
    for (int x = (0); x < (W); ++x) {
      kabes[0][y][x] = kabe[y][x];
    }
  }
  int bn = 0;
  for (;; bn++) {
    cerr << "bn\t" << bn << endl;
    kabes.push_back(vector<vector<char> >(H, vector<char>(W, 0)));
    for (int y = (0); y < (H); ++y) {
      for (int x = (0); x < (W); ++x) {
        if (kabes[bn][y][x] == 1) kabes[bn + 1][y][x] = kabes[bn][y][x];
      }
    }
    for (int k = (0); k < (K); ++k) {
      deque<pair<int, int> > tpos;
      while (!pos.empty()) {
        pair<int, int> tp = pos.front();
        pos.pop_front();
        for (int i = (0); i < (4); ++i) {
          int ty = tp.first + dy[i];
          int tx = tp.second + dx[i];
          if (ts[ty][tx] == 0) {
            if (kabes[bn][ty][tx] == 0) {
              pos.push_back(pair<int, int>(ty, tx));
              ts[ty][tx] = 1;
              if (check(ty, tx)) {
                goto roopend1;
              }
            } else {
              kabes[bn + 1][ty][tx] = 0;
              ts[ty][tx] = 1;
              tpos.push_back(pair<int, int>(ty, tx));
              if (check(ty, tx)) {
                bn++;
                goto roopend1;
              }
            }
          }
        }
      }
      while (!tpos.empty()) {
        pair<int, int> tp = tpos.front();
        pos.push_back(tp);
        tpos.pop_front();
      }
    }
  }
roopend1:
  cerr << "roopedend1 bn:\t" << bn << endl;
  for (int b = (0); b < (kabes.size()); ++b) {
    cerr << "kabes " << b << endl;
    print(kabes[b]);
    cerr << endl;
  }
  pos.clear();
  pos.push_back(start);
  for (int y = (0); y < (H); ++y) {
    for (int x = (0); x < (W); ++x) {
      ts[y][x] = s[y][x];
    }
  }
  int cnt = 1;
  for (int b = (0); b < (kabes.size()); ++b) {
    for (int k = (0); k < (K); ++k) {
      deque<pair<int, int> > tpos;
      while (!pos.empty()) {
        pair<int, int> tp = pos.front();
        pos.pop_front();
        for (int i = (0); i < (4); ++i) {
          int ty = tp.first + dy[i];
          int tx = tp.second + dx[i];
          if (ts[ty][tx] == 0) {
            if (kabes[b][ty][tx] == 0) {
              tpos.push_back(pair<int, int>(ty, tx));
              ts[ty][tx] = 1;
              if (check(ty, tx)) {
                cout << cnt << endl;
                return 0;
              }
            }
          }
        }
        tpos.push_back(tp);
      }
      while (!tpos.empty()) {
        pair<int, int> tp = tpos.front();
        pos.push_back(tp);
        tpos.pop_front();
      }
    }
    cnt++;
  }
  cerr << "ts" << endl;
  print(ts);
  for (;;) {
    for (int k = (0); k < (K); ++k) {
      deque<pair<int, int> > tpos;
      while (!pos.empty()) {
        pair<int, int> tp = pos.front();
        pos.pop_front();
        for (int i = (0); i < (4); ++i) {
          int ty = tp.first + dy[i];
          int tx = tp.second + dx[i];
          if (ts[ty][tx] == 0) {
            if (kabes[kabes.size() - 1][ty][tx] == 0) {
              tpos.push_back(pair<int, int>(ty, tx));
              ts[ty][tx] = 1;
              if (check(ty, tx)) {
                cout << cnt << endl;
                return 0;
              }
            }
          }
        }
      }
      while (!tpos.empty()) {
        pair<int, int> tp = tpos.front();
        pos.push_back(tp);
        tpos.pop_front();
      }
    }
    cnt++;
  }
  return 0;
}
