func_code_string: #include<bits/stdc++.h>   #define PI acos(-1.0)    #define eps 1e-8   #define fi first   #define se second   #define m_p make_pair   //#define int long long   #define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)   using namespace std;   typedef long long ll;   typedef unsigned int uint;    const double E_num = 0.57721566490153286060651209;   const int maxn = 2e3 + 10;   const int N = 5e5 + 10;   const int INF = 0x3f3f3f3f;   const int mod = 1e9 + 7;         int n,m,w;   int mp[maxn][maxn];   ll dis1[maxn][maxn],dis2[maxn][maxn],ans1 = 1e18,ans2 = 1e18;   queue<pair<int,int>> q;   inline void bfs1(int x,int y)   {    #define dis dis1    memset(dis,INF,sizeof(dis));    q.push(m_p(x,y));    dis[x][y] = 0;    while(!q.empty())    {     x = q.front().fi,y = q.front().se;     q.pop();     if(dis[x - 1][y] > dis[x][y] + w && mp[x - 1][y] >= 0)     {      q.push(m_p(x - 1,y));      dis[x - 1][y] = dis[x][y] + w;     }     if(dis[x + 1][y] > dis[x][y] + w && mp[x + 1][y] >= 0)     {      q.push(m_p(x + 1,y));      dis[x + 1][y] = dis[x][y] + w;     }     if(dis[x][y - 1] > dis[x][y] + w && mp[x][y - 1] >= 0)     {      q.push(m_p(x,y - 1));      dis[x][y - 1] = dis[x][y] + w;     }     if(dis[x][y + 1] > dis[x][y] + w && mp[x][y + 1] >= 0)     {      q.push(m_p(x,y + 1));      dis[x][y + 1] = dis[x][y] + w;     }    }    #undef dis   }   inline void bfs2(int x,int y)   {    #define dis dis2    memset(dis,INF,sizeof(dis));    q.push(m_p(x,y));    dis[x][y] = 0;    while(!q.empty())    {     x = q.front().fi,y = q.front().se;     q.pop();     if(dis[x - 1][y] > dis[x][y] + w && mp[x - 1][y] >= 0)     {      q.push(m_p(x - 1,y));      dis[x - 1][y] = dis[x][y] + w;     }     if(dis[x + 1][y] > dis[x][y] + w && mp[x + 1][y] >= 0)     {      q.push(m_p(x + 1,y));      dis[x + 1][y] = dis[x][y] + w;     }     if(dis[x][y - 1] > dis[x][y] + w && mp[x][y - 1] >= 0)     {      q.push(m_p(x,y - 1));      dis[x][y - 1] = dis[x][y] + w;     }     if(dis[x][y + 1] > dis[x][y] + w && mp[x][y + 1] >= 0)     {      q.push(m_p(x,y + 1));      dis[x][y + 1] = dis[x][y] + w;     }    }    #undef dis   }   int main()   {    IOS;    cin >> n >> m >> w;    for(int i = 0;i <= m + 1; ++i)    {     mp[0][i] = mp[n + 1][i] = -1;    }    for(int i = 1;i <= n + 1; ++i)    {     mp[i][0] = mp[i][m + 1] = -1;    }    for(int i = 1;i <= n; ++i)    {     for(int j = 1;j <= m; ++j)      {      cin >> mp[i][j];     }    }    bfs1(1,1);    bfs2(n,m);    for(int i = 1;i <= n; ++i)    {     for(int j = 1;j <= m; ++j)     {      if(mp[i][j] > 0) ans1 = min(ans1,dis1[i][j] + mp[i][j]);     }    }    for(int i = 1;i <= n; ++i)    {     for(int j = 1;j <= m; ++j)     {      if(mp[i][j] > 0) ans2 = min(ans2,dis2[i][j] + mp[i][j]);     }    }    ans1 = min(dis1[n][m] ,ans1 + ans2);    if(ans1 < 1e18) cout << ans1 << endl;    else cout << -1 << endl;    return 0;   }   