Language: C++

#include<bits/stdc++.h>
using namespace std;

long long H, W, K;

long long A[805][805];
long long WORK[805][805];

#define OPEN INT_MAX
#define CLOSED LONG_MAX/3


bool valid(long long h, long long w){
	return (h >= 0) && (h < H) && (w >= 0) && (w < W);
}

void move(long long h, long long w, long long cost){
	if(valid(h, w) == false || A[h][w] == CLOSED || cost > K || A[h][w] <= cost){
		return;
	}
	A[h][w] = cost;
	move(h-1, w, cost+1);
	move(h+1, w, cost+1);
	move(h, w-1, cost+1);
	move(h, w+1, cost+1);
}

void firstmove(){
	for(long long h = 0; h < H; h++){
		for(long long w = 0; w < W; w++){
			if(A[h][w] == 1){
				move(h, w, 0);
				return;
			}
		}
	}
}


int main(){
	cin >> H >> W >> K;

	for(long long h = 0; h < H; h++){
		string S;
		cin >> S;
		for(long long w = 0; w < W; w++){
			if(S[w] == '.'){
				A[h][w] = OPEN;
			}else if(S[w] == 'S'){
				A[h][w] = 1;
			}else{
				A[h][w] = CLOSED;
			}
		}
	}

	firstmove();

	long long ans = INT_MAX;
	for(long long h = 0; h < H; h++){
		for(long long w = 0; w < W; w++){
			if(A[h][w] != OPEN && A[h][w] != CLOSED){
				long long tmp;
				tmp = h;
				tmp = min(tmp, w);
				tmp = min(tmp, H - h - 1);
				tmp = min(tmp, W - w - 1);
				if(tmp % K == 0){
					ans = min(ans, tmp/K);
				}else{
					ans = min(ans, tmp/K + 1);
				}
			}
		}
	}
	cout << ans + 1 << endl;
}


Compiler output:
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h: In instantiation of '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = true; _II = long long int; _OI = int]':
/usr/include/c++/14/bits/stl_algobase.h:548:42:   required from '_OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove = true; _II = long long int; _OI = int]'
  548 |     { return std::__copy_move_a2<_IsMove>(__first, __last, __result); }
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:556:31:   required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = true; _II = long long int; _OI = int]'
  556 |                 std::__copy_move_a1<_IsMove>(std::__niter_base(__first),
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  557 |                                              std::__niter_base(__last),
      |                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~
  558 |                                              std::__niter_base(__result)));
      |                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:683:38:   required from '_OI std::move(_II, _II, _OI) [with _II = long long int; _OI = int]'
  683 |       return std::__copy_move_a<true>(std::__miter_base(__first),
      |              ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  684 |                                       std::__miter_base(__last), __result);
      |                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:32:9:   required from here
   32 |                                 move(h, w, 0);
      |                                 ~~~~^~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:514:64: error: no type named 'iterator_category' in 'struct std::iterator_traits<long long int>'
  514 |       typedef typename iterator_traits<_II>::iterator_category _Category;
      |                                                                ^~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:521:51: error: no type named 'iterator_category' in 'struct std::iterator_traits<long long int>'
  520 |       return std::__copy_move<_IsMove, __memcpyable<_OI, _II>::__value,
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  521 |                               _Category>::__copy_m(__first, __last, __result);
      |                               ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~