#include <bits/stdc++.h>
using namespace std;
template <typename T>
using V = vector<T>;
int R, C, K;
int sr, sc;
V<string> G;
V<V<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
bool check(int allowed, int moves) {
  V<V<int>> vis(R, V<int>(C));
  queue<tuple<int, int, int, int>> Q;
  Q.emplace(sr, sc, 0, 0);
  vis[sr][sc] = 1;
  while (Q.size()) {
    int r, c, seen, dist;
    tie(r, c, seen, dist) = Q.front();
    Q.pop();
    if (r == 0 || c == 0) return 1;
    for (const auto& dir : dirs) {
      int cr = r + dir[0], cc = c + dir[1];
      if (cr < 0 || cr >= R || cc < 0 || cc >= C) continue;
      if (vis[cr][cc]) continue;
      if (seen + (G[cr][cc] == '#') > allowed) continue;
      if (dist + 1 > moves) continue;
      Q.emplace(cr, cc, seen + (G[cr][cc] == '#'), dist + 1);
      vis[cr][cc] = 1;
    }
  }
  return 0;
}
int solve_fast() {
  int lo = 1, hi = R * C + 100;
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    int64_t num = int64_t(mid - 1) * int64_t(K);
    if (num > R * C) num = R * C;
    if (check(num, num + K)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  return lo;
}
int solve_slow() {
  V<V<unordered_map<int, bool>>> vis(R, V<unordered_map<int, bool>>(C));
  V<V<int>> dist(R, V<int>(C, 1e9));
  queue<tuple<int, int, int, int>> Q;
  Q.emplace(sr, sc, 0, 0);
  vis[sr][sc][0] = 1;
  dist[sr][sc] = 1;
  while (Q.size()) {
    int r, c, seen, d;
    tie(r, c, seen, d) = Q.front();
    dist[r][c] = min(dist[r][c],
                     max(seen / K + bool(seen % K) + 1, d / K + bool(d % K)));
    Q.pop();
    for (const auto& dir : dirs) {
      int cr = r + dir[0], cc = c + dir[1];
      if (cr < 0 || cr >= R || cc < 0 || cc >= C) continue;
      if (seen + (G[cr][cc] == '#') > 50) continue;
      if (vis[cr][cc][seen + (G[cr][cc] == '#')]) continue;
      Q.emplace(cr, cc, seen + (G[cr][cc] == '#'), d + 1);
      vis[cr][cc][seen + (G[cr][cc] == '#')] = 1;
    }
  }
  int ans = 1e9;
  for (int(r) = 0; (r) < (R); ++(r)) {
    ans = min(ans, dist[r][0]);
  }
  for (int(c) = 0; (c) < (C); ++(c)) {
    ans = min(ans, dist[0][c]);
  }
  return ans;
}
void test() {
  default_random_engine re;
  for (int tc = 0; tc < 100000; ++tc) {
    R = re() % 10 + 1;
    C = re() % 10 + 1;
    K = re() % 10 + 1;
    G = vector<string>(R, string(C, '.'));
    for (int(r) = 0; (r) < (R); ++(r)) {
      for (char& c : G[r]) {
        c = (re() % 2 ? '#' : '.');
      }
    }
    sr = re() % R;
    sc = re() % C;
    G[sr][sc] = 'S';
    cout << R << " " << C << " " << K << endl;
    for (string s : G) {
      cout << s << endl;
    }
    cout << solve_slow() << " " << solve_fast() << endl;
    assert(solve_slow() == solve_fast());
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> R >> C >> K;
  G.resize(R);
  for (string& s : G) {
    cin >> s;
  }
  for (int(r) = 0; (r) < (R); ++(r)) {
    for (int(c) = 0; (c) < (C); ++(c))
      if (G[r][c] == 'S') {
        sr = r;
        sc = c;
        break;
      }
  }
  cout << solve_slow() << endl;
}
