#include <bits/stdc++.h>
using namespace std;
__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
int h, w, k, minw, mini;
vector<vector<char> > buil(801, vector<char>(801));
vector<vector<long long> > ma(801, vector<long long>(801, 99999));
int tansaku(int gh, int gw, int num) {
  if (ma[gh][gw] <= num || buil[gh][gw] == '#') {
    return 1;
  }
  if (gh == 0 || gh == h - 1 || gw == 0 || gw == w - 1) {
    return 0;
  }
  if (num == k) {
    int res = min(min(gh, h - gh), min(gw, w - gw));
    if (mini > res) {
      mini = res;
    }
    return 1;
  }
  ma[gh][gw] = num;
  return min(min(tansaku(gh + 1, gw, num + 1), tansaku(gh - 1, gw, num + 1)),
             min(tansaku(gh, gw + 1, num + 1), tansaku(gh, gw - 1, num + 1)));
}
int main() {
  cin >> h >> w >> k;
  char a;
  int sh, sw;
  for (int i = (0); i < (h); i++) {
    for (int j = (0); j < (w); j++) {
      cin >> a;
      if (a == 'S') {
        sh = i;
        sw = j;
      }
      buil[i][j] = a;
    }
  }
  if (!tansaku(sh, sw, 0)) {
    cout << (1) << endl;
    ;
    return 0;
  }
  int ans = min(min(h - mini, w - mini), mini) / k;
  cout << (ans + ans ? 1 : 2) << endl;
  ;
}
