#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
int H, W, K;
int sy, sx;
struct pnt {
  int y, x, cost, cnt;
};
bool operator<(const pnt &pnt1, const pnt &pnt2) {
  return pnt1.cost < pnt2.cost;
}
bool operator>(const pnt &pnt1, const pnt &pnt2) {
  return pnt1.cost > pnt2.cost;
}
vector<vector<int>> mv = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int bfs(vector<vector<char>> &grid) {
  priority_queue<pnt, vector<pnt>, greater<pnt>> que;
  que.push({sy, sx, 1, 0});
  while ((int)que.size() > 0) {
    pnt now = que.top();
    que.pop();
    if (now.y == 0 || now.y == H - 1 || now.x == 0 || now.x == W - 1) {
      return now.cost;
    } else if (now.cnt == K) {
      now.cost++;
      now.cnt = 0;
    }
    bool flag = true;
    for (auto &m : mv) {
      int nx, ny;
      nx = now.x + m[1];
      ny = now.y + m[0];
      if (grid[ny][nx] == '#' && now.cost == 1) {
        continue;
      } else {
        flag = false;
        que.push({ny, nx, now.cost, now.cnt + 1});
      }
    }
    if (flag) {
      que.push({now.y, now.x, now.cost + 1, 0});
    }
  }
}
int main() {
  cin >> H >> W >> K;
  vector<vector<char>> grid(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      grid[i][j] = c;
      if (c == 'S') {
        sy = i;
        sx = j;
      }
    }
  }
  cout << bfs(grid) << endl;
  ;
}
