/*
	Description : Graph sheep , wolf and dog
	Date :	13-10-19 11:58
*/

#include<bits/stdc++.h>

#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

typedef long long ll;
#define INF (1<<29)
#define LINF (1ll<<60)

typedef long long int lli;
using namespace std;

void desperate_optimization(bool submitting=true,int precision=10)
{
  if(!submitting) return;
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(precision);
}

int main()
{
    desperate_optimization(false);
	int n,i,j,count=0,temp,flag1=0,flag2=0;

	int m;
	cin>>n>>m;

	char grid[505][505];

	for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
			cin>>grid[i][j];

	for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
		{
			if(grid[i][j]=='S')
			{
				if(grid[i-1][j]=='W'||grid[i+1][j]=='W' ||grid[i][j+1]=='W' ||grid[i][j-1]=='W')
				{
					flag1=1;
					break;
				}
			}
		}

	if(flag1)
		return cout<<"NO",0;

	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
			if(grid[i][j]=='.')
				grid[i][j]='D';

	cout<<"YES\n";

	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			cout<<grid[i][j];
		}
		cout<<"\n";
	}

	int do_not_hack;

	return 0;
}


/*
	Description : Graph sheep , wolf and dog
	Date :	13-10-19 11:58
*/

#include<bits/stdc++.h>

#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

typedef long long ll;
#define INF (1<<29)
#define LINF (1ll<<60)

typedef long long int lli;
using namespace std;

void desperate_optimization(bool submitting=true,int precision=10)
{
  if(!submitting) return;
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(precision);
}

