#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = decltype(1ull);
template <class T>
int size(T&& a) {
  return a.size();
}
template <class B>
pair<int, B> make_pair(B b) {
  return {0, b};
}
void Calc();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin.exceptions(istream::failbit | istream::badbit);
  cout << fixed << setprecision(15);
  Calc();
  cout.flush();
  return 0;
}
template <class A>
void amax(A& a, A b) {
  a = max(a, b);
}
template <class A>
void amin(A& a, A b) {
  a = min(a, b);
}
template <class A>
int comp(A a, A b) {
  return a < b ? 1 : b < a ? -1 : 0;
}
struct Scanner {
  template <class A = string>
  A Next() {
    A a;
    Next(a);
    return a;
  }
  template <class A, class B>
  void Next(pair<A, B>& a) {
    cin >> a.first >> a.second;
  }
  template <class A>
  void Next(A& a) {
    cin >> a;
  }
  template <class A = int>
  vector<A> Array(int n) {
    vector<A> a(n);
    for (A& i : a) (*this), i;
    return a;
  }
  string Line() const {
    string s;
    getline(cin, s);
    return s;
  }
  template <class A>
  Scanner& operator,(A& a) {
    a = Next<A>();
    return *this;
  }
  template <class A>
  operator A() {
    return Next<A>();
  }
} in;
struct XY {
  int x, y;
  XY& operator+=(const XY& d) {
    x += d.x;
    y += d.y;
    return *this;
  }
  XY operator+(const XY& d) const { return XY(*this) += d; }
  bool operator==(const XY& d) const { return x == d.x && y == d.y; }
  bool check(int w, int h) const { return 0 <= x && x < w && 0 <= y && y < h; }
};
const XY d4[] = {
    {-1, 0},
    {1, 0},
    {0, -1},
    {0, 1},
};
template <class T>
auto get(T& a, XY d) -> decltype(a[d.y][d.x]) {
  return a[d.y][d.x];
}
ostream& operator<<(ostream& os, XY d) {
  return os << '(' << d.x << ',' << d.y << ')';
}
void Calc() {
  int h, w, k;
  in, h, w, k;
  ;
  auto s = in.Array<string>(h);
  XY start;
  for (typename make_signed<decltype(make_pair(h).second)>::type
           i = make_pair(h).first,
           _li = make_pair(h).second;
       i < _li; i++)
    for (typename make_signed<decltype(make_pair(w).second)>::type
             j = make_pair(w).first,
             _lj = make_pair(w).second;
         j < _lj; j++)
      if (s[i][j] == 'S') {
        start = {j, i};
        break;
      }
  vector<XY> p;
  p.push_back(start);
  queue<XY> q;
  q.push(start);
  while (q.size()) {
    auto t = q.front();
    q.pop();
    if (get(s, t) == 'A') continue;
    p.push_back(t);
    get(s, t) = 'A';
    for (auto& i : d4) {
      auto next = i + t;
      if (next.check(w, h) && get(s, next) == '.') {
        q.push(next);
      }
    }
  }
  const int INF = 1e9 - 1;
  int ans = INF;
  for (auto& i : p) {
    auto mini = min({i.x, i.y, w - i.x, h - i.y});
    amin(ans, (mini + k - 1) / k + 1);
  }
  cout << (ans) << endl;
}
