Language: C++

#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required

using namespace __gnu_pbds; 
using namespace std;

template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; 
typedef long long  ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll,ll> PLL;
typedef pair<int,int> PII;
#define pb push_back

#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(),a.end()
#define fr(i,x,y) for(int i=x;i<y;i++)
#define frr(i,x,y) for(int i=x-1;i>=y;i--)
#define inf 1e18+1
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int mod=1000000007;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll power(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;}a=a*a;b>>=1;}return ans;}
ll powerm(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;ans%=mod;}a=a*a;a%=mod;b>>=1;}return ans%mod;}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
ll r,c;

vector<string> arr;
vector<VL> visit;
void dfs(ll i,ll j)
{
    if(i+1<r)
    {
        if((visit[i][j]+1<visit[i+1][j])&&arr[i+1][j]=='.')
        {
            visit[i+1][j]=visit[i][j]+1;
            dfs(i+1,j);
        }
    }
    if(i-1>=0)
    {
        if((visit[i][j]+1<visit[i-1][j])&&arr[i-1][j]=='.')
        {
            visit[i-1][j]=visit[i][j]+1;
            dfs(i-1,j);
        }
    }
    if(j+1<c)
    {
        if((visit[i][j]+1<visit[i][j+1])&&arr[i][j+1]=='.')
        {
            visit[i][j+1]=visit[i][j]+1;
            dfs(i,j+1);
        }
    }
    if(j-1>=0)
    {
        if((visit[i][j]+1<visit[i][j-1])&&arr[i][j-1]=='.')
        {
            visit[i][j-1]=visit[i][j]+1;
            dfs(i,j-1);
        }
    }
    return;
}
void solve()
{
    cin>>r>>c;
    //arr.assign(r,"");
    arr=vector<string>(r,"");
    fr(i,0,r)
    {
        cin>>arr[i];
    }
    ll ct=0;
    fr(i,0,r)
    {
        fr(j,0,c)
        {
            if(arr[i][j]=='.')
            {
                ct++;
            }
        }
    }
    visit=vector<VL>(r,VL(c,inf));
    visit[0][0]=1;
    if(arr[0][0]=='#'||arr[r-1][c-1]=='#')
    {
        cout<<"-1\n";
        return;
    }
    dfs(0,0);
    if(visit[r-1][c-1]==inf)
    {
        cout<<"-1\n";
    }
    else
    {
        cout<<ct-visit[r-1][c-1]<<endl;
    }
    return;
}
int main()
{
    IOS;
    ll t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
*/
// BITMASK:
// 1)When some constrall is of the order of 15-20, think of bitmask DP.
// 2)When some constrall is around 40, try out meet in the middle
// 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithm

Compiler output:
a.cc: In function 'void dfs(ll, ll)':
a.cc:41:13: error: reference to 'visit' is ambiguous
   41 |         if((visit[i][j]+1<visit[i+1][j])&&arr[i+1][j]=='.')
      |             ^~~~~
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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:41:27: error: reference to 'visit' is ambiguous
   41 |         if((visit[i][j]+1<visit[i+1][j])&&arr[i+1][j]=='.')
      |                           ^~~~~
/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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:43:13: error: reference to 'visit' is ambiguous
   43 |             visit[i+1][j]=visit[i][j]+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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:43:27: error: reference to 'visit' is ambiguous
   43 |             visit[i+1][j]=visit[i][j]+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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:49:13: error: reference to 'visit' is ambiguous
   49 |         if((visit[i][j]+1<visit[i-1][j])&&arr[i-1][j]=='.')
      |             ^~~~~
/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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:49:27: error: reference to 'visit' is ambiguous
   49 |         if((visit[i][j]+1<visit[i-1][j])&&arr[i-1][j]=='.')
      |                           ^~~~~
/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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:51:13: error: reference to 'visit' is ambiguous
   51 |             visit[i-1][j]=visit[i][j]+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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:51:27: error: reference to 'visit' is ambiguous
   51 |             visit[i-1][j]=visit[i][j]+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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:57:13: error: reference to 'visit' is ambiguous
   57 |         if((visit[i][j]+1<visit[i][j+1])&&arr[i][j+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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:57:27: error: reference to 'visit' is ambiguous
   57 |         if((visit[i][j]+1<visit[i][j+1])&&arr[i][j+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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:59:13: error: reference to 'visit' is ambiguous
   59 |             visit[i][j+1]=visit[i][j]+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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:59:27: error: reference to 'visit' is ambiguous
   59 |             visit[i][j+1]=visit[i][j]+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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:65:13: error: reference to 'visit' is ambiguous
   65 |         if((visit[i][j]+1<visit[i][j-1])&&arr[i][j-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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:65:27: error: reference to 'visit' is ambiguous
   65 |         if((visit[i][j]+1<visit[i][j-1])&&arr[i][j-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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:67:13: error: reference to 'visit' is ambiguous
   67 |             visit[i][j-1]=visit[i][j]+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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:67:27: error: reference to 'visit' is ambiguous
   67 |             visit[i][j-1]=visit[i][j]+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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc: In function 'void solve()':
a.cc:93:5: error: reference to 'visit' is ambiguous
   93 |     visit=vector<VL>(r,VL(c,inf));
      |     ^~~~~
/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:36:12: note:                 'std::vector<std::vector<long long int> > visit'
   36 | vector<VL> visit;
      |            ^~~~~
a.cc:94:5: error: reference to 'visit' is ambiguous
   94 |     visit[0][0]=1;
      |     ^~~~~
/usr/incl