int main()
{
    desperate_optimization(false);
	int n,i,j,count=0,temp,flag1=0,flag2=0;

	int m;
	cin>>n>>m;

	char grid[505][505];

	for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
			cin>>grid[i][j];

	for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
		{
			if(grid[i][j]=='S')
			{
				if(grid[i-1][j]=='W'||grid[i+1][j]=='W' ||grid[i][j+1]=='W' ||grid[i][j-1]=='W')
				{
					flag1=1;
					break;
				}
			}
		}

	if(flag1)
		return cout<<"NO",0;

	for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
			if(grid[i][j]=='.')
				grid[i][j]='D';

	cout<<"YES\n";

	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			cout<<grid[i][j];
		}
		cout<<"\n";
	}

	int do_not_hack;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main () {
  int r,c; cin>>r>>c;
  char field [r][c];
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) {
      cin>>field[i][j];
      if (field[i][j]=='.') field[i][j]='D';
    }
  }
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) {
      if (field[i][j]=='S') {
        if ((i>0 && field[i-1][j]=='W') || (i<r-1 && field[i+1][j]=='W') || (j>0 && field[i][j-1]=='W') || (j<c-1 && field[i][j+1]=='W')) {
          cout<<"No";
          return 0;
        }
      }
    }
  }
  cout<<"Yes"<<"\n";
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) cout<<field[i][j];
    cout<<"\n";
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main () {
  int r,c; cin>>r>>c;
  char field [r][c];
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) cin>>field[r][c];
  }
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) {
      if (field[r][c]=='S') {
        if (j>0) {
          if (field[r][j-1]=='W') {
            cout<<"No";
            return 0;
          }
          else if (field[r][j-1]=='.') field[r][j-1]='D';
        }
        if (j<c-1) {
          if (field[r][j+1]=='W') {
            cout<<"No";
            return 0;
          }
          else if (field[r][j+1]=='.') field[r][j+1]='D';
        }
      }
    }
  }
  for (int j=0;j<c;++j) {
    for (int i=0;i<r-1;++i) {
      if ((field[i][j]=='S' && field[i+1][j]=='W') || (field[i][j]=='W' && field[i+1][j]=='S')) {
        cout<<"No";
        return 0;
      }
    }
  }
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++i) cout<<field[i][j];
    cout<<endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main () {
  int r,c; cin>>r>>c;
  char field [r][c];
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) cin>>field[i][j];
  }
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) {
      if (field[i][j]=='S') {
        if (j>0) {
          if (field[i][j-1]=='W') {
            cout<<"No";
            return 0;
          }
          else if (field[i][j-1]=='.') field[i][j-1]='D';
        }
        if (j<c-1) {
          if (field[i][j+1]=='W') {
            cout<<"No";
            return 0;
          }
          else if (field[i][j+1]=='.') field[i][j+1]='D';
        }
      }
    }
  }
  for (int j=0;j<c;++j) {
    for (int i=0;i<r-1;++i) {
      if ((field[i][j]=='S' && field[i+1][j]=='W') || (field[i][j]=='W' && field[i+1][j]=='S')) {
        cout<<"No";
        return 0;
      }
    }
  }
  cout<<"Yes"<<"\n";
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) cout<<field[i][j];
    cout<<"\n";
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main () {
  int r,c; cin>>r>>c;
  char field [r][c];
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) cin>>field[i][j];
  }
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) {
      if (field[i][j]=='.') field[i][j]='D';
    }
  }
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) {
      if (field[i][j]=='S') {
        if ((i>0 && field[i-1][j]=='W') || (i<r-1 && field[i+1][j]=='W') || (j>0 && field[i][j-1]=='W') || (j<c-1 && field[i][j+1]=='W')) {
          cout<<"No";
          return 0;
        }
      }
    }
  }
  cout<<"Yes"<<"\n";
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) cout<<field[i][j];
    cout<<"\n";
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main () {
  int r,c; cin>>r>>c;
  char field [r][c];
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) {
      cin>>field[i][j];
      if (field[i][j]=='.') field[i][j]='D';
  }
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) {
      if (field[i][j]=='S') {
        if ((i>0 && field[i-1][j]=='W') || (i<r-1 && field[i+1][j]=='W') || (j>0 && field[i][j-1]=='W') || (j<c-1 && field[i][j+1]=='W')) {
          cout<<"No";
          return 0;
        }
      }
    }
  }
  cout<<"Yes"<<"\n";
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) cout<<field[i][j];
    cout<<"\n";
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main () {
  int r,c; cin>>r>>c;
  char field [r][c];
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) cin>>field[i][j];
  }
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++j) {
      if (field[i][j]=='S') {
        if (j>0) {
          if (field[i][j-1]=='W') {
            cout<<"No";
            return 0;
          }
          else if (field[i][j-1]=='.') field[i][j-1]='D';
        }
        if (j<c-1) {
          if (field[i][j+1]=='W') {
            cout<<"No";
            return 0;
          }
          else if (field[i][j+1]=='.') field[i][j+1]='D';
        }
      }
    }
  }
  for (int j=0;j<c;++j) {
    for (int i=0;i<r-1;++i) {
      if ((field[i][j]=='S' && field[i+1][j]=='W') || (field[i][j]=='W' && field[i+1][j]=='S')) {
        cout<<"No";
        return 0;
      }
    }
  }
  for (int i=0;i<r;++i) {
    for (int j=0;j<c;++i) cout<<field[i][j];
    cout<<endl;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

//#define cin fi
//#define cout fo

//ifstream fi (".INP");
//ofstream fo (".INP");

int main(){
//	freopen("test.txt" , "r" , stdin);
//	ios_base:: sync_with_stdio(false);
//	cin.tie(0);
//	cout.tie(0);
	int n, m;
	cin >> n >> m;
	char s[n+3][m+3];
	for (int i = 1; i <= n ; i++){
		for (int j = 1; j <= m ; j++){
			cin >> s[i][j];
		}
	}int res = 0;
	for (int i = 1; i <= n ; i++){
		for (int j = 1; j <= m ; j++){
			if(s[i][j] == 'S' ) {
				if ((s[i-1][j] == 'W' || s[i][j-1] == 'W' ||s[i+1][j] == 'W' ||s[i][j+1] == 'W' )) 	{
					res = 1;
					break;
				}
			}
			else if (s[i][j] != 'W') {
				s[i][j] = 'D';
			}
		}
	}
	if (res == 1) cout << "No";
	else {
		cout << "Yes\n" ;
		for (int i = 1 ; i <= n ;i++){
			for (int j = 1; j <= m ; j++){
				cout << s[i][j];
			}
			cout << "\n";
		}
	}

	return 0;
}


//2-8-20
#pragma GCC optimize ("-O2")
#define _CRT_SECURE_NO_WARNINGS 1

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define scu(n)                            scanf("%u",&n)
#define scl(n)                            scanf("%d",&n)
#define scll(n)                           scanf("%lld",&n)
#define sz(a)                             (int)a.size()
#define maxN                              10000005
#define uu                                first
#define vv                                second
#define ll                                long long
#define U                                 unsigned
#define pb                                emplace_back
#define pf                                emplace_front
#define mp                                make_pair
#define pii                               pair<int,int>
#define vi                                vector<int>
#define mii                               map<int,int>
#define Pi                                acos(-1.0)
#define endl                              "\n"
#define isOn(S, j)                        (S & (1 << j))
#define setBit(S, j)                      (S |= (1 << j))
#define clearBit(S, j)                    (S &= ~(1 << j))
#define toggleBit(S, j)                   (S ^= (1 << j))
#define lowBit(S)                         (S & (-S))
#define setAll(S, n)                      (S = (1 << n) - 1)
#define isPowerOfTwo(S)                   (!(S & (S - 1)))
#define nearestPowerOfTwo(S)              ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S)                 ((S) & (S - 1))
#define turnOnLastZero(S)                 ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S)     ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S)    ((S) | (S - 1))
#define _fastio                              ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod                               1000000007
#define inf                               1e18
#define EPS                               1e-9
#define ps(x,y)                           fixed<<setprecision(y)<<x
#define mk(arr,n,type)                    type *arr=new type[n];
#define TEST                              int tc;cin>>tc; while(tc--)
#define mem(arr)                          memset(arr,false,sizeof(arr)

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

//Vector Macros
#define all(v)                            v.begin(),v.end()
#define unq(v)                            v.erase(unique(all(v)),v.end())
#define MIN(c)                            *min_element(all(c))
#define MAX(c)                            *max_element(all(c))
#define SUM(c)                            accumulate(all(c),0)

//Loop Macros
#define For(i,a,b)                        for(ll i=a;i<=b;i++)
#define rep(i,a,b)                        for(int i=a;i<=b;i++)
#define fr(i,n)                           for(int i=0;i<n;i++)

//Debug Macros
#define open                              freopen("input.txt","r",stdin);
#define close                             freopen("output.txt","w",stdout);
#define deb(a)                            cout << #a << " : " << a << " ";
#define debln(a)                          cout<< #a <<" : " << a << endl;
//Print Macros
#define print(v)  for(auto x : v)         {  cout<<x<<" ";   }  cout<<endl;
#define PRINT(v)  for(auto x : v)         { cout<<x.uu<<" "<<x.vv<<endl;}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
    cout << name << " : " << arg1 << endl;
    //use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " : " << arg1 << " | ";
    __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

void solve()
{
    int r,c;
    cin>>r>>c;

    string s[r];
    fr(i,r)
    cin>>s[i];

    for(int i=0; i<r; i++)
    {
        for(int j=0; j<c; j++)
        {
            if(s[i][j]=='S')
            {
                if(s[i-1][j]=='.')
                    s[i-1][j]='D' ;
                if(s[i][j-1]=='.')
                    s[i][j-1]='D' ;
                if(s[i][j+1]=='.')
                    s[i][j+1]='D' ;
                if(s[i+1][j]=='.')
                    s[i+1][j]='D';
                else if(s[i-1][j]=='S' || s[i][j-1]=='S' ||s[i][j+1]=='S'||s[i+1][j]=='S')
                    continue;
                else if(s[i-1][j]=='W' || s[i][j-1]=='W' ||s[i][j+1]=='W'||s[i+1][j]=='W')
                {
                    puts("No");
                    return;
                }
            }
        }
    }

    puts("Yes");
    fr(i,r)
    cout<<s[i]<<endl;

}

int main()
{
    _fastio

    solve();

    return 0;
}


//2-8-20
#pragma GCC optimize ("-O2")
#define _CRT_SECURE_NO_WARNINGS 1

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define scu(n)                            scanf("%u",&n)
#define scl(n)                            scanf("%d",&n)
#define scll(n)                           scanf("%lld",&n)
#define sz(a)                             (int)a.size()
#define maxN                              10000005
#define uu                                first
#define vv                                second
#define ll                                long long
#define U                                 unsigned
#define pb                                emplace_back
#define pf                                emplace_front
#define mp                                make_pair
#define pii                               pair<int,int>
#define vi                                vector<int>
#define mii                               map<int,int>
#define Pi                                acos(-1.0)
#define endl                              "\n"
#define isOn(S, j)                        (S & (1 << j))
#define setBit(S, j)                      (S |= (1 << j))
#define clearBit(S, j)                    (S &= ~(1 << j))
#define toggleBit(S, j)                   (S ^= (1 << j))
#define lowBit(S)                         (S & (-S))
#define setAll(S, n)                      (S = (1 << n) - 1)
#define isPowerOfTwo(S)                   (!(S & (S - 1)))
#define nearestPowerOfTwo(S)              ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S)                 ((S) & (S - 1))
#define turnOnLastZero(S)                 ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S)     ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S)    ((S) | (S - 1))
#define _fastio                              ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod                               1000000007
#define inf                               1e18
#define EPS                               1e-9
#define ps(x,y)                           fixed<<setprecision(y)<<x
#define mk(arr,n,type)                    type *arr=new type[n];
#define TEST                              int tc;cin>>tc; while(tc--)
#define mem(arr)                          memset(arr,false,sizeof(arr)

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

//Vector Macros
#define all(v)                            v.begin(),v.end()
#define unq(v)                            v.erase(unique(all(v)),v.end())
#define MIN(c)                            *min_element(all(c))
#define MAX(c)                            *max_element(all(c))
#define SUM(c)                            accumulate(all(c),0)

//Loop Macros
#define For(i,a,b)                        for(ll i=a;i<=b;i++)
#define rep(i,a,b)                        for(int i=a;i<=b;i++)
#define fr(i,n)                           for(int i=0;i<n;i++)

//Debug Macros
#define open                              freopen("input.txt","r",stdin);
#define close                             freopen("output.txt","w",stdout);
#define deb(a)                            cout << #a << " : " << a << " ";
#define debln(a)                          cout<< #a <<" : " << a << endl;
//Print Macros
#define print(v)  for(auto x : v)         {  cout<<x<<" ";   }  cout<<endl;
#define PRINT(v)  for(auto x : v)         { cout<<x.uu<<" "<<x.vv<<endl;}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
    cout << name << " : " << arg1 << endl;
    //use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " : " << arg1 << " | ";
    __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

void solve()
{
    int r,c;
    cin>>r>>c;

    string s[r];
    fr(i,r)
    cin>>s[i];

    for(int i=0; i<r; i++)
    {
        for(int j=0; j<c; j++)
        {
            if(s[i][j]=='S')
            {
                if(s[i-1][j]=='.')
                    s[i-1][j]='D' ;
                if(s[i][j-1]=='.')
                    s[i][j-1]='D' ;
                if(s[i][j+1]=='.')
                    s[i][j+1]='D' ;
                if(s[i+1][j]=='.')
                    s[i+1][j]='D';
                else if(s[i-1][j]=='S' || s[i][j-1]=='S' ||s[i][j+1]=='S'||s[i+1][j]=='S')
                    continue;
                else if(s[i-1][j]=='W' || s[i][j-1]=='W' ||s[i][j+1]=='W'||s[i+1][j]=='W')
                {
                    puts("No");
                    return;
                }
            }
        }
    }

    puts("Yes");
    fr(i,r)
    cout<<s[i]<<endl;

}

int main()
{
    // _fastio

    solve();

    return 0;
}


//2-8-20
#pragma GCC optimize ("-O2")
#define _CRT_SECURE_NO_WARNINGS 1

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define scu(n)                            scanf("%u",&n)
#define scl(n)                            scanf("%d",&n)
#define scll(n)                           scanf("%lld",&n)
#define sz(a)                             (int)a.size()
#define maxN                              10000005
#define uu                                first
#define vv                                second
#define ll                                long long
#define U                                 unsigned
#define pb                                emplace_back
#define pf                                emplace_front
#define mp                                make_pair
#define pii                               pair<int,int>
#define vi                                vector<int>
#define mii                               map<int,int>
#define Pi                                acos(-1.0)
#define endl                              "\n"
#define isOn(S, j)                        (S & (1 << j))
#define setBit(S, j)                      (S |= (1 << j))
#define clearBit(S, j)                    (S &= ~(1 << j))
#define toggleBit(S, j)                   (S ^= (1 << j))
#define lowBit(S)                         (S & (-S))
#define setAll(S, n)                      (S = (1 << n) - 1)
#define isPowerOfTwo(S)                   (!(S & (S - 1)))
#define nearestPowerOfTwo(S)              ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S)                 ((S) & (S - 1))
#define turnOnLastZero(S)                 ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S)     ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S)    ((S) | (S - 1))
#define _fastio                              ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod                               1000000007
#define inf                               1e18
#define EPS                               1e-9
#define ps(x,y)                           fixed<<setprecision(y)<<x
#define mk(arr,n,type)                    type *arr=new type[n];
#define TEST                              int tc;cin>>tc; while(tc--)
#define mem(arr)                          memset(arr,false,sizeof(arr)

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

//Vector Macros
#define all(v)                            v.begin(),v.end()
#define unq(v)                            v.erase(unique(all(v)),v.end())
#define MIN(c)                            *min_element(all(c))
#define MAX(c)                            *max_element(all(c))
#define SUM(c)                            accumulate(all(c),0)

//Loop Macros
#define For(i,a,b)                        for(ll i=a;i<=b;i++)
#define rep(i,a,b)                        for(int i=a;i<=b;i++)
#define fr(i,n)                           for(int i=0;i<n;i++)

//Debug Macros
#define open                              freopen("input.txt","r",stdin);
#define close                             freopen("output.txt","w",stdout);
#define deb(a)                            cout << #a << " : " << a << " ";
#define debln(a)                          cout<< #a <<" : " << a << endl;
//Print Macros
#define print(v)  for(auto x : v)         {  cout<<x<<" ";   }  cout<<endl;
#define PRINT(v)  for(auto x : v)         { cout<<x.uu<<" "<<x.vv<<endl;}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
    cout << name << " : " << arg1 << endl;
    //use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " : " << arg1 << " | ";
    __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

void solve()
{
    int r,c;
    cin>>r>>c;

    char s[600][600];
    fr(i,r)
    {
        fr(j,c)
        cin>>s[i][j];
    }

    for(int i=0; i<r; i++)
    {
        for(int j=0; j<c; j++)
        {
            if(s[i][j]=='S')
            {
                if(s[i-1][j]=='.')
                    s[i-1][j]='D' ;
                if(s[i][j-1]=='.')
                    s[i][j-1]='D' ;
                if(s[i][j+1]=='.')
                    s[i][j+1]='D' ;
                if(s[i+1][j]=='.')
                    s[i+1][j]='D';
                else if(s[i-1][j]=='S' || s[i][j-1]=='S' ||s[i][j+1]=='S'||s[i+1][j]=='S')
                    continue;
                else if(s[i-1][j]=='W' || s[i][j-1]=='W' ||s[i][j+1]=='W'||s[i+1][j]=='W')
                {
                    puts("No");
                    return;
                }
            }
        }
    }

    puts("Yes");
    fr(i,r)
    {
        fr(j,c)
        cout<<s[i][j];
        cout<<endl;
    }
    cout<<endl;

}

int main()
{
    // _fastio

    solve();

    return 0;
}


//2-8-20
#pragma GCC optimize ("-O2")
#define _CRT_SECURE_NO_WARNINGS 1

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define scu(n)                            scanf("%u",&n)
#define scl(n)                            scanf("%d",&n)
#define scll(n)                           scanf("%lld",&n)
#define sz(a)                             (int)a.size()
#define maxN                              10000005
#define uu                                first
#define vv                                second
#define ll                                long long
#define U                                 unsigned
#define pb                                emplace_back
#define pf                                emplace_front
#define mp                                make_pair
#define pii                               pair<int,int>
#define vi                                vector<int>
#define mii                               map<int,int>
#define Pi                                acos(-1.0)
#define endl                              "\n"
#define isOn(S, j)                        (S & (1 << j))
#define setBit(S, j)                      (S |= (1 << j))
#define clearBit(S, j)                    (S &= ~(1 << j))
#define toggleBit(S, j)                   (S ^= (1 << j))
#define lowBit(S)                         (S & (-S))
#define setAll(S, n)                      (S = (1 << n) - 1)
#define isPowerOfTwo(S)                   (!(S & (S - 1)))
#define nearestPowerOfTwo(S)              ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S)                 ((S) & (S - 1))
#define turnOnLastZero(S)                 ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S)     ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S)    ((S) | (S - 1))
#define _fastio                              ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod                               1000000007
#define inf                               1e18
#define EPS                               1e-9
#define ps(x,y)                           fixed<<setprecision(y)<<x
#define mk(arr,n,type)                    type *arr=new type[n];
#define TEST                              int tc;cin>>tc; while(tc--)
#define mem(arr)                          memset(arr,false,sizeof(arr)

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

//Vector Macros
#define all(v)                            v.begin(),v.end()
#define unq(v)                            v.erase(unique(all(v)),v.end())
#define MIN(c)                            *min_element(all(c))
#define MAX(c)                            *max_element(all(c))
#define SUM(c)                            accumulate(all(c),0)

//Loop Macros
#define For(i,a,b)                        for(ll i=a;i<=b;i++)
#define rep(i,a,b)                        for(int i=a;i<=b;i++)
#define fr(i,n)                           for(int i=0;i<n;i++)

//Debug Macros
#define open                              freopen("input.txt","r",stdin);
#define close                             freopen("output.txt","w",stdout);
#define deb(a)                            cout << #a << " : " << a << " ";
#define debln(a)                          cout<< #a <<" : " << a << endl;
//Print Macros
#define print(v)  for(auto x : v)         {  cout<<x<<" ";   }  cout<<endl;
#define PRINT(v)  for(auto x : v)         { cout<<x.uu<<" "<<x.vv<<endl;}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
    cout << name << " : " << arg1 << endl;
    //use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " : " << arg1 << " | ";
    __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

void solve()
{
    int r,c;
    cin>>r>>c;

    char s[600][600];
    fr(i,r)
    {
        fr(j,c)
        cin>>s[i][j];
    }

    for(int i=0; i<r; i++)
    {
        for(int j=0; j<c; j++)
        {
            if(s[i][j]=='S')
            {
                if(s[i-1][j]=='.')
                    s[i-1][j]='D' ;
                if(s[i][j-1]=='.')
                    s[i][j-1]='D' ;
                if(s[i][j+1]=='.')
                    s[i][j+1]='D' ;
                if(s[i+1][j]=='.')
                    s[i+1][j]='D';
                else if(s[i-1][j]=='S' || s[i][j-1]=='S' ||s[i][j+1]=='S'||s[i+1][j]=='S')
                    continue;
                else if(s[i-1][j]=='W' || s[i][j-1]=='W' ||s[i][j+1]=='W'||s[i+1][j]=='W')
                {
                    puts("No");
                    return;
                }
            }

            else if(s[i][j]=='W')
            {
                if(s[i-1][j]=='S' || s[i][j-1]=='S' || s[i][j+1]=='S'||s[i+1][j]=='S')
                {
                    puts("No");
                    return;
                }
            }
        }
    }

    puts("Yes");
    fr(i,r)
    {
        fr(j,c)
        cout<<s[i][j];
        cout<<endl;
    }
    cout<<endl;

}

int main()
{
    // _fastio

    solve();

    return 0;
}


//1-8-20
#pragma GCC optimize ("-O2")
#define _CRT_SECURE_NO_WARNINGS 1

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define scu(n)                            scanf("%u",&n)
#define scl(n)                            scanf("%d",&n)
#define scll(n)                           scanf("%lld",&n)
#define sz(a)                             (int)a.size()
#define maxN                              10000005
#define uu                                first
#define vv                                second
#define ll                                long long
#define U                                 unsigned
#define pb                                emplace_back
#define pf                                emplace_front
#define mp                                make_pair
#define pii                               pair<int,int>
#define vi                                vector<int>
#define mii                               map<int,int>
#define Pi                                acos(-1.0)
#define endl                              "\n"
#define isOn(S, j)                        (S & (1 << j))
#define setBit(S, j)                      (S |= (1 << j))
#define clearBit(S, j)                    (S &= ~(1 << j))
#define toggleBit(S, j)                   (S ^= (1 << j))
#define lowBit(S)                         (S & (-S))
#define setAll(S, n)                      (S = (1 << n) - 1)
#define isPowerOfTwo(S)                   (!(S & (S - 1)))
#define nearestPowerOfTwo(S)              ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S)                 ((S) & (S - 1))
#define turnOnLastZero(S)                 ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S)     ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S)    ((S) | (S - 1))
#define _fastio                              ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod                               1000000007
#define inf                               1e18
#define EPS                               1e-9
#define ps(x,y)                           fixed<<setprecision(y)<<x
#define mk(arr,n,type)                    type *arr=new type[n];
#define TEST                              int tc;cin>>tc; while(tc--)
#define mem(arr)                          memset(arr,false,sizeof(arr)

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;//Vector Macros

//Vector Macros
#define all(v)                            v.begin(),v.end()
#define unq(v)                            v.erase(unique(all(v)),v.end())
#define MIN(c)                            *min_element(all(c))
#define MAX(c)                            *max_element(all(c))
#define SUM(c)                            accumulate(all(c),0)

//Loop Macros
#define For(i,a,b)                        for(ll i=a;i<=b;i++)
#define rep(i,a,b)                        for(int i=a;i<=b;i++)
#define fr(i,n)                           for(int i=0;i<n;i++)

//Debug Macros
#define open                              freopen("input.txt","r",stdin);
#define close                             freopen("output.txt","w",stdout);
#define deb(a)                            cout << #a << " : " << a << " ";
#define debln(a)                          cout<< #a <<" : " << a << endl;
//Print Macros
#define print(v)  for(auto x : v)         {  cout<<x<<" ";   }  cout<<endl;
#define PRINT(v)  for(auto x : v)         { cout<<x.uu<<" "<<x.vv<<endl;}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
    cout << name << " : " << arg1 << endl;
    //use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " : " << arg1 << " | ";
    __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

void solve()
{
    int n,k,z,a[1000000];
    cin>>n>>k>>z;

    fr(i,n)
    cin>>a[i];

    int ans = 0;
    for(int left=0; left<=z; ++left)
    {
        int border = k - 2*left;
        int sum = 0,mxLeft = 0;

        for(int i=0; i<=border; ++i)
        {
            sum += a[i];
            mxLeft = ma
        }
    }

}

int main()
{
    _fastio

    solve();

    return 0;
}


#include<iostream>
#include<vector>
#include<string>

using namespace std;

int n,m,sh,w;
string res;
vector<vector<char>>a;

bool IF(int i, int j)
{
    bool r = true;
    if(i+1<n)
        if(a[i+1][j]=='W')
            r = false;
    if(j+1<m)
        if(a[i][j+1]=='W')
            r = false;
    if(i-1>=0)
        if(a[i-1][j]=='W')
            r = false;
    if(j-1>=0)
        if(a[i][j-1]=='W')
            r = false;
    return r;
}

int main()
{
    res = "No";
    cin >> n >> m;
    a.resize(n);
    string s = "";
    for(int i = 0; i<n; i++)
        a[i].resize(m);
    for(int i = 0; i<n; i++)
    {
        cin >> s;
        for(int j = 0; j<m; j++)
        {
            a[i][j]=s[j];
            if(s[j]=='S')sh++;
            if(s[j]=='W')w++;
        }
    }
    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            if(a[i][j]=='S')
                if(IF(i,j))
                {
                    res = "Yes";
                }
        }
    }
    cout << res << '\n';
    if(sh!=0 && w!=0 && res=="Yes")
    {
        for(int i = 0; i<n; i++)
        {
            for(int j = 0; j<m; j++)
            {
                if(a[i][j]=='S')
                {
                    if(i+1<n)
                        a[i+1][j]='D';
                    if(j+1<m)
                        a[i][j+1]='D';
                    if(i-1>=0)
                        a[i-1][j]='D';
                    if(j-1>=0)
                        a[i][j-1]='D';
                }
            }
        }
    }
    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            cout << a[i][j];
        }
        cout << '\n';
    }
}


