Language: C++

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

int n, m, k, step=0;    pair<int, int> s, bas;
vector< vector<char> > vec; vector< vector<int> > ans;
queue< pair<int, int> > q;   vector< vector<int> > visit;
void dene();
void kir();

void kir(){
    while(!q.empty()){
        auto p=q.front();   q.pop();
        int x=p.first, y=p.second;  int cx=x, cy=y; int i=0;
        //cout << cx << " " << cy << "\n";
        while(cx>0 and k>i){
            cx--;
            if(vec[cx][y]=='#'){
                vec[cx][y]='.'; i++;
            }
            //cout << cx << " " << cy << "\n";
        }
        i=0;   cx=x;
        while(n-1>cx and k>i){
            cx++;
            if(vec[cx][y]=='#'){
                vec[cx][y]='.';  i++;
            }
        }
        i=0;   cy=y;
        while(cy>0 and k>i){
            cy--;
            if(vec[x][cy]=='#'){
                vec[x][cy]='.';  i++;
            }
        }
        i=0;   cy=y;
        while(n-1>cy and k>i){
            cy++;
            if(vec[x][cy]=='#'){
                vec[x][cy]='.';   i++;
            }
        }
        s={x, y};
        dene();
    }
}


void dene(){
    int x=s.first;  int y=s.second; visit[x][y]=1;
    bool sol=true, sag=true, yukari=true, asagi=true;   ++step; bool islem=false;
    for(int i=1;k>=i;i++){
        if(x+i<n and vec[x+i][y]=='.' and sag and !visit[x+i][y]){
            ans[x+i][y]=step;   q.push({x+i, y});   visit[x+i][y]=1;    islem=true;
        }
        if(y+i<m and vec[x][y+i]=='.' and yukari and !visit[x][y+i]){
            ans[x][y+i]=step;   q.push({x, y+i});   visit[x][y+i]=1;    islem=true;
        }
        if(x-i>=0 and vec[x-i][y]=='.' and sol and !visit[x-i][y]){
            ans[x-i][y]=step;   q.push({x-i, y});   visit[x-i][y]=1;    islem=true;
        }
        if(y-i>=0 and vec[x][y-i]=='.' and asagi and !visit[x][y-i]){
            ans[x][y-i]=step;   q.push({x, y-i});   visit[x][y-i]=1;    islem=true;
        }
        if(x+i<n and vec[x+i][y]=='#'){
            sag=false;
        }
        if(x-i>=0 and vec[x-i][y]=='#'){
            sol=false;
        }
        if(y+i<n and vec[x][y+i]=='#'){
            yukari=false;
        }
        if(y-i>0 and vec[x][y-i]=='#'){
            asagi=false;
        }
    }
    if(islem){
        kir();
    }
    else if(s==bas){
        q.push({x, y}); kir();
    }
}

int main(){
    ios::sync_with_stdio(false);    cin.tie(NULL);  cout.tie(NULL);
    cin >> n >> m >> k; vec.resize(n, vector<char>(m));
    ans.resize(n, vector<int>(m, INF)); visit.resize(n, vector<int>(m, 0));
    for(int i=0;n>i;i++){
        for(int j=0;m>j;j++){
            cin >> vec[i][j];
            if(vec[i][j]=='S'){
                s={i, j};   ans[i][j]=0;
            }
        }
    }
    bas=s;  dene();/*
    for(int i=0;n>i;i++){
        for(int j=0;m>j;j++){
            if(ans[i][j]==INF){
                cout << "U" << ' '; continue;
            }
            cout << ans[i][j] << " ";
        }
        cout << "\n";
    }*/
    int mini=INF;
    for(int i=0;n>i;i++){
        mini=min(mini, ans[i][0]);  mini=min(mini, ans[i][m-1]);
    }
    for(int i=0;m>i;i++){
        mini=min(mini, ans[0][i]);  mini=min(mini, ans[n-1][i]);
    }
    cout << mini;
}


