#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ll H,W;
cin>>H>>W;
char mat[H+1][W+1];
for(ll i=0;i<H;i++)
{
	for(ll j=0;j<W;j++)
	{
		cin>>mat[i][j];
	}
}
ll count=0;
for(ll i=1;i<H-1;i++)
{
	for(ll j=1;j<W-1;j++)
	{
		if(mat[i][j]=='#')
		{
	     if(mat[i][j-1]=='#')
	     {
	     	if(mat[i-1][j-1]=='#'&&mat[i-1][j]=='.')
	       	 {
	     		count++;
			 }
			 if(mat[i+1][j-1]=='#'&&mat[i+1][j]=='.')
			 {
			 	count++;
			 }
		 }
		 else
		 {
		 	if(mat[i+1][j]=='.')
			 {
		 		count++;
			 }
			 if(mat[i-1][j]=='.')
			 {
		 		count++;
			 }
		 }
    	}
	}
}
for(ll i=1;i<H-1;i++)
{
	for(ll j=1;j<W-1;j++)
	{
		if(mat[i][j]=='#')
		{
	     if(mat[i-1][j]=='#')
	     {
	     	if(mat[i-1][j-1]=='#'&&mat[i][j-1]=='.')
	       	 {
	     		count++;
			 }
			 if(mat[i-1][j+1]=='#'&&mat[i][j+1]=='.')
			 {
			 	count++;
			 }
		 }
		 else
		 {
		 	if(mat[i][j+1]=='.')
			 {
		 		count++;
			 }
			 if(mat[i][j-1]=='.')
			 {
		 		count++;
			 }
		 }
    	}
	}
}
cout<<count;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define endl ("\n")
#define int long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);





int32_t main(){
    fast
    int t=1;
    // cin>>t;
    while(t--){
        int n, k;cin>>n>>k;
        vector<pair<int, int>>vv, v;
        map<int, int>mm;
        set<int>s;
        rep(i, n){
            int x, y;cin>>x>>y;
            mm[x]+=y;
            s.insert(x);
        }
        
        for(auto i:s){
            v.pb({i, mm[i]});
        }
        
        // for(auto i:v){
        //     cout<<i.ff<<" "<<i.ss<<endl;
        // }
        sort(all(v));
        int pre[v.size()+1]={0};
        int val[v.size()+1]={0};
        pre[0]=v[0].ff, val[0]=v[0].ss;
        for(int i=1;i<v.size();i++){
            pre[i]=v[i].ff+pre[i-1];
            val[i]=v[i].ss+val[i-1];
        }
        
        
        int ans=k, stop=0;;
        for(int i=0;i<v.size();i++){
            if(ans>=(v[i].ff-stop) and stop!=v[i].ff){
                ans =k+val[i];
                ans-=v[i].ff;
                stop=v[i].ff;
            }
            else if(stop==v[i].ff){
                ans+=v[i].ss;
            }
            else break;
        }
        // cout<<stop<<" "<<ans<<endl;
        cout<<stop+ans<<endl;
    }
    return 0;
}