#include<iostream>
#include<vector>
#include<string>

using namespace std;

int n,m,sh,w;
string res;
vector<vector<char>>a;

bool IF(int i, int j)
{
    bool r = true;
    if(i+1<n)
        if(a[i+1][j]=='W')
            r = false;
    if(j+1<m)
        if(a[i][j+1]=='W')
            r = false;
    if(i-1>=0)
        if(a[i-1][j]=='W')
            r = false;
    if(j-1>=0)
        if(a[i][j-1]=='W')
            r = false;
    return r;
}

int main()
{
    res = "Yes";
    cin >> n >> m;
    a.resize(n);
    string s = "";
    for(int i = 0; i<n; i++)
        a[i].resize(m);
    for(int i = 0; i<n; i++)
    {
        cin >> s;
        for(int j = 0; j<m; j++)
        {
            a[i][j]=s[j];
            if(s[j]=='S')sh++;
            if(s[j]=='W')w++;
        }
    }
    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            if(a[i][j]=='S')
                if(!IF(i,j))
                {
                    res = "No";
                }
        }
    }
    cout << res << '\n';
    if(sh!=0 && w!=0 && res=="Yes")
    {
        for(int i = 0; i<n; i++)
        {
            for(int j = 0; j<m; j++)
            {
                /*if(a[i][j]=='S')
                {
                    if(i+1<n)
                        a[i+1][j]='D';
                    if(j+1<m)
                        a[i][j+1]='D';
                    if(i-1>=0)
                        a[i-1][j]='D';
                    if(j-1>=0)
                        a[i][j-1]='D';
                }*/
                if(a[i][j]=='.')
                    a[i][j]='D';
            }
        }
    }
    if(res!="No")
    {
        for(int i = 0; i<n; i++)
        {
            for(int j = 0; j<m; j++)
            {
                cout << a[i][j];
            }
            cout << '\n';
        }
    }
}


#include<iostream>
#include<vector>
#include<string>

using namespace std;

int n,m,sh,w;
string res;
vector<vector<char>>a;

bool IF(int i, int j)
{
    bool r = true;
    if(i+1<n)
        if(a[i+1][j]=='W')
            r = false;
    if(j+1<m)
        if(a[i][j+1]=='W')
            r = false;
    if(i-1>=0)
        if(a[i-1][j]=='W')
            r = false;
    if(j-1>=0)
        if(a[i][j-1]=='W')
            r = false;
    return r;
}

int main()
{
    res = "No";
    cin >> n >> m;
    a.resize(n);
    string s = "";
    for(int i = 0; i<n; i++)
        a[i].resize(m);
    for(int i = 0; i<n; i++)
    {
        cin >> s;
        for(int j = 0; j<m; j++)
        {
            a[i][j]=s[j];
            if(s[j]=='S')sh++;
            if(s[j]=='W')w++;
        }
    }
    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            if(a[i][j]=='S')
                if(IF(i,j))
                {
                    res = "Yes";
                }
        }
    }
    cout << res;
    if(sh!=0 && w!=0 && res=="Yes")
    {
        for(int i = 0; i<n; i++)
        {
            for(int j = 0; j<m; j++)
            {
                if(a[i][j]=='S')
                {
                    if(i+1<n)
                        a[i+1][j]='D';
                    if(j+1<m)
                        a[i][j+1]='D';
                    if(i-1>=0)
                        a[i-1][j]='D';
                    if(j-1>=0)
                        a[i][j-1]='D';
                }
            }
        }
    }
    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            cout << a[i][j];
        }
        cout << '\n';
    }
}


#include<iostream>
#include<vector>
#include<string>

using namespace std;

int n,m,sh,w;
string res;
vector<vector<char>>a;

bool IF(int i, int j)
{
    bool r = true;
    if(i+1<n)
        if(a[i+1][j]=='W')
            r = false;
    if(j+1<m)
        if(a[i][j+1]=='W')
            r = false;
    if(i-1>=0)
        if(a[i-1][j]=='W')
            r = false;
    if(j-1>=0)
        if(a[i][j-1]=='W')
            r = false;
    return r;
}

int main()
{
    res = "No";
    cin >> n >> m;
    a.resize(n);
    string s = "";
    for(int i = 0; i<n; i++)
        a[i].resize(m);
    for(int i = 0; i<n; i++)
    {
        cin >> s;
        for(int j = 0; j<m; j++)
        {
            a[i][j]=s[j];
            if(s[j]=='S')sh++;
            if(s[j]=='W')w++;
        }
    }
    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            if(a[i][j]=='S')
                if(IF(i,j))
                {
                    res = "Yes";
                }
        }
    }
    cout << res << '\n';
    if(sh!=0 && w!=0 && res=="Yes")
    {
        for(int i = 0; i<n; i++)
        {
            for(int j = 0; j<m; j++)
            {
                /*if(a[i][j]=='S')
                {
                    if(i+1<n)
                        a[i+1][j]='D';
                    if(j+1<m)
                        a[i][j+1]='D';
                    if(i-1>=0)
                        a[i-1][j]='D';
                    if(j-1>=0)
                        a[i][j-1]='D';
                }*/
                if(a[i][j]=='.')
                    a[i][j]='D';
            }
        }
    }
    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            cout << a[i][j];
        }
        cout << '\n';
    }
}


#include<iostream>
#include<vector>
#include<string>

using namespace std;

int n,m,sh,w;
string res;
vector<vector<char>>a;

bool IF(int i, int j)
{
    bool r = true;
    if(i+1<n)
        if(a[i+1][j]=='W')
            r = false;
    if(j+1<m)
        if(a[i][j+1]=='W')
            r = false;
    if(i-1>=0)
        if(a[i-1][j]=='W')
            r = false;
    if(j-1>=0)
        if(a[i][j-1]=='W')
            r = false;
    return r;
}

int main()
{
    res = "No";
    cin >> n >> m;
    a.resize(n);
    string s = "";
    for(int i = 0; i<n; i++)
        a[i].resize(m);
    for(int i = 0; i<n; i++)
    {
        cin >> s;
        for(int j = 0; j<m; j++)
        {
            a[i][j]=s[j];
            if(s[j]=='S')sh++;
            if(s[j]=='W')w++;
        }
    }
    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            if(a[i][j]=='S')
                if(IF(i,j))
                {
                    res = "Yes";
                }
        }
    }
    cout << res << '\n';
    if(sh!=0 && w!=0 && res=="Yes")
    {
        for(int i = 0; i<n; i++)
        {
            for(int j = 0; j<m; j++)
            {
                /*if(a[i][j]=='S')
                {
                    if(i+1<n)
                        a[i+1][j]='D';
                    if(j+1<m)
                        a[i][j+1]='D';
                    if(i-1>=0)
                        a[i-1][j]='D';
                    if(j-1>=0)
                        a[i][j-1]='D';
                }*/
                if(a[i][j]=='.')
                    a[i][j]='D';
            }
        }
    }
    if(res!="No")
    {
        for(int i = 0; i<n; i++)
        {
            for(int j = 0; j<m; j++)
            {
                cout << a[i][j];
            }
            cout << '\n';
        }
    }
}


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;
    char s[1000][1000];
for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
        cin>>s[i][j];
    }
}
    int k=0;
    int l=0;
    int o=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m-1;j++){
            if(s[i][j]=='S' && s[i][j+1]=='W'){
                k++;
            }
            else if(s[i][j]=='S' && s[i][j-1]=='W'){
              l++;
            }
            else{
                o++;
            }
        }
    }
    if(k>0 || l>0){
        cout<<"No"<<endl;
    }
    else{
        cout<<"Yes"<<endl;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(s[i][j]=='S'){
                    cout<<s[i][j];

                }
                else if(s[i][j]=='W'){

                    cout<<s[i][j];
                }
                else{

                    cout<<"D";
                }
            }
            cout<<endl;
        }
    }

return 0;}


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;
    char s[1000][1000];
for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
        cin>>s[i][j];
    }
}
    int k=0;
    int l=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='S' && s[i][j+1]=='.'){
                k++;
            }
            else if(s[i][j]=='W' && s[i][j-1]=='.'){
              l++;
            }
        }
    }
    if(k>0 || l>0){
        cout<<"Yes"<<endl;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(s[i][j]=='S'){
                    cout<<s[i][j];

                }
                else if(s[i][j]=='W'){

                    cout<<s[i][j];
                }
                else{

                    cout<<"D";
                }
            }
            cout<<endl;
        }
    }
    else{
        cout<<"No"<<endl;
    }