Compiler output:
a.cc: In function 'void dene()':
a.cc:50:37: error: reference to 'visit' is ambiguous
   50 |     int x=s.first;  int y=s.second; visit[x][y]=1;
      |                                     ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:52: note:                 'std::vector<std::vector<int> > visit'
    6 | queue< pair<int, int> > q;   vector< vector<int> > visit;
      |                                                    ^~~~~
a.cc:53:52: error: reference to 'visit' is ambiguous
   53 |         if(x+i<n and vec[x+i][y]=='.' and sag and !visit[x+i][y]){
      |                                                    ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:52: note:                 'std::vector<std::vector<int> > visit'
    6 | queue< pair<int, int> > q;   vector< vector<int> > visit;
      |                                                    ^~~~~
a.cc:54:53: error: reference to 'visit' is ambiguous
   54 |             ans[x+i][y]=step;   q.push({x+i, y});   visit[x+i][y]=1;    islem=true;
      |                                                     ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:52: note:                 'std::vector<std::vector<int> > visit'
    6 | queue< pair<int, int> > q;   vector< vector<int> > visit;
      |                                                    ^~~~~
a.cc:56:55: error: reference to 'visit' is ambiguous
   56 |         if(y+i<m and vec[x][y+i]=='.' and yukari and !visit[x][y+i]){
      |                                                       ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:52: note:                 'std::vector<std::vector<int> > visit'
    6 | queue< pair<int, int> > q;   vector< vector<int> > visit;
      |                                                    ^~~~~
a.cc:57:53: error: reference to 'visit' is ambiguous
   57 |             ans[x][y+i]=step;   q.push({x, y+i});   visit[x][y+i]=1;    islem=true;
      |                                                     ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:52: note:                 'std::vector<std::vector<int> > visit'
    6 | queue< pair<int, int> > q;   vector< vector<int> > visit;
      |                                                    ^~~~~
a.cc:59:53: error: reference to 'visit' is ambiguous
   59 |         if(x-i>=0 and vec[x-i][y]=='.' and sol and !visit[x-i][y]){
      |                                                     ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:52: note:                 'std::vector<std::vector<int> > visit'
    6 | queue< pair<int, int> > q;   vector< vector<int> > visit;
      |                                                    ^~~~~
a.cc:60:53: error: reference to 'visit' is ambiguous
   60 |             ans[x-i][y]=step;   q.push({x-i, y});   visit[x-i][y]=1;    islem=true;
      |                                                     ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:52: note:                 'std::vector<std::vector<int> > visit'
    6 | queue< pair<int, int> > q;   vector< vector<int> > visit;
      |                                                    ^~~~~
a.cc:62:55: error: reference to 'visit' is ambiguous
   62 |         if(y-i>=0 and vec[x][y-i]=='.' and asagi and !visit[x][y-i]){
      |                                                       ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:52: note:                 'std::vector<std::vector<int> > visit'
    6 | queue< pair<int, int> > q;   vector< vector<int> > visit;
      |                                                    ^~~~~
a.cc:63:53: error: reference to 'visit' is ambiguous
   63 |             ans[x][y-i]=step;   q.push({x, y-i});   visit[x][y-i]=1;    islem=true;
      |                                                     ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:52: note:                 'std::vector<std::vector<int> > visit'
    6 | queue< pair<int, int> > q;   vector< vector<int> > visit;
      |                                                    ^~~~~
a.cc: In function 'int main()':
a.cc:89:34: error: 'INF' was not declared in this scope
   89 |     ans.resize(n, vector<int>(m, INF)); visit.resize(n, vector<int>(m, 0));
      |                                  ^~~
a.cc:89:41: error: reference to 'visit' is ambiguous
   89 |     ans.resize(n, vector<int>(m, INF)); visit.resize(n, vector<int>(m, 0));
      |                                         ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:6:52: note:                 'std::vector<std::vector<int> > visit'
    6 | queue< pair<int, int> > q;   vector< vector<int> > visit;
      |                                                    ^~~~~