#include<bits/stdc++.h> using namespace std; typedef long long ll; ll n,m,C,a[1005][1005],b[1005][1005],ans=1e18; int main(){ cin>>n>>m>>C,memset(b,0x3f,sizeof(b)); for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cin>>a[i][j]; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ b[i][j]=min(b[i-1][j],b[i][j-1]); ans=min(ans,a[i][j]+C*(i+j)+b[i][j]); b[i][j]=min(b[i][j],a[i][j]-C*i-C*j); } } memset(b,0x3f,sizeof(b)); for(int i=1;i<=n;i++){ for(int j=m;j>=1;j--){ b[i][j]=min(b[i-1][j],b[i][j+1]); ans=min(ans,a[i][j]+C*(i-j)+b[i][j]); b[i][j]=min(b[i][j],a[i][j]-C*i+C*j); } } cout<<ans; }