return 0;}


#include <iostream>
#include <vector>

using namespace std;

int main(){
    int r, c;
    cin >> r >> c;
    vector<string> arr;
    for(int i=0; i<r; i++){
        string s;
        cin >> s;
        arr.push_back(s);
    }

    for(int ro=0; ro<r; ro++){
        for(int co=0; co<c; co++){
            if(arr[ro][co] == 'S'){
                if(arr[ro][co+1] == '.'){
                    arr[ro][co+1] = 'D';
                }else if(arr[ro][co+1] == 'W'){
                    cout << "NO";
                    return 0;
                }

                if(arr[ro][co-1] == '.'){
                    arr[ro][co-1] = 'D';
                }else if(arr[ro][co-1] == 'W'){
                    cout << "NO";
                    return 0;
                }

                if(arr[ro+1][co] == '.'){
                    arr[ro+1][co] = 'D';
                }else if(arr[ro+1][co] == 'W'){
                    cout << "NO";
                    return 0;
                }

                if(arr[ro-1][co] == '.'){
                    arr[ro-1][co] = 'D';
                }else if(arr[ro-1][co] == 'W'){
                    cout << "NO";
                    return 0;
                }
            }
        }
    }
    cout << "YES" << endl;

    for(int i=0; i<r; i++){
        cout << arr[i] << endl;
    }
}


#include <iostream>
#include <vector>

using namespace std;

int main(){
    int r, c;
    cin >> r >> c;
    vector<string> arr;
    for(int i=0; i<r; i++){
        string s;
        cin >> s;
        arr.push_back(s);
    }

    for(int ro=0; ro<r; ro++){
        for(int co=0; co<c; co++){
            if(arr[ro][co] == 'S'){
                if(arr[ro][co+1] == '.'){
                    arr[ro][co+1] = 'D';
                }else if(arr[ro][co+1] == 'D' || arr[ro][co+1] == 'S'){

                }else if(arr[ro][co+1] == 'W'){
                    cout << "NO";
                    return 0;
                }

                if(arr[ro][co-1] == '.'){
                    arr[ro][co-1] = 'D';
                }else if(arr[ro][co-1] == 'D' || arr[ro][co-1] == 'S'){

                }else if(arr[ro][co-1] == 'W'){
                    cout << "NO";
                    return 0;
                }

                if(arr[ro+1][co] == '.'){
                    arr[ro+1][co] = 'D';
                }else if(arr[ro+1][co] == 'D' || arr[ro+1][co] == 'S'){

                }else if(arr[ro+1][co] == 'W'){
                    cout << "NO";
                    return 0;
                }

                if(arr[ro-1][co] == '.'){
                    arr[ro-1][co] = 'D';
                }else if(arr[ro-1][co] == 'D' || arr[ro-1][co] == 'S'){

                }else if(arr[ro-1][co] == 'W'){
                    cout << "NO";
                    return 0;
                }
            }
        }
    }
    cout << "YES" << endl;

    for(int i=0; i<r; i++){
        for(int x=0; x<c; x++){
            cout << arr[i][x];
        }
        cout << endl;
    }
}


#include <iostream>
#include <vector>

using namespace std;

int main(){
    int r, c;
    cin >> r >> c;
    vector<string> arr;
    for(int i=0; i<r; i++){
        string s;
        cin >> s;
        arr.push_back(s);
    }

    for(int ro=0; ro<r; ro++){
        for(int co=0; co<c; co++){
            if(arr[ro][co] == 'S'){
                if(co != c-1){
                    if(arr[ro][co+1] == '.'){
                        arr[ro][co+1] = 'D';
                    }else if(arr[ro][co+1] == 'W'){
                        cout << "NO";
                        return 0;
                    }
                }

                if(co != 0){
                    if(arr[ro][co-1] == '.'){
                        arr[ro][co-1] = 'D';
                    }else if(arr[ro][co-1] == 'W'){
                        cout << "NO";
                        return 0;
                    }
                }

                if(ro != r-1){
                    if(arr[ro+1][co] == '.'){
                        arr[ro+1][co] = 'D';
                    }else if(arr[ro+1][co] == 'W'){
                        cout << "NO";
                        return 0;
                    }
                }

                if(ro != 0){
                    if(arr[ro-1][co] == '.'){
                        arr[ro-1][co] = 'D';
                    }else if(arr[ro-1][co] == 'W'){
                        cout << "NO";
                        return 0;
                    }
                }
            }
        }
    }
    cout << "YES" << endl;

    for(int i=0; i<r; i++){
        cout << arr[i] << endl;
    }
}


#include <iostream>

using namespace std;

int main(){
    int r, c;
    cin >> r >> c;
    char arr[r][c];
    for(int i=0; i<r; i++){
        cin >> arr[i];
    }

    for(int ro=0; ro<r; ro++){
        for(int co=0; co<c; co++){
            if(arr[ro][co] == 'S'){
                if(arr[ro][co+1] == '.'){
                    arr[ro][co+1] = 'D';
                }else if(arr[ro][co+1] == 'D' || arr[ro][co+1] == 'S'){

                }else if(arr[ro][co+1] == 'W'){
                    cout << "NO";
                    return 0;
                }

                if(arr[ro][co-1] == '.'){
                    arr[ro][co-1] = 'D';
                }else if(arr[ro][co-1] == 'D' || arr[ro][co-1] == 'S'){

                }else if(arr[ro][co-1] == 'W'){
                    cout << "NO";
                    return 0;
                }

                if(arr[ro+1][co] == '.'){
                    arr[ro+1][co] = 'D';
                }else if(arr[ro+1][co] == 'D' || arr[ro+1][co] == 'S'){

                }else if(arr[ro+1][co] == 'W'){
                    cout << "NO";
                    return 0;
                }

                if(arr[ro-1][co] == '.'){
                    arr[ro-1][co] = 'D';
                }else if(arr[ro-1][co] == 'D' || arr[ro-1][co] == 'S'){

                }else if(arr[ro-1][co] == 'W'){
                    cout << "NO";
                    return 0;
                }
            }
        }
    }
    cout << "YES" << endl;

    for(int i=0; i<r; i++){
        for(int x=0; x<c; x++){
            cout << arr[i][x];
        }
        cout << endl;
    }
}


#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int a[100010],phi[100],n,m,mod;

int get(int x)
{
    int ans=x;
    for(int i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            ans=ans/i*(i-1);
            while(x%i==0)
            {
                x/=i;
            }
        }
    }
    if(x!=1)
    {
        ans=ans/x*(x-1);
    }
    return ans;
}

int gg(long long x,int p)
{
    return x>=p?x%p+p:x;
}

int kasumi(int a,int b,int p)
{
    int ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=gg(1ll*ans*a,p);
        }
        a=gg(1ll*a*a,p);
        b>>=1;
    }
    return ans;
}

int dfs(int l,int r,int i)
{
    if(l==r||phi[i]==1)
    {
        return gg(a[l],phi[i]);
    }
    return kasumi(a[l],dfs(l+1,r,i+1),phi[i]);
}

int main()
{
    scanf("%d%d",&n,&mod);
    phi[0]=mod;
    for(int i=1;i<=30;i++)
    {
        phi[i]=get(phi[i-1]);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",dfs(l,r,0)%mod);
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
typedef long long int ll;
const int maxn = 1e7+1;
int w[maxn],phi[100];
int get(int x)
{
    int ans=x;
    for(int i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            ans=ans/i*(i-1);
            while(x%i==0)
            {
                x/=i;
            }
        }
    }
    if(x!=1)
    {
        ans=ans/x*(x-1);
    }
    return ans;
}
ll qpow(ll a, ll n, ll MOD){
    ll ans = 1;
    while(n){
        if(n & 1){
            ans = ans * a;
            if(ans >= MOD) ans = ans % MOD + MOD;
        }
        a = a * a ;
        if(a >= MOD) a = a % MOD + MOD;
        n >>= 1;
    }
    return ans;
}
int l, r;
ll func(ll pos, int i){
    if(phi[i] == 1) return 1;
    if(pos == r) return w[pos];
    ll mi = func(pos+1,i+1);
    return qpow(w[pos], mi, phi[i]);
}
int main()
{
//    freopen("in.txt", "r", stdin);
    int n, m, q;
    scanf("%d %d", &n, &m);
    phi[0]=m;
    for(int i=1;i<=30;i++)
    {
        phi[i]=get(phi[i-1]);
    }

    for(int i = 1; i <= n; i++)
        scanf("%d", &w[i]);

    scanf("%d", &q);
    while(q--){
        scanf("%d%d",&l,&r);
        cout << func(l, 0)% m << endl;
    }

    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
typedef __int64 ll;
const int maxn = 1e7+1;
int w[maxn];
ll euler(ll n){
	ll cnt = n, nownum = n;
	for(ll i = 2; i * i <= nownum; i++){
		if(nownum % i == 0){
			cnt -= (cnt / i);
			while(nownum % i == 0)
				nownum /= i;
		}
	}
	if(nownum > 1)
		cnt -= (cnt/nownum);
	return cnt;
}
ll qpow(ll a, ll n, ll MOD){
    ll ans = 1;
    while(n){
        if(n & 1){
            ans = ans * a;
            if(ans >= MOD) ans = ans % MOD + MOD;
        }
        a = a * a ;
        if(a >= MOD) a = a % MOD + MOD;
        n >>= 1;
    }
    return ans;
}
int l, r;
ll func(ll pos, ll mod){
    if(pos == r) return w[pos] % mod;
    ll p = euler(mod);
    ll mi = func(pos+1,p);
    return qpow(w[pos], mi, mod);
}
int main()
{
//    freopen("in.txt", "r", stdin);
    int n, m, q;
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &w[i]);
    scanf("%d", &q);
    while(q--){
        scanf("%d%d",&l,&r);
        printf("%I64d\n", func(l, m)% m);
    }

    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
typedef long long int ll;
const int maxn = 1e7+1;
int w[maxn];
ll euler(ll n){
	ll cnt = n, nownum = n;
	for(ll i = 2; i * i <= nownum; i++){
		if(nownum % i == 0){
			cnt -= (cnt / i);
			while(nownum % i == 0)
				nownum /= i;
		}
	}
	if(nownum > 1)
		cnt -= (cnt/nownum);
	return cnt;
}
ll qpow(ll a, ll n, ll MOD){
    ll ans = 1;
    while(n){
        if(n & 1){
            ans = ans * a;
            if(ans >= MOD) ans = ans % MOD + MOD;
        }
        a = a * a ;
        if(a >= MOD) a = a % MOD + MOD;
        n >>= 1;
    }
    return ans;
}
int l, r;
ll func(ll pos, ll mod){
    if(mod == 1) return 1;
    if(pos == r) return w[pos];
    ll p = euler(mod);
    ll mi = func(pos+1,p);
    return qpow(w[pos], mi, mod);
}
int main()
{
//    freopen("in.txt", "r", stdin);
    int n, m, q;
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &w[i]);
    scanf("%d", &q);
    while(q--){
        scanf("%d%d",&l,&r);
        cout << func(l, m)% m << endl;
    }

    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
typedef long long int ll;
const int maxn = 1e7+1;
int w[maxn],phi[100];
int get(int x)
{
    int ans=x;
    for(int i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            ans=ans/i*(i-1);
            while(x%i==0)
            {
                x/=i;
            }
        }
    }
    if(x!=1)
    {
        ans=ans/x*(x-1);
    }
    return ans;
}
ll qpow(ll a, ll n, ll MOD){
    ll ans = 1;
    while(n){
        if(n & 1){
            ans = ans * a;
            if(ans >= MOD) ans = ans % MOD + MOD;
        }
        a = a * a ;
        if(a >= MOD) a = a % MOD + MOD;
        n >>= 1;
    }
    return ans;
}
int l, r;
ll func(ll pos, int i){
    if(phi[i] == 1) return 1;
    if(pos == r) return w[pos];
    ll mi = func(pos+1,i+1);
    return qpow(w[pos], mi, phi[i]);
}
int main()
{
//    freopen("in.txt", "r", stdin);
    int n, m, q;
    scanf("%d %d", &n, &m);
    phi[0]=m;
    for(int i=1;i<=30;i++)
    {
        phi[i]=get(phi[i-1]);
    }

    for(int i = 1; i <= n; i++)
        scanf("%d", &w[i]);

    scanf("%d", &q);
    while(q--){
        scanf("%d%d",&l,&r);
        printf("%lld\n", func(l, 0)% m);
    }

    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
typedef __int64 ll;
const int maxn = 1e7+1;
int w[maxn];
ll euler(ll n){
	ll cnt = n, nownum = n;
	for(ll i = 2; i * i <= nownum; i++){
		if(nownum % i == 0){
			cnt -= (cnt / i);
			while(nownum % i == 0)
				nownum /= i;
		}
	}
	if(nownum > 1)
		cnt -= (cnt/nownum);
	return cnt;
}
ll qpow(ll a, ll n, ll MOD){
    ll ans = 1;
    while(n){
        if(n & 1){
            ans = ans * a;
            if(ans >= MOD) ans = ans % MOD + MOD;
        }
        a = a * a ;
        if(a >= MOD) a = a % MOD + MOD;
        n >>= 1;
    }
    return ans;
}
int l, r;
ll func(ll pos, ll mod){
    if(mod == 1) return 0;
    if(pos == r) return w[pos] >= mod ? w[pos]%mod+mod : w[pos];
    ll p = euler(mod);
    ll mi = func(pos+1,p);
    return qpow(w[pos], mi, mod);
}
int main()
{
    freopen("in.txt", "r", stdin);
    int n, m, q;
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &w[i]);
    scanf("%d", &q);
    while(q--){
        scanf("%d%d",&l,&r);
        printf("%I64d\n", func(l, m)% m);
    }

    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
typedef long long int ll;
const int maxn = 1e7+1;
int w[maxn];
ll euler(ll n){
	ll cnt = n, nownum = n;
	for(ll i = 2; i * i <= nownum; i++){
		if(nownum % i == 0){
			cnt -= (cnt / i);
			while(nownum % i == 0)
				nownum /= i;
		}
	}
	if(nownum > 1)
		cnt -= (cnt/nownum);
	return cnt;
}
ll qpow(ll a, ll n, ll MOD){
    ll ans = 1;
    while(n){
        if(n & 1){
            ans = ans * a;
            if(ans >= MOD) ans = ans % MOD + MOD;
        }
        a = a * a ;
        if(a >= MOD) a = a % MOD + MOD;
        n >>= 1;
    }
    return ans;
}
int l, r;
ll func(ll pos, ll mod){
    if(mod == 1) return 0;
    if(pos == r) return w[pos];
    ll p = euler(mod);
    ll mi = func(pos+1,p);
    return qpow(w[pos], mi, mod);
}
int main()
{
    freopen("in.txt", "r", stdin);
    int n, m, q;
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &w[i]);
    scanf("%d", &q);
    while(q--){
        scanf("%d%d",&l,&r);
        cout << func(l, m)% m << endl;
    }

    return 0;
}


#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int a[100010],n,m,mod;

int euler(int n){
	int cnt = n, nownum = n;
	for(int i = 2; i * i <= nownum; i++){
		if(nownum % i == 0){
			cnt -= (cnt / i);
			while(nownum % i == 0)
				nownum /= i;
		}
	}
	if(nownum > 1)
		cnt -= (cnt/nownum);
	return cnt;
}

int gg(long long x,int p)
{
    return x>=p?x%p+p:x;
}

int kasumi(int a,int b,int p)
{
    int ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=gg(1ll*ans*a,p);
        }
        a=gg(1ll*a*a,p);
        b>>=1;
    }
    return ans;
}

