#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define Field(T) vector<vector<T>>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using PP = pair<int,P>;

vector<int> dx = {1,0,-1,0}, dy = {0,1,0,-1};

int inf = 1000000000;

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  rep(i,0,h) cin >> s[i];
  priority_queue<PP, vector<PP>, greater<PP>> q;
  vector<vector<int>> dist(h, vector<int>(w, inf));
  dist[0][0] = 1;
  q.push(PP(0,P(0,0)));
  while(!q.empty()) {
    PP p = q.top(); q.pop();
    int x = p.se.fi, y = p.se.se;
    rep(i,0,4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
      if (s[nx][ny] == '#') continue;
      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        q.push(PP(dist[nx][ny], P(nx, ny)));
      }
    }
  }
  if (dist[h-1][w-1] == inf) {
    cout << -1 << endl;
    return 0;
  }
  int hash = 0;
  rep(i,0,h) rep(j,0,w) if (s[i][j] == '#') hash++;
  int ans = h*w - hash - dist[h-1][w-1];
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree < pairs ,  null_type , greater<pairs>,  rb_tree_tag ,  tree_order_statistics_node_update > 
#define ll long long
#define lld long double
#define vc vector<ll>
const ll MOD=(1e9 +7);
typedef pair<ll,ll>pairs;
ll power(ll a, ll b){ll res=1;a=a%MOD;while(b>0){if(b&1){res=(res*a)%MOD;b--;}a=(a*a)%MOD;b>>=1;}
    return res;}

int main() {
	// your code goes here
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	ll t,n,i,j,k,l,c,f;
	cin>>n>>k;
	ll temp;
	vc a(n+1);
	for(i=1;i<=n;i++)cin>>a[i];
	ll ans=0;
	for(i=1;i<n;i++)
	{
	    temp=a[i]+a[i+1];
	    if(temp>k)
	    {
	        if(a[i+1]>=(temp-k))
	            a[i+1]-=(temp-k);
	        else
	            a[i+1]=0;
	        ans+=temp-k;
	    }
	}
	cout<<ans;
	return 0;
}
