#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int i, j;
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (i = 0; i < h; i++) cin >> s[i];
  int sum[800][800];
  int black[800][800];
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++) {
      sum[i][j] = 1e9;
      black[i][j] = 1e9;
    }
  queue<pair<int, int> > q;
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      if (s[i][j] == 'S') {
        sum[i][j] = 0;
        black[i][j] = 0;
        q.push(pair<int, int>(i, j));
      }
  int d[5] = {0, 1, 0, -1, 0};
  while (q.size()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (i = 0; i < 4; i++) {
      int ni = x + d[i];
      int nj = y + d[i + 1];
      if (ni < 0 || nj < 0 || ni >= h || nj >= w) continue;
      if (s[ni][nj] == '#') {
        if (black[ni][nj] > black[x][y] + 1) {
          black[ni][nj] = black[x][y] + 1;
          sum[ni][nj] = sum[x][y] + 1;
          q.push(pair<int, int>(ni, nj));
        } else if (black[ni][nj] == black[x][y] + 1) {
          if (sum[ni][nj] > sum[x][y] + 1) {
            black[ni][nj] = black[x][y] + 1;
            sum[ni][nj] = sum[x][y] + 1;
            q.push(pair<int, int>(ni, nj));
          }
        }
      } else {
        if (black[ni][nj] > black[x][y]) {
          black[ni][nj] = black[x][y];
          sum[ni][nj] = sum[x][y] + 1;
          q.push(pair<int, int>(ni, nj));
        } else if (black[ni][nj] == black[x][y]) {
          if (sum[ni][nj] > sum[x][y] + 1) {
            black[ni][nj] = black[x][y];
            sum[ni][nj] = sum[x][y] + 1;
            q.push(pair<int, int>(ni, nj));
          }
        }
      }
    }
  }
  int ans = 1e9;
  int a, b;
  for (i = 0; i < h; i++) {
    a = sum[i][0] / k + 1 - (sum[i][0] % k == 0);
    b = black[i][0] / k + 1 - (black[i][0] % k == 0);
    ans = min(ans, a + max(0, b - a + 1));
    a = sum[i][w - 1] / k + 1 - (sum[i][w - 1] % k == 0);
    b = black[i][w - 1] / k + 1 - (black[i][w - 1] % k == 0);
    ans = min(ans, a + max(0, b - a + 1));
  }
  for (i = 0; i < w; i++) {
    a = sum[0][i] / k + 1 - (sum[0][i] % k == 0);
    b = black[0][i] / k + 1 - (black[0][i] % k == 0);
    ans = min(ans, a + max(0, b - a + 1));
    a = sum[h - 1][i] / k + 1 - (sum[h - 1][i] % k == 0);
    b = black[h - 1][i] / k + 1 - (black[h - 1][i] % k == 0);
    ans = min(ans, a + max(0, b - a + 1));
  }
  cout << ans << endl;
}