int dfs(int l,int r,int mod)
{
    if(l==r||mod==1)
    {
        return gg(a[l],mod);
    }
    return kasumi(a[l],dfs(l+1,r,euler(mod)),mod);
}

int main()
{
    scanf("%d%d",&n,&mod);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",dfs(l,r,mod)%mod);
    }
    return 0;
}


#include<bits/stdc++.h>
#define I64 "%I64d"
#define RAN(v)v.begin(),v.end()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
using namespace std;
typedef long long ll;
template<class T>
inline void upd1(T&a,T b){a>b?a=b:0;}
template<class T>
inline void upd2(T&a,T b){a<b?a=b:0;}
struct ano{
	operator ll(){
		ll x=0,y=0,c=getchar();
		while(c<48)
			y=c==45,c=getchar();
		while(c>47)
			x=x*10+c-48,c=getchar();
		return y?-x:x;
	}
}buf;
int wop(int a,int n,int p){
	int s=1;
	for(;n;n>>=1){
		if(n&1)s=(ll)s*a%p;
		a=(ll)a*a%p;
	}
	return s;
}
int phi(int n){
	int x=n;
	for(int j=2;j*j<=n;++j)
		if(n%j==0){
			x-=x/j;
			while(n%j==0)n/=j;
		}
	if(n!=1)x-=x/n;
	return x;
}
int c[100005];
int main(){
	int n=buf,p=buf;
	vector<int>a,b;
	for(int i=1;i<=n;++i){
		c[i]=buf;
		if(c[i]>1)
			a.pb(i),b.pb(c[i]);
	}
	int q=buf;
	while(q--){
		int l=buf,r=buf,l2=lb(RAN(a),l)-a.begin(),r2=ub(RAN(a),r)-a.begin()-1,s=1;
		while(r2>=l2&&pow(b[r2],s)<1e9)
			s=pow(b[r2--],s)+.5;
		if(r2<l2)
			printf("%d\n",s%p);
		else{
			function<int(int,int)>sol;
			sol=[&](int i,int p)->int{
				if(i>a[r2])return s%p;
				if(p==1)return 0;
				int q=phi(p);
				return wop(c[i],sol(i+1,q)+q,p);
			};
			printf("%d\n",sol(l,p));
		}
	}
}


#include<bits/stdc++.h>
#define I64 "%I64d"
#define RAN(v)v.begin(),v.end()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
using namespace std;
typedef long long ll;
template<class T>
inline void upd1(T&a,T b){a>b?a=b:0;}
template<class T>
inline void upd2(T&a,T b){a<b?a=b:0;}
struct ano{
	operator ll(){
		ll x=0,y=0,c=getchar();
		while(c<48)
			y=c==45,c=getchar();
		while(c>47)
			x=x*10+c-48,c=getchar();
		return y?-x:x;
	}
}buf;
int wop(int a,int n,int p){
	int s=1%p;
	for(;n;n>>=1){
		if(n&1)s=(ll)s*a%p;
		a=(ll)a*a%p;
	}
	return s;
}
int phi(int n){
	int x=n;
	for(int j=2;j*j<=n;++j)
		if(n%j==0){
			x-=x/j;
			while(n%j==0)n/=j;
		}
	if(n!=1)x-=x/n;
	return x;
}
int c[100005],f[40];
int main(){
	int n=buf;
	f[0]=buf;
	for(int i=0;f[i]!=1;++i)
		f[i+1]=phi(f[i]);
	vector<int>g;
	for(int i=1;i<=n;++i){
		c[i]=buf;
		if(c[i]==1)g.pb(i);
	}
	int q=buf;
	while(q--){
		int l=buf,r=min((int)buf,*lb(RAN(g),l)-1),s=1;
		while(r>=l&&pow(c[r],s)<1e9)
			s=pow(c[r--],s)+.5;
		if(r<l)
			printf("%d\n",s%f[0]);
		else{
			function<int(int,int)>sol;
			sol=[&](int i,int j){
				return f[j]==1?0:wop(c[i],i==r?s:sol(i+1,j+1)+f[j+1],f[j]);
			};
			printf("%d\n",sol(l,0));
		}
	}
}


#include<bits/stdc++.h>
#define I64 "%I64d"
#define RAN(v)v.begin(),v.end()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
using namespace std;
typedef long long ll;
template<class T>
inline void upd1(T&a,T b){a>b?a=b:0;}
template<class T>
inline void upd2(T&a,T b){a<b?a=b:0;}
struct ano{
	operator ll(){
		ll x=0,y=0,c=getchar();
		while(c<48)
			y=c==45,c=getchar();
		while(c>47)
			x=x*10+c-48,c=getchar();
		return y?-x:x;
	}
}buf;
int wop(int a,int n,int p){
	int s=1%p;
	for(;n;n>>=1){
		if(n&1)s=(ll)s*a%p;
		a=(ll)a*a%p;
	}
	return s;
}
int phi(int n){
	int x=n;
	for(int j=2;j*j<=n;++j)
		if(n%j==0){
			x-=x/j;
			while(n%j==0)n/=j;
		}
	if(n!=1)x-=x/n;
	return x;
}
int c[100005],f[40];
int main(){
	int n=buf;
	f[0]=buf;
	for(int i=0;f[i]!=1;++i)
		f[i+1]=phi(f[i]);
	vector<int>a,b;
	for(int i=1;i<=n;++i){
		c[i]=buf;
		if(c[i]>1)
			a.pb(i),b.pb(c[i]);
	}
	int q=buf;
	while(q--){
		int l=buf,r=buf,l2=lb(RAN(a),l)-a.begin(),r2=ub(RAN(a),r)-a.begin()-1,s=1;
		while(r2>=l2&&pow(b[r2],s)<1e9)
			s=pow(b[r2--],s)+.5;
		if(r2<l2)
			printf("%d\n",s%f[0]);
		else{
			function<int(int,int)>sol;
			sol=[&](int i,int j){
				return f[j]==1?0:i>a[r2]?s%f[j]:wop(c[i],sol(i+1,j+1)+f[j+1],f[j]);
			};
			printf("%d\n",sol(l,0));
		}
	}
}


#include<bits/stdc++.h>
#define I64 "%I64d"
#define RAN(v)v.begin(),v.end()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
using namespace std;
typedef long long ll;
template<class T>
inline void upd1(T&a,T b){a>b?a=b:0;}
template<class T>
inline void upd2(T&a,T b){a<b?a=b:0;}
struct ano{
	operator ll(){
		ll x=0,y=0,c=getchar();
		while(c<48)
			y=c==45,c=getchar();
		while(c>47)
			x=x*10+c-48,c=getchar();
		return y?-x:x;
	}
}buf;
int wop(int a,int n,int p){
	int s=1;
	for(;n;n>>=1){
		if(n&1)s=(ll)s*a%p;
		a=(ll)a*a%p;
	}
	return s;
}
int phi(int n){
	int x=n;
	for(int j=2;j*j<=n;++j)
		if(n%j==0){
			x-=x/j;
			while(n%j==0)n/=j;
		}
	if(n!=1)x-=x/n;
	return x;
}
int c[100005];
int main(){
	int n=buf,p=buf;
	vector<int>a,b;
	for(int i=1;i<=n;++i){
		c[i]=buf;
		if(c[i]>1)
			a.pb(i),b.pb(c[i]);
	}
	int q=buf;
	while(q--){
		int l=buf,r=buf,l2=lb(RAN(a),l)-a.begin(),r2=ub(RAN(a),r)-a.begin()-1,s=1;
		while(r2>=l2&&pow(b[r2],s)<1e9)
			s=pow(b[r2--],s)+.5;
		if(r2<l2)
			printf("%d\n",s);
		else{
			function<int(int,int)>sol;
			sol=[&](int i,int p)->int{
				if(i>a[r2])return s%p;
				if(p==1)return 0;
				int q=phi(p);
				return wop(c[i],sol(i+1,q)+q,p);
			};
			printf("%d\n",sol(l,p));
		}
	}
}


#include<bits/stdc++.h>
#define I64 "%I64d"
#define RAN(v)v.begin(),v.end()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
using namespace std;
typedef long long ll;
template<class T>
inline void upd1(T&a,T b){a>b?a=b:0;}
template<class T>
inline void upd2(T&a,T b){a<b?a=b:0;}
struct ano{
	operator ll(){
		ll x=0,y=0,c=getchar();
		while(c<48)
			y=c==45,c=getchar();
		while(c>47)
			x=x*10+c-48,c=getchar();
		return y?-x:x;
	}
}buf;
int wop(int a,int n,int p){
	int s=1%p;
	for(;n;n>>=1){
		if(n&1)s=(ll)s*a%p;
		a=(ll)a*a%p;
	}
	return s;
}
int phi(int n){
	int x=n;
	for(int j=2;j*j<=n;++j)
		if(n%j==0){
			x-=x/j;
			while(n%j==0)n/=j;
		}
	if(n!=1)x-=x/n;
	return x;
}
const int N=1e5+5;
int c[N],g[N],f[40];
int main(){
	int n=buf;
	f[0]=buf;
	for(int i=0;f[i]!=1;++i)
		f[i+1]=phi(f[i]);
	for(int i=1;i<=n;++i)
		c[i]=buf;
	g[n+1]=1e9;
	for(int i=n;i>=1;--i)
		g[i]=c[i]==1?i:g[i+1];
	int q=buf;
	while(q--){
		int l=buf,r=min((int)buf,g[l]),s=1;
		while(r>=l&&pow(c[r],s)<1e9)
			s=pow(c[r--],s)+.5;
		if(r<l)
			printf("%d\n",s%f[0]);
		else{
			function<int(int,int)>sol;
			sol=[&](int i,int j){
				return f[j]==1?0:wop(c[i],i==r?s:sol(i+1,j+1)+f[j+1],f[j]);
			};
			printf("%d\n",sol(l,0));
		}
	}
}


