#include <bits/stdc++.h>
using namespace std;
char g[812][812];
int n, m, k;
int dx[]{-1, 0, 0, 1};
int dy[]{0, -1, 1, 0};
struct step {
  int x, y, u, d;
};
bool operator<(const step& a, const step& b) {
  return a.u / k + (a.u % k > 0) + a.d / k + (a.d % k > 0) >
         b.u / k + (b.u % k > 0) + b.d / k + (b.d % k > 0);
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int s_x, s_y;
  cin >> n >> m >> k;
  k *= 2;
  for (int i = 0; i < n; ++i) {
    cin >> g[i];
    for (int j = 0; j < m; ++j)
      if (g[i][j] == 'S') s_x = i, s_y = j;
  }
  priority_queue<step> pq;
  pq.push({s_x, s_y, 0, 0});
  while (pq.size()) {
    auto f = pq.top();
    pq.pop();
    if (f.x == 0 || 0 == f.y) {
      int cast = f.u / k + f.d / k;
      cast += f.u % k > 0;
      cast += f.d % k > 0;
      cout << cast << endl;
      exit(0);
    }
    for (int i = 0; i < 4; ++i) {
      int x = f.x + dx[i];
      int y = f.y + dy[i];
      if (n <= x || m <= y) continue;
      pq.push({x, y, f.u + 1, f.d + (g[x][y] == '#')});
    }
  }
}
