Language: C++

#include <iostream>
#include <sstream>
#include <vector>
#include <cmath>
#include <ctime>
#include <cassert>
#include <ctype.h>
#include <iomanip>
#include <cstdio>
#include <float.h>
#include <queue>
#include <set>
#include <map>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <stack>
#include <functional>
// #include <pair> 
       
using namespace std;
       
#define endl '\n'
#define MOD 1000000007
#define INF 1ll<<30
 
// #define MAX 100010
 
#define eps 1e-11
#define bit_max 1ll<<32
#define _USE_MATH_DEFINES
 
// long long binpow(long long a,long long b)
// {
//     if (b == 1) return a;
//     long long res = binpow(a, b/2)%MOD;
//     if (b % 2) return (res*((res*a)%MOD))%MOD;
//     else return (res * res)%MOD;
// }
 
// long long int fact(long long int a)
// {
//     if(a==0||a==1)
//         return 1;
//     long long int ans = 1;
//     for(int i=a;i>=1;i--)
//         ans = (ans * i);
//     return ans;
// }
 
// long long int ivmod(long long int a)
// {
//     return binpow(a,MOD-2);
// }
 
// long long int ncr(long long int a,long long int b)
// {
//     long long int pans=1;
//     for(int i =a;i>max(b,a-b);i--)
//         pans=pans*i;
//     long long int div = fact(min(b,a-b));
//     // cout<<ans<<"            "<<div<<endl;
//     pans = pans/div;
//     return pans;
// }
int h,w,k;
char grid[1000][1000];
int dx[] = {1,0,0,-1};
int dy[] = {0,1,-1,0};
long long int cost[1000][1000];
long long int calc(int x,int y)
{
    int mn;
    mn = min(x,y);
    mn = min(mn,h-1-x);
    mn = min(mn,w-1-y);
    // return mn;
// }

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    // #endif   
    for(int i=0;i<1000;i++)
    {
        for(int j=0;j<1000;j++)
            cost[i][j] = 1e18;
    }
    long long int ans =  1e18;
    queue<int> q;
    cin>>h>>w>>k;
    int startx=-1,starty=-1;
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            cin>>grid[i][j];
            if(grid[i][j]=='S')
            {
                grid[i][j]='.';
                startx = i;
                starty = j;
            }
        }
    }
    cost[startx][starty] = 0;
    q.push(startx);
    q.push(starty);
    ans = min(ans,calc(startx,starty));
    int tx,ty;
    while(!q.empty())
    {
        tx = q.front();
        q.pop();
        ty = q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int tdx,tdy;
            tdx = tx + dx[i];
            tdy = ty + dy[i];
            if(tdx < 0 || tdy<0 || tdx >= h ||tdy>=w)
            {
                continue;
            }
            if(grid[tdx][tdy]=='#')
            {
                continue;
            }
            if(cost[tdx][tdy]!=1e19)
            {
                continue;
            }
            cost[tdx][tdy] = cost[tx][ty] + 1;

            if(cost[tdx][tdy]==k)
                continue;
            q.push(tdx);
            q.push(tdy);
        }
    }
    cout<<ans+1<<endl;
    return 0;
}

Compiler output:
a.cc: In function 'long long int calc(int, int)':
a.cc:83:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
   83 | int main()
      |         ^~
a.cc:83:9: note: remove parentheses to default-initialize a variable
   83 | int main()
      |         ^~
      |         --
a.cc:83:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:84:1: error: a function-definition is not allowed here before '{' token
   84 | {
      | ^
a.cc:152:2: error: expected '}' at end of input
  152 | }
      |  ^
a.cc:75:1: note: to match this '{'
   75 | {
      | ^
a.cc:152:2: warning: no return statement in function returning non-void [-Wreturn-type]
  152 | }
      |  ^