#include<bits/stdc++.h>
#define I64 "%I64d"
#define RAN(v)v.begin(),v.end()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
using namespace std;
typedef long long ll;
template<class T>
inline void upd1(T&a,T b){a>b?a=b:0;}
template<class T>
inline void upd2(T&a,T b){a<b?a=b:0;}
struct ano{
	operator ll(){
		ll x=0,y=0,c=getchar();
		while(c<48)
			y=c==45,c=getchar();
		while(c>47)
			x=x*10+c-48,c=getchar();
		return y?-x:x;
	}
}buf;
int wop(int a,int n,int p){
	int s=1%p;
	for(;n;n>>=1){
		if(n&1)s=(ll)s*a%p;
		a=(ll)a*a%p;
	}
	return s;
}
int phi(int n){
	int x=n;
	for(int j=2;j*j<=n;++j)
		if(n%j==0){
			x-=x/j;
			while(n%j==0)n/=j;
		}
	if(n!=1)x-=x/n;
	return x;
}
int c[100005],f[40];
int main(){
	int n=buf;
	f[0]=buf;
	for(int i=0;f[i]!=1;++i)
		f[i+1]=phi(f[i]);
	vector<int>a,b;
	for(int i=1;i<=n;++i){
		c[i]=buf;
		if(c[i]>1)
			a.pb(i),b.pb(c[i]);
	}
	int q=buf;
	while(q--){
		int l=buf,r=buf,l2=lb(RAN(a),l)-a.begin(),r2=ub(RAN(a),r)-a.begin()-1,s=1;
		while(r2>=l2&&pow(b[r2],s)<1e9)
			s=pow(b[r2--],s)+.5;
		if(r2<l2)
			printf("%d\n",s%f[0]);
		else{
			function<int(int,int)>sol;
			sol=[&](int i,int j){
				return f[j]==1?0:wop(c[i],i==a[r2]?s:sol(i+1,j+1)+f[j+1],f[j]);
			};
			printf("%d\n",sol(l,0));
		}
	}
}


