#include <bits/stdc++.h>
using namespace std;
struct lol {
  int x, y, tip, used;
  lol(int _x = 0, int _y = 0, int _t = 0, int us = 0) {
    x = _x;
    y = _y;
    tip = _t;
    used = us;
  }
};
const int dx[] = {0, 0, -1, 1};
const int dy[] = {1, -1, 0, 0};
int i, j, n, m, k, dir, dp[1005][1005], x, y, tip, used, newx, newy,
    rs = 1e9 + 69;
string a[1005];
queue<lol> q;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> k;
  for (i = 0; i < n; ++i) cin >> a[i];
  for (i = 0; i < n; ++i)
    for (j = 0; j < m; ++j) {
      dp[i][j] = 1e9 + 69;
      if (a[i][j] == 'S') dp[i][j] = 0, q.push(lol(i, j, 1, k - 1));
    }
  for (; !q.empty(); q.pop()) {
    x = q.front().x;
    y = q.front().y;
    tip = q.front().tip;
    used = q.front().used;
    for (dir = 0; dir < 4; ++dir) {
      newx = x + dx[dir];
      newy = y + dy[dir];
      if (newx < 0 || newx >= n) continue;
      if (newy < 0 || newy >= m) continue;
      if (tip < 2) {
        if (a[newx][newy] == '.') {
          if (dp[newx][newy] <= dp[x][y] + (used == k - 1)) continue;
          dp[newx][newy] = dp[x][y] + (used == k - 1);
          q.push(lol(newx, newy, tip + (used == k - 1), (used + 1) % k));
        } else {
          if (dp[newx][newy] <= dp[x][y] + 2 * (used == k - 1)) continue;
          dp[newx][newy] = dp[x][y] + 2;
          q.push(lol(newx, newy, tip + (used == k - 1), (used + 1) % k));
        }
      } else {
        if (dp[newx][newy] <= dp[x][y] + (used == k - 1)) continue;
        dp[newx][newy] = dp[x][y] + (used == k - 1);
        q.push(lol(newx, newy, 3, (used + 1) % k));
      }
    }
  }
  for (i = 0; i < n; ++i) rs = min({rs, dp[i][0], dp[i][m - 1]});
  for (i = 0; i < m; ++i) rs = min({rs, dp[0][i], dp[n - 1][i]});
  cout << rs << '\n';
  return 0;
}
