#include <bits/stdc++.h>
using namespace std;
char heya[800][802];
char (*heya_)[802] = heya;
int h, w, k;
bool fill_s(int y, int x, int k) {
  if (k == 0) return false;
  if (x - 1 != -1) {
    if (x - 1 == 0 && heya_[y][x - 1] != '#') {
      return true;
    } else if (heya_[y][x - 1] == 'S') {
    } else if (heya_[y][x - 1] == '.') {
      heya_[y][x - 1] = 'S';
      if (fill_s(y, x - 1, k - 1)) return true;
    }
  }
  if (x + 1 != w) {
    if (x + 1 == w - 1 && heya_[y][x + 1] != '#') {
      return true;
    } else if (heya_[y][x + 1] == 'S') {
    } else if (heya_[y][x + 1] == '.') {
      heya_[y][x + 1] = 'S';
      if (fill_s(y, x + 1, k - 1)) return true;
    }
  }
  if (y - 1 != -1) {
    if (y - 1 == 0 && heya_[y - 1][x] != '#') {
      return true;
    } else if (heya_[y - 1][x] == 'S') {
    } else if (heya_[y - 1][x] == '.') {
      heya_[y - 1][x] = 'S';
      if (fill_s(y - 1, x, k - 1)) return true;
    }
  }
  if (y + 1 != h) {
    if (y + 1 == h - 1 && heya_[y + 1][x] != '#') {
      return true;
    } else if (heya_[y + 1][x] == 'S') {
    } else if (heya_[y + 1][x] == '.') {
      heya_[y + 1][x] = 'S';
      if (fill_s(y + 1, x, k - 1)) return true;
    }
  }
  return false;
}
int main() {
  cin >> h >> w >> k;
  {
    char tmp[5];
    cin.getline(tmp, 5);
  }
  for (int i = 0; i < h; i++) {
    cin.getline(heya[i], 802);
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (heya[i][j] == 'S' && fill_s(i, j, k)) {
        cout << 1 << endl;
        return 0;
      }
    }
  }
  int best = 99999999;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (heya[i][j] == 'S') {
        best = min(best, i);
        best = min(best, j);
        best = min(best, h - 1 - i);
        best = min(best, w - 1 - j);
      }
    }
  }
  cout << (best - 1) / k + 2 << endl;
  return 0;
}