#include<bits/stdc++.h>
#define I64 "%I64d"
#define RAN(v)v.begin(),v.end()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
using namespace std;
typedef long long ll;
template<class T>
inline void upd1(T&a,T b){a>b?a=b:0;}
template<class T>
inline void upd2(T&a,T b){a<b?a=b:0;}
struct ano{
	operator ll(){
		ll x=0,y=0,c=getchar();
		while(c<48)
			y=c==45,c=getchar();
		while(c>47)
			x=x*10+c-48,c=getchar();
		return y?-x:x;
	}
}buf;
int wop(int a,int n,int p){
	int s=1%p;
	for(;n;n>>=1){
		if(n&1)s=(ll)s*a%p;
		a=(ll)a*a%p;
	}
	return s;
}
int phi(int n){
	int x=n;
	for(int j=2;j*j<=n;++j)
		if(n%j==0){
			x-=x/j;
			while(n%j==0)n/=j;
		}
	if(n!=1)x-=x/n;
	return x;
}
int c[100005],f[40];
int main(){
	int n=buf;
	f[0]=buf;
	for(int i=0;f[i]!=1;++i)
		f[i+1]=phi(f[i]);
	vector<int>a,b;
	for(int i=1;i<=n;++i){
		c[i]=buf;
		if(c[i]>1)
			a.pb(i),b.pb(c[i]);
	}
	int q=buf;
	while(q--){
		int l=buf,r=buf,l2=lb(RAN(a),l)-a.begin(),r2=ub(RAN(a),r)-a.begin()-1,s=1;
		while(r2>=l2&&pow(b[r2],s)<f[1])
			s=pow(b[r2--],s)+.5;
		if(r2<l2)
			printf("%d\n",s%f[0]);
		else{
			function<int(int,int)>sol;
			sol=[&](int i,int j){
				return f[j]==1?0:i>a[r2]?s%f[j]:wop(c[i],sol(i+1,j+1)+f[j+1],f[j]);
			};
			printf("%d\n",sol(l,0));
		}
	}
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q,bj;
int v[maxm+10];
vector<int> M;

inline int work(int a,int b,int m)
{
    if(1ll*a*b>=m)bj=1;
    return 1ll*a*b%m;
}

int qpow(int a,int b,int m)
{
    int re=1;
    for(bj=0;b;b>>=1,a=work(a,a,m))
        if(b&1)re=work(re,a,m);
    return re;
}

int getphi(int t)
{
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    return re;
}

int euler(int v[],int l,int r)
{
    int mx=min(r-l+1,(int)M.size());
    int now=work(v[l+mx-1],1,M[mx-1]);
    for(int i=mx-2;i>=0;i--)
    {
        if(bj)now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    for(M.push_back(m);m!=1;m=getphi(m),M.push_back(m));

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r));
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q,bj;
int v[maxm+10];
vector<int> M;

int qpow(int a,int b,int m)
{
    int re=1;
    for(bj=0;b;b>>=1,a=1ll*a*a%m)
        if(b&1)re=1ll*re*a%m;
    return re;
}

int getphi(int t)
{
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    return re;
}

bool check(int l,int r,int m)
{
    if(m==1)return 1;
    long long now=1;
    for(int i=l;i<=r;i++)
    {
        long long val=1;
        if(v[i]!=1)for(int j=1;j<=now;j++)
        {
            val*=v[i];
            if(val>=m)return 1;
        }
        now=val;
    }
    return 0;
}

int euler(int v[],int l,int r)
{
    int mx=min(r-l+1,(int)M.size());
    int now=v[l+mx-1]%M[mx-1];
    for(int i=mx-2;i>=0;i--)
    {
        if(check(i+1,mx-1,M[i+1]))now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    int tmp=m;
    for(M.push_back(tmp);tmp!=1;tmp=getphi(tmp),M.push_back(tmp));

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r));
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q,bj;
int v[maxm+10];
vector<int> M;

inline int work(int a,int b,int m)
{
    if(1ll*a*b>=m)bj=1;
    return 1ll*a*b%m;
}

int qpow(int a,int b,int m)
{
    int re=1;
    for(bj=0;b;b>>=1,a=work(a,a,m))
        if(b&1)re=work(re,a,m);
    return re;
}

int getphi(int t)
{
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    return re;
}

int euler(int v[],int l,int r)
{
    bj=0;
    int mx=min(r-l+1,(int)M.size());
    int now=work(v[l+mx-1],1,M[mx-1]);
    for(int i=mx-2;i>=0;i--)
    {
        if(bj)now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    int tmp=m;
    for(M.push_back(tmp);tmp!=1;tmp=getphi(tmp),M.push_back(tmp));

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r));
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q;
int v[maxm+10];
vector<int> M;

int qpow(int a,int b,int m)
{
    int re;
    for(re=1;b;b>>=1,a=1ll*a*a%m)
        if(b&1)re=1ll*re*a%m;
    return re;
}

int getphi(int t)
{
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    return re;
}

bool check(int l,int r,int m)
{
    long long now=1;
    for(int i=l;i<=r;i++)
        if(v[i]==1){r=i-1;break;}
    for(int i=r;i>=l;i--)
    {
        long long val=1;
        for(int j=1;j<=now;j++)
            if((val*=v[i])>m)return 1;
        now=val;
    }
    return 0;
}

int euler(int v[],int l,int r)
{
    int mx=min(r-l+1,(int)M.size());
    int now=v[l+mx-1]%M[mx-1];
    for(int i=mx-2;i>=0;i--)
    {
        if(check(i+1,mx-1,M[i+1]))now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    int tmp=m;
    for(M.push_back(tmp);tmp!=1;tmp=getphi(tmp),M.push_back(tmp));

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r));
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q;
int v[maxm+10];
map<int,int> phi;

int qpow(int a,int b,int m)
{
    int re;
    for(re=1;b;b>>=1,a=1ll*a*a%m)
        if(b&1)re=1ll*re*a%m;
    return re;
}

bool check(int v[],int l,int r,int m)
{
    long long now=1;
    for(int i=r;i>=l;i--)
    {
        long long val=1;
        for(int j=1;j<=now;j++)
            if((val*=v[i])>=m)return 1;
        now=val;
    }
    return 0;
}

int getphi(int t)
{
    if(phi.count(t))return phi[t];
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    phi[t]=re;
    return re;
}

int euler(int v[],int l,int r,int mod)
{
    if(l==r)return v[l]%mod;
    vector<int> M;
    M.push_back(mod);
    for(int i=l+1;i<=r&&m!=1;i++)
        mod=getphi(mod),M.push_back(mod);

    int now=v[l+M.size()-1]%M.back();
    for(int i=M.size()-2;i>=0;i--)
    {
        if(check(v,l+i+1,l+M.size()-1,M[i+1]))now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r,m));
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q,bj;
int v[maxm+10];
map<int,int> phi;

inline int work(int a,int b)
{
    if(1ll*a*b>=m)bj=1;
    return 1ll*a*b%m;
}

int qpow(int a,int b,int m)
{
    int re=1;
    for(bj=0;b;b>>=1,a=work(a,a))
        if(b&1)re=work(re,a);
    return re;
}

int getphi(int t)
{
    if(phi.count(t))return phi[t];
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    phi[t]=re;
    return re;
}

int euler(int v[],int l,int r,int mod)
{
    if(l==r)return v[l]%mod;
    vector<int> M;
    M.push_back(mod);
    for(int i=l+1;i<=r&&m!=1;i++)
        mod=getphi(mod),M.push_back(mod);

    int now=v[l+M.size()-1]%M.back();
    for(int i=M.size()-2;i>=0;i--)
    {
        if(bj)now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r,m));
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q;
int v[maxm+10];
vector<int> M;

int qpow(int a,int b,int m)
{
    int re;
    for(re=1;b;b>>=1,a=1ll*a*a%m)
        if(b&1)re=1ll*re*a%m;
    return re;
}

int getphi(int t)
{
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    return re;
}

bool check(int l,int r,int m)
{
    long long now=1;
    for(int i=l;i<r;i++)
        if(v[i]==1){r=i;break;}
    for(int i=l;i<=r;i++)
    {
        long long val=1;
        for(int j=1;j<=now;j++)
            if((val*=v[i])>m)return 1;
        now=val;
    }
    return 0;
}

int euler(int v[],int l,int r)
{
    int mx=min(r-l+1,(int)M.size());
    int now=v[l+mx-1]%M[mx-1];
    for(int i=mx-2;i>=0;i--)
    {
        if(check(i+1,mx-1,M[i+1]))now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    int tmp=m;
    for(M.push_back(tmp);tmp!=1;tmp=getphi(tmp),M.push_back(tmp));

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r));
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q,bj;
int v[maxm+10];
vector<int> M;

inline int work(int a,int b,int m)
{
    if(1ll*a*b>=m)bj=1;
    return 1ll*a*b%m;
}

int qpow(int a,int b,int m)
{
    int re;
    for(re=1;b;b>>=1,a=work(a,a,m))
        if(b&1)re=work(re,a,m);
    return re;
}

int getphi(int t)
{
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    return re;
}

int euler(int v[],int l,int r)
{
    bj=0;
    int mx=min(r-l+1,(int)M.size());
    int now=work(v[l+mx-1],1,M[mx-1]);
    for(int i=mx-2;i>=0;i--)
    {
        if(bj)now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    int tmp=m;
    for(M.push_back(tmp);tmp!=1;tmp=getphi(tmp),M.push_back(tmp));

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r));
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q,bj;
int v[maxm+10];
vector<int> M;

inline int work(int a,int b,int m)
{
    if(1ll*a*b>=m)bj=1;
    return 1ll*a*b%m;
}

int qpow(int a,int b,int m)
{
    int re=1;
    for(bj=0;b;b>>=1,a=work(a,a,m))
        if(b&1)re=work(re,a,m);
    return re;
}

int getphi(int t)
{
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    return re;
}

int euler(int v[],int l,int r)
{
    int mx=min(r-l+1,(int)M.size());
    int now=v[l+mx-1]%M[mx-1];
    for(int i=mx-2;i>=0;i--)
    {
        if(bj)now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    for(M.push_back(m);m!=1;m=getphi(m),M.push_back(m));

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r));
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q;
int v[maxm+10];
vector<int> M;

int qpow(int a,int b,int m)
{
    int re;
    for(re=1;b;b>>=1,a=1ll*a*a%m)
        if(b&1)re=1ll*re*a%m;
    return re;
}

int getphi(int t)
{
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    return re;
}

bool check(int l,int r,int m)
{
    long long now=1;
    for(int i=l;i<=r;i++)
    {
        long long val=1;
        if(v[i]!=1)for(int j=1;j<=now;j++)
            if((val*=v[i])>m)return 1;
        now=val;
    }
    return 0;
}

int euler(int v[],int l,int r)
{
    int mx=min(r-l+1,(int)M.size());
    int now=v[l+mx-1]%M[mx-1];
    for(int i=mx-2;i>=0;i--)
    {
        if(check(i+1,mx-1,M[i+1]))now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    int tmp=m;
    for(M.push_back(tmp);tmp!=1;tmp=getphi(tmp),M.push_back(tmp));

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r));
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q;
int v[maxm+10];
vector<int> M;

int qpow(int a,int b,int m)
{
    int re;
    for(re=1;b;b>>=1,a=1ll*a*a%m)
        if(b&1)re=1ll*re*a%m;
    return re;
}

int getphi(int t)
{
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    return re;
}

bool check(int l,int r,int m)
{
    long long now=1;
    for(int i=l;i<r;i++)
        if(v[i]==1){r=i;break;}
    for(int i=r;i>=l;i--)
    {
        long long val=1;
        for(int j=1;j<=now;j++)
            if((val*=v[i])>m)return 1;
        now=val;
    }
    return 0;
}

int euler(int v[],int l,int r)
{
    int mx=min(r-l+1,(int)M.size());
    int now=v[l+mx-1]%M[mx-1];
    for(int i=mx-2;i>=0;i--)
    {
        if(check(i+1,mx-1,M[i+1]))now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    int tmp=m;
    for(M.push_back(tmp);tmp!=1;tmp=getphi(tmp),M.push_back(tmp));

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r));
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q,bj;
int v[maxm+10];
vector<int> M;

inline int work(int a,int b,int m)
{
    if(1ll*a*b>=m)bj=1;
    return 1ll*a*b%m;
}

int qpow(int a,int b,int m)
{
    int re=1;
    for(bj=0;b;b>>=1,a=work(a,a,m))
        if(b&1)re=work(re,a,m);
    return re;
}

int getphi(int t)
{
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    return re;
}

int euler(int v[],int l,int r)
{
    bj=0;
    int mx=min(r-l+1,(int)M.size());
    int now=work(v[l+mx-1],1,M[mx-1]);
    for(int i=mx-2;i>=0;i--)
    {
        if(bj)now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    for(M.push_back(m);m!=1;m=getphi(m),M.push_back(m));

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r));
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxm=1e5+10;

int n,m,q;
int v[maxm+10];
map<int,int> phi;

int qpow(int a,int b,int m)
{
    int re;
    for(re=1;b;b>>=1,a=1ll*a*a%m)
        if(b&1)re=1ll*re*a%m;
    return re;
}

bool check(int v[],int l,int r,int m)
{
    long long now=1;
    for(int i=r;i>=l;i--)
    {
        long long val=1;
        if(v[i]!=1)for(int j=1;j<=now;j++)
            if((val*=v[i])>=m)return 1;
        now=val;
    }
    return 0;
}

int getphi(int t)
{
    if(phi.count(t))return phi[t];
    int re=t;
    for(int i=2;i*i<=t;i++)
    {
        if(t%i!=0)continue;
        re/=i,re*=(i-1);
        while(t%i==0)t/=i;
    }
    if(t!=1)re/=t,re*=(t-1);
    phi[t]=re;
    return re;
}

int euler(int v[],int l,int r,int mod)
{
    if(l==r)return v[l]%mod;
    vector<int> M;
    M.push_back(mod);
    for(int i=l+1;i<=r&&m!=1;i++)
        mod=getphi(mod),M.push_back(mod);

    int now=v[l+M.size()-1]%M.back();
    for(int i=M.size()-2;i>=0;i--)
    {
        if(check(v,l+i+1,l+M.size()-1,M[i+1]))now+=M[i+1];
        now=qpow(v[l+i],now,M[i]);
    }
    return now;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",euler(v,l,r,m));
    }

    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+100;
int n,m,a[N],q;
unordered_map <int,int> mp;
int gcd(int a,int b)
{
	if (b==0) return a;
	return gcd(b,a%b);
}
inline int m_pow(int a,int b,int p)
{
	int ans=1;
	a%=p;
	while (b)
	{
		if (b&1) ans=(ans*a)%p;
		b>>=1;
		a=(a*a)%p;
	}
	return ans;
}
inline int phi(int x)
{
	if (mp[x]) return mp[x];
	int ans=x;
	for (int i=2;i*i<=x;i++)
	{
		if (x%i==0)
		{
			ans/=i;
			ans*=(i-1);
			while (x%i==0) x/=i;
		}
	}
	if (x>1) ans/=x,ans*=(x-1);
	mp[x]=ans;
	return ans;
}
int solve(int l,int r,int p)
{
	if (l==r) return a[l]%p;
	if (p==1) return 0;
	return m_pow(a[l],solve(l+1,r,phi(p))+phi(p),p);
}
signed main()
{
	scanf("%lld%lld",&n,&m);
	for (int i=1;i<=n;i++) scanf("%lld",&a[i]),a[i]%=m;
	for (int i=1;i<=n;i++) printf("%lld ",a[i]);
	printf("\n");
	scanf("%lld",&q);
	while (q--)
	{
		int l,r;
		scanf("%lld%lld",&l,&r);
		printf("%lld\n",solve(l,r,m));
	}
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+100;
int n,m,a[N],q;
unordered_map <int,int> mp;
int gcd(int a,int b)
{
	if (b==0) return a;
	return gcd(b,a%b);
}
inline int m_pow(int a,int b,int p)
{
	int ans=1;
	while (b)
	{
		if (b&1)
		{
			ans=(ans*a);
			if (ans>=p) ans=ans%p+p;
		}
		b>>=1;
		a=(a*a);
		if (a>=p) a=a%p+p;
	}
	return ans;
}
inline int phi(int x)
{
	if (mp[x]) return mp[x];
	int ans=x,tmp=x;
	for (int i=2;i*i<=tmp;i++)
	{
		if (tmp%i==0)
		{
			ans/=i;
			ans*=(i-1);
			while (tmp%i==0) tmp/=i;
		}
	}
	if (tmp>1) ans/=tmp,ans*=(tmp-1);
	mp[x]=ans;
	return ans;
}
int solve(int l,int r,int p)
{
	if (l==r) return a[l]%p+(a[l]>=p)*p;
	if (p==1) return 1;
	if (a[l]==1) return 1;
	int k=solve(l+1,r,phi(p));
	return m_pow(a[l],k,p);
}
signed main()
{
	scanf("%lld%lld",&n,&m);
	for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
	scanf("%lld",&q);
	while (q--)
	{
		int l,r;
		scanf("%lld%lld",&l,&r);
		printf("%lld\n",solve(l,r,m)%m);
	}
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+100;
int n,m,a[N],q;
unordered_map <int,int> mp;
int gcd(int a,int b)
{
	if (b==0) return a;
	return gcd(b,a%b);
}
inline int m_pow(int a,int b,int p)
{
	int ans=1;
	a%=p;
	while (b)
	{
		if (b&1) ans=(ans*a)%p;
		b>>=1;
		a=(a*a)%p;
	}
	return ans;
}
inline int phi(int x)
{
	if (mp[x]) return mp[x];
	int ans=x;
	for (int i=2;i*i<=x;i++)
	{
		if (x%i==0)
		{
			ans/=i;
			ans*=(i-1);
			while (x%i==0) x/=i;
		}
	}
	if (x>1) ans/=x,ans*=(x-1);
	mp[x]=ans;
	return ans;
}
int solve(int l,int r,int p)
{
	if (l==r) return a[l]%p;
	if (p==1) return 0;
	return m_pow(a[l],solve(l+1,r,phi(p))+phi(p),p);
}
signed main()
{
	scanf("%lld%lld",&n,&m);
	for (int i=1;i<=n;i++) scanf("%lld",&a[i]),a[i]%=m;
	printf("\n");
	scanf("%lld",&q);
	while (q--)
	{
		int l,r;
		scanf("%lld%lld",&l,&r);
		printf("%lld\n",solve(l,r,m));
	}
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+100;
int n,m,a[N],q;
unordered_map <int,int> mp;
int gcd(int a,int b)
{
	if (b==0) return a;
	return gcd(b,a%b);
}
inline int m_pow(int a,int b,int p)
{
	int ans=1;
	while (b)
	{
		if (b&1)
		{
			ans=(ans*a);
			if (ans>=p) ans=ans%p+p;
		}
		b>>=1;
		a=(a*a);
		if (a>=p) a=a%p+p;
	}
	return ans;
}
inline int phi(int x)
{
	if (mp[x]) return mp[x];
	int ans=x,tmp=x;
	for (int i=2;i*i<=tmp;i++)
	{
		if (tmp%i==0)
		{
			ans/=i;
			ans*=(i-1);
			while (tmp%i==0) tmp/=i;
		}
	}
	if (tmp>1) ans/=tmp,ans*=(tmp-1);
	mp[x]=ans;
	return ans;
}
int solve(int l,int r,int p)
{
	if (l==r) return a[l]%p+(a[l]>=p)*p;
	if (p==1) return 1;
	if (a[l]==1) return 1;
	int k=solve(l+1,r,phi(p));
	return m_pow(a[l],k,p);
}
signed main()
{
	scanf("%lld%lld",&n,&m);
	for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
	for (int i=1;i<=n;i++) printf("%lld ",a[i]);
	printf("\n");
	scanf("%lld",&q);
	while (q--)
	{
		int l,r;
		scanf("%lld%lld",&l,&r);
		printf("%lld\n",solve(l,r,m)%m);
	}
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+100;
int n,m,a[N],q;
unordered_map <int,int> mp;
inline int m_pow(int a,int b,int p)
{
	int ans=1;
	while (b)
	{
		if (b&1) ans=(ans*a)%p;
		b>>=1;
		a=(a*a)%p;
	}
	return ans;
}
inline int phi(int x)
{
	if (mp[x]) return mp[x];
	int ans=x;
	for (int i=2;i*i<=x;i++)
	{
		if (x%i==0)
		{
			ans/=i;
			ans*=(i-1);
			while (x%i==0) x/=i;
		}
	}
	if (x>1) ans/=x,ans*=(x-1);
	mp[x]=ans;
	return ans;
}
int solve(int l,int r,int p)
{
	if (l==r) return a[l]%p;
	if (p==1) return 0;
	return m_pow(a[l],solve(l+1,r,phi(p)),p);
}
signed main()
{
	scanf("%lld%lld",&n,&m);
	for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
	scanf("%lld",&q);
	while (q--)
	{
		int l,r;
		scanf("%lld%lld",&l,&r);
		printf("%lld\n",solve(l,r,m));
	}
}


#include<bits/stdc++.h>
using namespace std;
int power(int x,int y,int p){
	int ans=1;
	for (;y;y>>=1,x=1ll*x*x%p)
		if (y&1) ans=1ll*ans*x%p;
	return ans;
}
int Phi(int x){
	int ans=x;
	for (int i=2;i*i<=x;i++)
	if (x%i==0){
		ans=ans/i*(i-1);
		while (x%i==0) x/=i;
	}
	if (x>1) ans=ans/x*(x-1);
	return ans;
}
const int N=2e5+10;
int n,m,q,w[N],R[N],phi[N],size;
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&w[i]);
	phi[0]=m;
	for (;phi[size]>1;size++) phi[size+1]=Phi(phi[size]);
	scanf("%d",&q);
	for (int i=1;i<=q;i++){
		int l,r;
		scanf("%d%d",&l,&r);
		r=min(r,l+size);
		int ans=1;double now=1;
		for (int i=r,dep=r-l;i>=l;i--,dep--){
			int k=1,d=__gcd(w[i],phi[dep]);
			if (now>30) k=power(d,30,phi[dep]);else k=power(d,(int)round(now),phi[dep]);
			ans=power(w[i]/d,ans,phi[dep]);
			ans=1ll*ans*k%phi[dep];
			now=pow(w[i],now);
		}
		printf("%d\n",ans%m);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int power(int x,int y,int p){
	int ans=1;
	for (;y;y>>=1,x=1ll*x*x%p)
		if (y&1) ans=1ll*ans*x%p;
	return ans;
}
int Phi(int x){
	int ans=x;
	for (int i=2;i*i<=x;i++)
	if (x%i==0){
		ans=ans/i*(i-1);
		while (x%i==0) x/=i;
	}
	if (x>1) ans=ans/x*(x-1);
	return ans;
}
const int N=2e5+10;
int n,m,q,w[N],R[N],phi[N],size;
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&w[i]);
	phi[0]=m;
	for (;phi[size]>1;size++) phi[size+1]=Phi(phi[size]);
	scanf("%d",&q);
	for (int i=1;i<=q;i++){
		int l,r,R;
		scanf("%d%d",&l,&r);
		R=min(r,l+size);
		double now=1;
		for (int i=min(r,R+10);i>R;i++) now=pow(w[i],now);
		int ans=1;
		for (int i=R,dep=R-l;i>=l;i--,dep--){
			int k=1,d=__gcd(w[i],phi[dep]);
			if (now>30) k=power(d,30,phi[dep]);else k=power(d,(int)round(now),phi[dep]);
			ans=power(w[i]/d,ans,phi[dep]);
			ans=1ll*ans*k%phi[dep];
			now=pow(w[i],now);
		}
		printf("%d\n",ans%m);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int power(int x,int y,int p){
	int ans=1;
	for (;y;y>>=1,x=1ll*x*x%p)
		if (y&1) ans=1ll*ans*x%p;
	return ans;
}
int Phi(int x){
	int ans=x;
	for (int i=2;i*i<=x;i++)
	if (x%i==0){
		ans=ans/i*(i-1);
		while (x%i==0) x/=i;
	}
	if (x>1) ans=ans/x*(x-1);
	return ans;
}
const int N=2e5+10;
int n,m,q,w[N],phi[N],size;
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&w[i]);
	phi[0]=m;
	for (;phi[size]>1;size++) phi[size+1]=Phi(phi[size]);
	scanf("%d",&q);
	for (int i=1;i<=q;i++){
		int l,r;
		scanf("%d%d",&l,&r);
		r=min(r,l+size);
		int ans=1;
		for (int i=r,dep=r-l;i>=l;i--,dep--) ans=power(w[i],ans,phi[dep]);
		printf("%d\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int power(int x,int y,int p){
	int ans=1;
	for (;y;y>>=1,x=1ll*x*x%p)
		if (y&1) ans=1ll*ans*x%p;
	return ans;
}
int Phi(int x){
	int ans=x;
	for (int i=2;i*i<=x;i++)
	if (x%i==0){
		ans=ans/i*(i-1);
		while (x%i==0) x/=i;
	}
	if (x>1) ans=ans/x*(x-1);
	return ans;
}
const int N=2e5+10;
int n,m,q,w[N],phi[N],size;
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&w[i]);
	phi[0]=m;
	for (;phi[size]>1;size++) phi[size+1]=Phi(phi[size]);
	scanf("%d",&q);
	for (int i=1;i<=q;i++){
		int l,r;
		scanf("%d%d",&l,&r);
		r=min(r,l+size);
		int ans=1;
		for (int i=r,dep=r-l;i>=l;i--,dep--) ans=power(w[i],ans,phi[dep]);
		printf("%d\n",ans%m);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int power(int x,int y,int p){
	int ans=1;
	for (;y;y>>=1,x=1ll*x*x%p)
		if (y&1) ans=1ll*ans*x%p;
	return ans;
}
int Phi(int x){
	int ans=x;
	for (int i=2;i*i<=x;i++)
	if (x%i==0){
		ans=ans/i*(i-1);
		while (x%i==0) x/=i;
	}
	if (x>1) ans=ans/x*(x-1);
	return ans;
}
const int N=2e5+10;
int n,m,q,w[N],phi[N],size;
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&w[i]);
	phi[0]=m;
	for (;phi[size]>1;size++) phi[size+1]=Phi(phi[size]);
	scanf("%d",&q);
	for (int i=1;i<=q;i++){
		int l,r,R;
		scanf("%d%d",&l,&r);
		R=min(r,l+size);
		double now=1;
		for (int i=min(r,R+10);i>R;i--) now=pow(w[i],now);
		int ans=1;
		for (int i=R,dep=R-l;i>=l;i--,dep--){
			int k=1,d=__gcd(w[i],phi[dep]);
			if (now>30) k=power(d,30,phi[dep]);else k=power(d,(int)round(now),phi[dep]);
			ans=power(w[i]/d,ans,phi[dep]);
			ans=1ll*ans*k%phi[dep];
			now=pow(w[i],now);
		}
		printf("%d\n",ans%m);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int power(int x,int y,int p){
	int ans=1;
	for (;y;y>>=1,x=1ll*x*x%p)
		if (y&1) ans=1ll*ans*x%p;
	return ans;
}
int Phi(int x){
	int ans=x;
	for (int i=2;i*i<=x;i++)
	if (x%i==0){
		ans=ans/i*(i-1);
		while (x%i==0) x/=i;
	}
	if (x>1) ans=ans/x*(x-1);
	return ans;
}
const int N=2e5+10;
int n,m,q,w[N],phi[N],size;
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&w[i]);
	phi[0]=m;
	for (;phi[size]>1;size++) phi[size+1]=Phi(phi[size]);
	phi[size+1]=1;
	scanf("%d",&q);
	for (int i=1;i<=q;i++){
		int l,r,R;
		scanf("%d%d",&l,&r);
		R=min(r,l+size);
		double now=1;
		for (int i=min(r,R+10);i>R;i--) now=pow(w[i],now);
		int ans=1;
		for (int i=R,dep=R-l;i>=l;i--,dep--){
			int k=1,d=__gcd(w[i],phi[dep]);
			if (now>=phi[dep+1]) ans=power(w[i],ans+phi[dep+1],phi[dep]);
			else ans=power(w[i],ans,phi[dep]);
//			if (now>phi[dep]) k=power(d,30,phi[dep]);
//				else k=power(d,(int)round(now),phi[dep]);
//			ans=power(w[i],ans+phi[dep+1],phi[dep]);
//			ans=power(w[i]/d,ans,phi[dep]);
//			ans=1ll*ans*k%phi[dep];
			now=pow(w[i],now);
		}
		printf("%d\n",ans%m);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
long long read(){
    long long xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
const int maxn=100010;
long long N,M,w[maxn];
int mod[maxn];
int phi(int x){
	int re=x;
	for(int i=2;i*i<=x;i++)
		if(x%i==0){
			re=re/i*(i-1);
			while(x%i==0)
				x/=i;
		}
	if(x!=1)
		re=re/x*(x-1);
	return re;
}
inline int fmod(long long x,int md){
	if(x>md)
		return x%md+md;
	return x;
}
long long mypow(long long b,long long p,int md){
	long long re=1;
	while(p){
		if(p&1)
			re=fmod(1LL*re*b,md);
		p>>=1;
		b=fmod(1LL*b*b,md);
	}
	return re;
}
int dfs(int L,int R,int mk){
	int md=mod[mk];
	if(L==R||md==1)
		return fmod(w[L],md);
	return mypow(w[L],dfs(L+1,R,mk+1),md);
}
int main(){
	//freopen("in","r",stdin);
	N=read(),M=read();
	mod[0]=M;
	for(int i=1;i<=N;i++)
		mod[i]=phi(mod[i-1]);
	for(int i=1;i<=N;i++)
		w[i]=read();
	for(int Q=read();Q;Q--){
		int t1=read(),t2=read();
		printf("%d\n",dfs(t1,t2,0)%M);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
int read(){
    int xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
const int maxn=100010;
int N,M,w[maxn];
int mod[maxn];
int phi(int x){
	int re=x;
	for(int i=2;i*i<=x;i++)
		if(x%i==0){
			re=re/i*(i-1);
			while(x%i==0)
				x/=i;
		}
	if(x!=1)
		re=re/x*(x-1);
	return re;
}
inline int fmod(long long x,int md){
	if(x>md)
		return x%md+md;
	return x;
}
int mypow(long long b,long long p,int md){
	int re=1;
	while(p){
		if(p&1)
			re=fmod(1LL*re*b,md);
		p>>=1;
		b=fmod(1LL*b*b,md);
	}
	return re;
}
int dfs(int L,int R,int mk){
	int md=mod[mk];
	if(L==R||md==1)
		return fmod(w[L],md);
	return mypow(w[L],dfs(L+1,R,mk+1),md);
}
int main(){
	//freopen("in","r",stdin);
	N=read(),M=read();
	mod[0]=M;
	for(int i=1;i<=N;i++)
		mod[i]=phi(mod[i-1]);
	for(int i=1;i<=N;i++)
		w[i]=read();
	for(int Q=read();Q;Q--){
		int t1=read(),t2=read();
		printf("%d\n",dfs(t1,t2,0)%M);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
int read(){
    int xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
const int maxn=100010;
int N,M,w[maxn];
int mod[maxn];
int phi(int x){
	int re=x;
	for(int i=2;i*i<=x;i++)
		if(x%i==0){
			re=re/i*(i-1);
			while(x%i==0)
				x/=i;
		}
	if(x!=1)
		re=re/x*(x-1);
	return re;
}
inline int fmod(long long x,int md){
	if(x>md)
		return x%md+md;
	return x;
}
int mypow(long long b,long long p,int md){
	int re=1;
	b=fmod(b,md);
	while(p){
		if(p&1)
			re=fmod(1LL*re*b,md);
		p>>=1;
		b=fmod(1LL*b*b,md);
	}
	return re;
}
int dfs(int L,int R,int mk){
	int md=mod[mk];
	if(L==R||md==1)
		return fmod(w[L],md);
	return mypow(w[L],dfs(L+1,R,mk+1),md);
}
int main(){
	//freopen("in","r",stdin);
	N=read(),M=read();
	mod[0]=M;
	for(int i=1;i<=N;i++)
		mod[i]=phi(mod[i-1]);
	for(int i=1;i<=N;i++)
		w[i]=read();
	for(int Q=read();Q;Q--){
		int t1=read(),t2=read();
		printf("%d\n",dfs(t1,t2,0)%M);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
long long read(){
    long long xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
const int maxn=100010;
long long N,M,w[maxn];
int mod[maxn];
int phi(int x){
	int re=x;
	for(int i=2;i*i<=x;i++)
		if(x%i==0){
			re=re/i*(i-1);
			while(x%i==0)
				x/=i;
		}
	if(x!=1)
		re=re/x*(x-1);
	return re;
}
inline int fmod(long long x,int md){
	if(x>md)
		return x%md+md;
	return x;
}
int mypow(long long b,long long p,int md){
	int re=1;
	while(p){
		if(p&1)
			re=fmod(1LL*re*b,md);
		p>>=1;
		b=fmod(1LL*b*b,md);
	}
	return re;
}
int dfs(int L,int R,int mk){
	int md=mod[mk];
	if(L==R||md==1)
		return fmod(w[L],md);
	return mypow(w[L],dfs(L+1,R,mk+1),md);
}
int main(){
	//freopen("in","r",stdin);
	N=read(),M=read();
	mod[0]=M;
	for(int i=1;i<=N;i++)
		mod[i]=phi(mod[i-1]);
	for(int i=1;i<=N;i++)
		w[i]=read();
	for(int Q=read();Q;Q--){
		int t1=read(),t2=read();
		printf("%d\n",dfs(t1,t2,0)%M);
	}
	return 0;
}