// THIS IS WRITTEN BY ME YAD YA FANLA
#include<iostream>
#define _CRT_SECURE_NO_WARNINGS
#include<queue>
#include<cmath>
#include<string >
#include<algorithm>
#include<deque>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;

#define ll long long
#define ull unsigned long long
int main ()
{
	std::ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		ll n,arr[100000]={0},freq1[100000]={0},sum =0;
		cin>>n;
		for (ll i=1 ;i<=n ;i++)
		{
		cin>>arr[i];
		freq1[i]=arr[i];

		arr[i]+=arr[i-1];
		}
	    	sum=0;
	    sort (freq1,freq1+n+1);
		for (ll i=1; i<=n ;i++)
		{
		freq1[i]+=freq1[i-1];
		}
		   ll q;
		   cin>>q;
		   for (ll j=1 ;j<=q ;j++)
		   {
		       ll t,x,y;
		       cin>>t >> x >>y;
		       if (t == 1)
		       {
		           cout<< arr[y] - arr[x-1]<<endl;
		       }
		      else  if (t == 2)
		       {

		            cout<< freq1[y] - freq1[x-1] << endl;
		       }
		   }
			//system("pause");
	return 0;

}


// THIS IS WRITTEN BY ME YAD YA FANLA
#include<iostream>
#define _CRT_SECURE_NO_WARNINGS
#include<queue>
#include<cmath>
#include<string >
#include<algorithm>
#include<deque>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;

#define ll long long
#define ull unsigned long long
int main ()
{
	std::ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		ll n,arr[100000]={0},freq1[1000000000]={0},freq2[1000000000]={0},sum =0;
		cin>>n;
		for (ll i=1 ;i<=n ;i++)
		{
		cin>>arr[i];
		sum+=arr[i];
		freq1[i]=sum;
		}
	    	sum=0;
	    sort (arr,arr+n+1);
		for (ll i=1; i<=n ;i++)
		{
		sum+=arr[i];
		freq2[i]=sum;
		}
		   ll q;
		   cin>>q;
		   for (ll j=1 ;j<=q ;j++)
		   {
		       ll t,x,y,mx=0,mn=0;
		       cin>>t >> x >>y;
		       if (t == 1)
		       {
		           mx=max(x,y);
		           mn=min(x,y);
		           cout<< freq1[mx] - freq1[mn-1]<<endl;
		       }
		      else  if (t == 2)
		       {
		           mx=max(x,y);
		           mn=min(x,y);
		            cout<< freq2[mx] - freq2[mn-1] << endl;
		       }
		   }
			//system("pause");
	return 0;

}


// THIS IS WRITTEN BY ME YAD YA FANLA
#include<iostream>
#define _CRT_SECURE_NO_WARNINGS
#include<queue>
#include<cmath>
#include<string >
#include<algorithm>
#include<deque>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;

#define ll long long
#define ull unsigned long long
int main ()
{
	std::ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		ll n,arr[100000]={0},freq1[10000000000]={0},freq2[10000000000]={0},sum =0;
		cin>>n;
		for (ll i=1 ;i<=n ;i++)
		{
		cin>>arr[i];
		sum+=arr[i];
		freq1[i]=sum;
		}
	    	sum=0;
	    sort (arr,arr+n+1);
		for (ll i=1; i<=n ;i++)
		{
		sum+=arr[i];
		freq2[i]=sum;
		}
		   ll q;
		   cin>>q;
		   for (ll j=1 ;j<=q ;j++)
		   {
		       ll t,x,y,mx=0,mn=0;
		       cin>>t >> x >>y;
		       if (t == 1)
		       {
		           mx=max(x,y);
		           mn=min(x,y);
		           cout<< freq1[mx] - freq1[mn-1]<<endl;
		       }
		      else  if (t == 2)
		       {
		           mx=max(x,y);
		           mn=min(x,y);
		            cout<< freq2[mx] - freq2[mn-1] << endl;
		       }
		   }
			//system("pause");
	return 0;

}


// THIS IS WRITTEN BY ME YAD YA FANLA
#include<iostream>
#define _CRT_SECURE_NO_WARNINGS
#include<queue>
#include<cmath>
#include<string >
#include<algorithm>
#include<deque>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;

#define ll long long
#define ull unsigned long long
int main ()
{
	std::ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		ll n,arr1[100000]={0},arr2[100000]={0};
		cin>>n;
		for (ll i=1 ;i<=n ;i++)
		{
		cin>>arr1[i];
		arr2[i]=arr1[i];
		}
		sort (arr2,arr2+n+1);
		for (ll i=1 ;i<=n ;i++)
		{
		arr1[i] = arr1[i] + arr1[i-1];
		arr2[i] = arr2[i] + arr2[i-1];
		}
		 ll q;
		 cin >> q;
		 for (ll j=1 ;j<=q ;j++)
		 {
		 ll t,x,y;
		 cin>>t>>x>>y;
		 if (t == 1 )
			 cout << arr1[y] - arr1[x-1]<<endl;
		 else if (t == 2)
			 cout << arr2[y] - arr2[x-1] << endl;
		 }

			//system("pause");
	return 0;

}


// THIS IS WRITTEN BY ME YAD YA FANLA
#include<iostream>
#define _CRT_SECURE_NO_WARNINGS
#include<queue>
#include<cmath>
#include<string >
#include<algorithm>
#include<deque>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;

#define ll long long
#define ull unsigned long long
int main ()
{
	std::ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		ll n,arr1[100000]={0},arr2[100000]={0};
		cin>>n;
		for (ll i=1 ;i<=n ;i++)
		{
		cin>>arr1[i];
		arr2[i]=arr1[i];
		}
		sort (arr2,arr2+n+1);
		for (ll i=1 ;i<=n ;i++)
		{
		arr1[i] = arr1[i] + arr1[i-1];
		arr2[i] = arr2[i] + arr2[i-1];
		}
		 ll q;
		 cin >> q;
		 for (ll j=1 ;j<=q ;j++)
		 {
		 ll t,x,y;
		 cin>>t>>x>>y;
		 if (t == 1 )
			 cout << arr1[y] - arr1[x-1]<<endl;
		 else if (t == 2)
			 cout << arr2[y] - arr2[x-1] << endl;
		 }
		 cout << endl;
			//system("pause");
	return 0;

}


#include<bits/stdc++.h>
#include<string>

using namespace std;

#define ull                  unsigned long long
#define ll                   long long
#define ff                   first
#define ss                   second
#define dance()              std::ios_base::sync_with_stdio(0);cin.tie(NULL);
#define pb                   push_back

string to_small(string x )
{
for (ll i=0 ;i<x .size() ;i++)
{
    if (x[i]>=65 && x[i]<=90)
        x[i]=x[i]-'A'+'a';
}
return x;
}

string to_capital (string x)
{
    for (ll i=0 ;i<x.size() ; i++)
    {
        if (x[i]>=97 && x[i]<=122)
            x[i]=x[i]-'a'+'A';
    }
    return x;
}

ll fact (ll n)
{
    ll f=1;
    for (ll i=1 ;i<=n ;i++)
        f*=i;
    return f;
}

bool is_prime (ll n)
{
     bool flag=true ;
    if (n==1)
        flag=false;
    else
    {
    ll c=0;
    for (ll i=2 ;i<=sqrt(n) ;i++)
    {
        if (n %i ==0)
            c++;
    }
     (c>0) ? flag=false : flag =true ;
    }
    return flag;
}

ll gcd(ll x, ll y)
{
   while(y!=0)
	{
		ll temp=y;
		y=x%y;
		x=temp;
	}
	if(x<0)
		x*=-1;
	return x;
}

ll lcm (ll x ,ll y)
{
    ll a=x;
    ll b=y;
    while (y!=0)
    {
        ll temp=y;
        y=x%y;
        x=temp;
    }
    if (x<0)x*=-1;
     a=a*b;
     if (a<0)a*=-1;
             return (a/ x) ;
}

//THE_CODE_IS_HERE .....

int main()
{
   // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);
dance() ;
ll n;
cin >> n ;
ll arr1[n] ;
ll arr2[n] ;
for (ll i=0 ;i<n ;i++)
{
    cin >>arr1[i] ;
    arr2[i]=arr1[i] ;
}
sort(arr2,arr2+n) ;
ll pre1[100000]={0} ;
ll pre2[100000]={0} ;
for (ll i=0 ;i<n ;i++)
{
    if (i==0)
    pre1[i]+=arr1[i] ;
    else
        pre1[i]+=arr1[i]+pre1[i-1] ;
}
for (ll i=0 ;i<n ;i++)
{
    if (i==0)
    pre2[i]+=arr2[i] ;
    else
        pre2[i]+=arr2[i]+pre2[i-1] ;
}
ll m;
cin >> m ;
for (ll i=0 ;i<m ;i++)
{
    ll so,l,r;
    cin >> so >> l >> r;
    if (so==1)
    {
        if (l!=r && l!=1)
        cout << pre1[r-1]-pre1[l-2] << endl ;
        else if (l==r)
            cout << arr1[l-1] << endl ;
        else if (l==1)
            cout << pre1[r-1] << endl ;
    }
    else
    {
         if (l!=r && l!=1)
         cout << pre2[r-1]-pre2[l-2] << endl ;
         else if (l==r)
            cout << arr2[l-1] << endl ;
            else if (l==1)
                cout << pre2[r-1]  << endl ;

    }
}

    return 0;
}


// THIS IS WRITTEN BY ME YAD YA FANLA
#include<iostream>
#define _CRT_SECURE_NO_WARNINGS
#include<queue>
#include<cmath>
#include<string >
#include<algorithm>
#include<deque>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;

#define ll long long
#define ull unsigned long long
int main ()
{
	std::ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		ll n,arr[100000]={0},freq1[100000]={0},freq2[1000000]={0},sum =0;
		cin>>n;
		for (ll i=1 ;i<=n ;i++)
		{
		cin>>arr[i];
		sum+=arr[i];
		freq1[i]=sum;
		}
	    	sum=0;
	    sort (arr,arr+n+1);
		for (ll i=1; i<=n ;i++)
		{
		sum+=arr[i];
		freq2[i]=sum;
		}
		   ll q;
		   cin>>q;
		   for (ll j=1 ;j<=q ;j++)
		   {
		       ll t,x,y,mx=0,mn=0;
		       cin>>t >> x >>y;
		       if (t == 1)
		       {
		           mx=max(x,y);
		           mn=min(x,y);
		           cout<< freq1[mx] - freq1[mn-1]<<endl;
		       }
		      else  if (t == 2)
		       {
		           mx=max(x,y);
		           mn=min(x,y);
		            cout<< freq2[mx] - freq2[mn-1] << endl;
		       }
		   }
			//system("pause");
	return 0;

}


#include <iostream>
#include<algorithm>
#include <functional>
using namespace std;

int main()
{
    long n,i,m,o,l,r,sum;

    cin>>n;
    long arr[n],arr1[n];
    for( i=0; i<n; i++)
    {
        cin>>arr[i];
        arr1[i]=arr[i];
    }
    sort(arr1, arr1+n );
    cin>>m;
    for( i=0; i<m; i++)
    {
        cin>>o;
        sum=0;
        if(o==1)

        {
            cin>>l>>r;
            for(int j=l-1; j<r; j++)
                sum+=arr[j];

        }
        else
        {
            cin>>l>>r;
            for(int k=l-1; k<r; k++)
                sum+=arr1[k];

        }

        cout<<sum<<endl;
    }

    return 0;
}


#include <iostream>
#include<algorithm>
#include <functional>
using namespace std;

int main()
{
   unsigned long long int n,i,m,o,l,r,sum;

    cin>>n;
     unsigned long long arr[n],arr1[n];
    for( i=0; i<n; i++)
    {
        cin>>arr[i];
        arr1[i]=arr[i];
    }
    sort(arr1, arr1+n+1 );
    cin>>m;
    for( i=0; i<m; i++)
    {
        cin>>o;
        sum=0;
        if(o==1)

        {
            cin>>l>>r;
            for( unsigned  long long j=l-1; j<r; j++)
                sum+=arr[j];

        }
        else
        {
            cin>>l>>r;
            for( unsigned long long k=l-1; k<r; k++)
                sum+=arr1[k];

        }

        cout<<sum<<endl;
    }

    return 0;
}


#include <iostream>
#include<algorithm>
#include <functional>
using namespace std;

int main()
{
    long long int n,i,m,o,l,r,sum;

    cin>>n;
    long long arr[n],arr1[n];
    for( i=0; i<n; i++)
    {
        cin>>arr[i];
        arr1[i]=arr[i];
    }
    sort(arr1, arr1+n+1 );
    cin>>m;
    for( i=0; i<m; i++)
    {
        cin>>o;
        sum=0;
        if(o==1)

        {
            cin>>l>>r;
            for( long long j=l-1; j<r; j++)
                sum+=arr[j];

        }
        else
        {
            cin>>l>>r;
            for(long long k=l-1; k<r; k++)
                sum+=arr1[k];

        }

        cout<<sum<<endl;
    }

    return 0;
}


#include <iostream>
#include<algorithm>
#include <functional>
using namespace std;

int main()
{
   unsigned long long  n,i,m,o,l,r,sum;

    cin>>n;
     unsigned long long arr[n+5],arr1[n+5];
     arr[0]=0;arr1[0]=0;
    for( i=1; i<=n; i++)
    {
        cin>>arr[i];
        arr1[i]=arr[i];
    }
    sort(arr1, arr1+n );
    cin>>m;
    for(i=1;i<=n;i++)
    {arr[i]+=arr[i-1];
        arr1[i]+=arr1[i-1];
    }
    for( i=0; i<m; i++)
    {
        cin>>o;

        if(o==1)

        {
            cin>>l>>r;
            cout<<arr[r]-arr[l-1]<<endl;

        }
        else
        {
            cin>>l>>r;
             cout<<arr1[r]-arr1[l-1]<<endl;

        }

    }

    return 0;
}


#include <iostream>
#include<algorithm>
#include <functional>
using namespace std;

int main()
{
    long n,i,m,o,l,r,sum;

    cin>>n;
    long arr[n],arr1[n];
    for( i=0; i<n; i++)
    {
        cin>>arr[i];
        arr1[i]=arr[i];
    }
    sort(arr1, arr1+n );
    cin>>m;
    for( i=0; i<m; i++)
    {
        cin>>o;
        sum=0;
        if(o==1)

        {
            cin>>l>>r;
            for(long j=l-1; j<r; j++)
                sum+=arr[j];

        }
        else
        {
            cin>>l>>r;
            for(long k=l-1; k<r; k++)
                sum+=arr1[k];

        }

        cout<<sum<<endl;
    }

    return 0;
}


#include <iostream>
#include<algorithm>
#include <functional>
using namespace std;

int main()
{
   unsigned long long  n,i,m,o,l,r,sum;

    cin>>n;
     unsigned long long arr[n],arr1[n];
    for( i=0; i<n; i++)
    {
        cin>>arr[i];
        arr1[i]=arr[i];
    }
    sort(arr1, arr1+n+1 );
    cin>>m;
    for( i=0; i<m; i++)
    {
        cin>>o;
        sum=0;
        if(o==1)

        {
            cin>>l>>r;
            for( unsigned  long long j=l-1; j<r; j++)
                sum+=arr[j];

        }
        else
        {
            cin>>l>>r;
            for( unsigned long long k=l-1; k<r; k++)
                sum+=arr1[k];

        }

        cout<<sum<<endl;
    }

    return 0;
}


#include <iostream>
#include<algorithm>
#include <functional>
using namespace std;

int main()
{
    unsigned long long  n,i,m,o,l,r;

    cin>>n;
    unsigned long long arr[n+5],arr1[n+5];
    arr[0]=0;
    arr1[0]=0;
    for( i=1; i<=n; i++)
    {
        cin>>arr[i];
        arr1[i]=arr[i];
    }
    sort(arr1+1, arr1+n+1 );
    cin>>m;
    for(i=1; i<=n; i++)
    {
        arr[i]+=arr[i-1];
        arr1[i]+=arr1[i-1];
    }

    for( i=0; i<m; i++)
    {
        cin>>o;

        if(o==1)

        {
            cin>>l>>r;
            cout<<arr[r]-arr[l-1]<<endl;

        }
        else
        {
            cin>>l>>r;
            cout<<arr1[r]-arr1[l-1]<<endl;

        }

    }

    return 0;
}


#include <iostream>
#include<algorithm>
#include <functional>
using namespace std;

int main()
{
    unsigned long long n,i,m,o,l,r,sum;

    cin>>n;
    unsigned long long arr[n],arr1[n];
    for( i=0; i<n; i++)
    {
        cin>>arr[i];
        arr1[i]=arr[i];
    }
    sort(arr1, arr1+n );
    cin>>m;
    for( i=0; i<m; i++)
    {
        cin>>o;
        sum=0;
        if(o==1)

        {
            cin>>l>>r;
            for(unsigned long long j=l-1; j<r; j++)
                sum+=arr[j];

        }
        else
        {
            cin>>l>>r;
            for(unsigned long long k=l-1; k<r; k++)
                sum+=arr1[k];

        }

        cout<<sum<<endl;
    }

    return 0;
}


#include <iostream>
#include<algorithm>
#include <functional>
using namespace std;

int main()
{
    unsigned long long  n,i,m,o,l,r;

    cin>>n;
    unsigned long long arr[n+5],arr1[n+5];
    arr[0]=0;
    arr1[0]=0;
    for( i=1; i<=n; i++)
    {
        cin>>arr[i];
        arr1[i]=arr[i];
    }
    sort(arr1+1, arr1+n+1 );
    cin>>m;
    for(i=1; i<=n; i++)
    {
        arr[i]+=arr[i-1];
        arr1[i]+=arr1[i-1];
    }
    for(i=0; i<=n; i++)
    {
        cout<<arr1[i]<<endl;
    }

    for( i=0; i<m; i++)
    {
        cin>>o;

        if(o==1)

        {
            cin>>l>>r;
            cout<<arr[r]-arr[l-1]<<endl;

        }
        else
        {
            cin>>l>>r;
            cout<<arr1[r]-arr1[l-1]<<endl;

        }

    }

    return 0;
}


#include <iostream>
#include<algorithm>
#include <functional>
using namespace std;

int main()
{
   unsigned long long  n,i,m,o,l,r,sum;

    cin>>n;
     unsigned long long arr[n],arr1[n];
    for( i=0; i<n; i++)
    {
        cin>>arr[i];
        arr1[i]=arr[i];
    }
    sort(arr1, arr1+n );
    cin>>m;
    for( i=0; i<m; i++)
    {
        cin>>o;
        sum=0;
        if(o==1)

        {
            cin>>l>>r;
            for( unsigned  long long j=l-1; j<r; j++)
                sum+=arr[j];

        }
        else
        {
            cin>>l>>r;
            for( unsigned long long k=l-1; k<r; k++)
                sum+=arr1[k];

        }

        cout<<sum<<endl;
    }

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main()
{
    unsigned long long n,m,k,r,l;
    cin>>n;
    int arr[n],arr2[n];
    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
        arr2[i]=arr[i];

    }
    sort(arr2,arr2+n);
    cin>>k;
    for(unsigned long long i=0; i<k; i++)
    {
        unsigned long long sum=0;
        cin>>m>>r>>l;
        for(unsigned long long j=r-1; j<l; j++)
            {if(m==1)
                sum+=arr[j];
            else
sum+=arr2[j];}

        cout<<sum<<endl;

    }

    return 0;
}


/*  Coded by A man
Current goal :- passing my semester exams.. (Expiry Date :- 15th December,2019)
CF :- LONG TIME NO SEE
ME :- YEAH ...FUCK INDIAN EDUCATION SYSTEM....FKKK
*/
#include<bits/stdc++.h>
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define maxsize 200001
#define w() while(tc--)
#define atg aman the great
#define max3(a,b,c) max(max(a,b), c)
#define min3(a,b,c) min (min(a,b), c)
#define mod 1000000007
#define INF INT_MAX
#define for_i(n) for( long long i=0; i<n; i++)
#define for_j(n) for(long long j=0; j<n; j++)
#define for_1(n) for(long long xyz = 1; xyz<n; xyz++)
#define ifDiv(n,i) if(n%i==0)
#define pi 3.141592653589793238
#include<time.h>
using namespace std;

ll arr1[10001][10001] = {0};
ll sorta1[10000][10001] = {0};
ll n;
ll arr[100001],sorta[100001];

ll f1(ll x, ll y){
    ll sum(0);
    if(x>10000||y>10000){
        for(ll j=y; j>=x; j--){
            if(j<=10000&&x<=10000)
            {
                sum += f1(x,j);
                return sum;
            }
            sum+=arr[j];
        }
    }
    if(arr1[x][y]>0)return arr1[x][y];
    if(x==y){
        arr1[x][y] = arr[x];
        return arr[x];
    }
    arr1[x][y] = f1(x,y-1) + arr[y];
    return arr1[x][y];
}
// for sorted

ll f2(ll x, ll y){
    ll sum(0);

    if(x>10000||y>10000){
        for(ll j=y; j>=x; j--){
            if(j<=10000&&x<=10000)
            {
                sum += f2(x,j);
                return sum;
            }
            sum+=sorta[j];
        }
    }
    if(sorta1[x][y]>0)return sorta1[x][y];
    if(x==y){
        sorta1[x][y] = sorta[x];
        return sorta[x];
    }
    sorta1[x][y] = f2(x,y-1) + sorta[y];
    return sorta1[x][y];
}

void sol(){
    cin >> n;
    ll t,nq;
    ll type,x,y;

    for_i(n){
        cin >>t;
        arr[i] = sorta[i]= t;
    }
    sort(sorta,sorta+n);
    cin >> nq;
    while(nq--){
        cin >> type>>x>>y;
        x--; y--;
        if(type==1){
            cout<< f1(x,y);
        }
        else
        {
            cout<< f2(x,y);
        }
        cout<<"\n";
    }

}

 // don't dlt this

int main(){
    fast
    ll tc= 1;
    //cin >> tc;

    while(tc--){
        sol();
    }

}


/*  Coded by A man
Current goal :- passing my semester exams.. (Expiry Date :- 15th December,2019)
CF :- LONG TIME NO SEE
ME :- YEAH ...FUCK INDIAN EDUCATION SYSTEM....FKKK
*/
#include<bits/stdc++.h>
#define ll int64_t
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define maxsize 200001
#define w() while(tc--)
#define atg aman the great
#define max3(a,b,c) max(max(a,b), c)
#define min3(a,b,c) min (min(a,b), c)
#define mod 1000000007
#define INF INT_MAX
#define for_i(n) for( long long i=0; i<n; i++)
#define for_j(n) for(long long j=0; j<n; j++)
#define for_1(n) for(long long xyz = 1; xyz<n; xyz++)
#define ifDiv(n,i) if(n%i==0)
#define pi 3.141592653589793238
#include<time.h>
using namespace std;

ll arr1[1001][1001] = {0};
ll sorta1[1001][1001] = {0};
ll n;
ll arr[100001],sorta[100001];

ll f1(ll x, ll y){
    ll sum(0);
    if(x>1000||y>1000){
        for(ll j=y; j>=x; j--){
            if(j<=1000&&x<=1000)
            {
                sum += f1(x,j);
                return sum;
            }
            sum+=arr[j];
        }
    }
    if(arr1[x][y]>0)return arr1[x][y];
    if(x==y){
        arr1[x][y] = arr[x];
        return arr[x];
    }
    arr1[x][y] = f1(x,y-1) + arr[y];
    return arr1[x][y];
}
// for sorted

ll f2(ll x, ll y){
    ll sum(0);
    if(x<0||y<0)return 0;
    if(x>1000||y>1000){
        for(ll j=y; j>=x; j--){
            if(j<=1000&&x<=1000)
            {
                sum += f2(x,j);
                return sum;
            }
            sum+=sorta[j];
        }
    }
    if(sorta1[x][y]>0)return sorta1[x][y];
    if(x==y){
        sorta1[x][y] = sorta[x];
        return sorta[x];
    }
    sorta1[x][y] = f2(x,y-1) + sorta[y];
    return sorta1[x][y];
}

void sol(){
    cin >> n;
    ll t,nq;
    ll type,x,y;

    for_i(n){
        cin >>t;
        arr[i] = sorta[i]= t;
    }
    sort(sorta,sorta+n);
    cin >> nq;
    while(nq--){
        cin >> type>>x>>y;
        x--; y--;
        if(type==1){
            cout<< f1(x,y);
        }
        else
        {
            cout<< f2(x,y);
        }
        cout<<"\n";
    }

}

 // don't dlt this

int main(){
    fast
    ll tc= 1;
    //cin >> tc;

    while(tc--){
        sol();
    }

}


/*  Coded by A man
Current goal :- passing my semester exams.. (Expiry Date :- 15th December,2019)
CF :- LONG TIME NO SEE
ME :- YEAH ...FUCK INDIAN EDUCATION SYSTEM....FKKK
*/
#include<bits/stdc++.h>
#define ll int64_t
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define maxsize 200001
#define w() while(tc--)
#define atg aman the great
#define max3(a,b,c) max(max(a,b), c)
#define min3(a,b,c) min (min(a,b), c)
#define mod 1000000007
#define INF INT_MAX
#define for_i(n) for( long long i=0; i<n; i++)
#define for_j(n) for(long long j=0; j<n; j++)
#define for_1(n) for(long long xyz = 1; xyz<n; xyz++)
#define ifDiv(n,i) if(n%i==0)
#define pi 3.141592653589793238
#include<time.h>
using namespace std;

ll arr1[1001][1001] = {0};
ll sorta1[1001][1001] = {0};
ll n;
ll arr[100001],sorta[100001];

ll f1(ll x, ll y){
    ll sum(0);
    if(x>1000||y>1000){
        for(ll j=y; j>=x; j--){
            if(j<=1000&&x<=1000)
            {
                sum += f1(x,j);
                return sum;
            }
            sum+=arr[j];
        }
    }
    if(arr1[x][y]>0)return arr1[x][y];
    if(x==y){
        arr1[x][y] = arr[x];
        return arr[x];
    }
    arr1[x][y] = f1(x,y-1) + arr[y];
    return arr1[x][y];
}
// for sorted

ll f2(ll x, ll y){
    ll sum(0);

    if(x>1000||y>1000){
        for(ll j=y; j>=x; j--){
            if(j<=1000&&x<=1000)
            {
                sum += f2(x,j);
                return sum;
            }
            sum+=sorta[j];
        }
    }
    if(sorta1[x][y]>0)return sorta1[x][y];
    if(x==y){
        sorta1[x][y] = sorta[x];
        return sorta[x];
    }
    sorta1[x][y] = f2(x,y-1) + sorta[y];
    return sorta1[x][y];
}

void sol(){
    cin >> n;
    ll t,nq;
    ll type,x,y;

    for_i(n){
        cin >>t;
        arr[i] = sorta[i]= t;
    }
    sort(sorta,sorta+n);
    cin >> nq;
    while(nq--){
        cin >> type>>x>>y;
        x--; y--;
        if(type==1){
            cout<< f1(x,y);
        }
        else
        {
            cout<< f2(x,y);
        }
        cout<<"\n";
    }

}

 // don't dlt this

int main(){
    fast
    ll tc= 1;
    //cin >> tc;

    while(tc--){
        sol();
    }

}


/*  Coded by A man
Current goal :- passing my semester exams.. (Expiry Date :- 15th December,2019)
CF :- LONG TIME NO SEE
ME :- YEAH ...FUCK INDIAN EDUCATION SYSTEM....FKKK
*/
#include<bits/stdc++.h>
#define ll int64_t
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define maxsize 200001
#define w() while(tc--)
#define atg aman the great
#define max3(a,b,c) max(max(a,b), c)
#define min3(a,b,c) min (min(a,b), c)
#define mod 1000000007
#define INF INT_MAX
#define for_i(n) for( long long i=0; i<n; i++)
#define for_j(n) for(long long j=0; j<n; j++)
#define for_1(n) for(long long xyz = 1; xyz<n; xyz++)
#define ifDiv(n,i) if(n%i==0)
#define pi 3.141592653589793238
#include<time.h>
using namespace std;

ll arr1[4001][4001] = {0};
ll sorta1[4000][4001] = {0};
ll n;
ll arr[100001],sorta[100001];

ll f1(ll x, ll y){
    ll sum(0);
    if(x>1000||y>1000){
        for(ll j=y; j>=x; j--){
            if(j<=1000&&x<=1000)
            {
                sum += f1(x,j);
                return sum;
            }
            sum+=arr[j];
        }
    }
    if(arr1[x][y]>0)return arr1[x][y];
    if(x==y){
        arr1[x][y] = arr[x];
        return arr[x];
    }
    arr1[x][y] = f1(x,y-1) + arr[y];
    return arr1[x][y];
}
// for sorted

ll f2(ll x, ll y){
    ll sum(0);
    if(x<0||y<0)return 0;
    if(x>1000||y>1000){
        for(ll j=y; j>=x; j--){
            if(j<=1000&&x<=1000)
            {
                sum += f2(x,j);
                return sum;
            }
            sum+=sorta[j];
        }
    }
    if(sorta1[x][y]>0)return sorta1[x][y];
    if(x==y){
        sorta1[x][y] = sorta[x];
        return sorta[x];
    }
    sorta1[x][y] = f2(x,y-1) + sorta[y];
    return sorta1[x][y];
}

void sol(){
    cin >> n;
    ll t,nq;
    ll type,x,y;

    for_i(n){
        cin >>t;
        arr[i] = sorta[i]= t;
    }
    sort(sorta,sorta+n);
    cin >> nq;
    while(nq--){
        cin >> type>>x>>y;
        x--; y--;
        if(type==1){
            cout<< f1(x,y);
        }
        else
        {
            cout<< f2(x,y);
        }
        cout<<"\n";
    }

}

 // don't dlt this

int main(){
    fast
    ll tc= 1;
    //cin >> tc;

    while(tc--){
        sol();
    }

}


/*  Coded by A man
Current goal :- passing my semester exams.. (Expiry Date :- 15th December,2019)
CF :- LONG TIME NO SEE
ME :- YEAH ...FUCK INDIAN EDUCATION SYSTEM....FKKK
*/
#include<bits/stdc++.h>
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define maxsize 200001
#define w() while(tc--)
#define atg aman the great
#define max3(a,b,c) max(max(a,b), c)
#define min3(a,b,c) min (min(a,b), c)
#define mod 1000000007
#define INF INT_MAX
#define for_i(n) for( long long i=0; i<n; i++)
#define for_j(n) for(long long j=0; j<n; j++)
#define for_1(n) for(long long xyz = 1; xyz<n; xyz++)
#define ifDiv(n,i) if(n%i==0)
#define pi 3.141592653589793238
#include<time.h>
using namespace std;
/*
6
6 4 2 7 2 7
3
2 3 6
1 3 4
1 1 6
*/
ll arr1[10001][10001] = {0};
ll sorta1[10000][10001] = {0};
ll n;     ll arr[100001],sorta[100001];
ll f1(ll x, ll y){
    ll sum(0);

    if(x>10000||y>10000){
        for(ll j=y; j>=x; j--){
            if(j<=10000&&x<=10000)
            {
                sum += f1(x,j);
                return sum;
            }
            sum+=arr[j];
        }
    }
    if(arr1[x][y]>0)return arr1[x][y];
    if(x==y){
        arr1[x][y] = arr[x];
        return arr[x];
    }
    arr1[x][y] = f1(x,y-1) + arr[y];
    return arr1[x][y];
    /*
    cout<<arr1[x][y];
    cout<<"\n"<<arr[y];
    if(arr1[x][y]>0)return arr1[x][y];
    if(x==y)return arr[x];
    arr1[x][y] = f1(x,y-1)+arr[y];
    return arr1[x][y]; */

}
// for sorted

ll f2(ll x, ll y){
    ll sum(0);

    if(x>10000||y>10000){
        for(ll j=y; j>=x; j--){
            if(j<=10000&&x<=10000)
            {
                sum += f2(x,j);
                return sum;
            }
            sum+=sorta[j];
        }
    }
    if(sorta1[x][y]>0)return sorta1[x][y];
    if(x==y){
        sorta1[x][y] = sorta[x];
        return sorta[x];
    }
    sorta1[x][y] = f2(x,y-1) + sorta[y];
    return sorta1[x][y];
    /*
        if(sorta1[x][y]>=0)return sorta1[x][y];
        if(x==y)return sorta[x];
        sorta1[x][y] = f2(x,y-1)+sorta[y];
        return sorta1[x][y];*/
}

void sol(){
    cin >> n;
    ll t,nq;
    ll type,x,y;

    for_i(n){
        cin >>t;
        arr[i] = sorta[i]= t;
    }
    sort(sorta,sorta+n);
    cin >> nq;
    while(nq--){
        cin >> type>>x>>y;
        x--; y--;
        if(type==1){
            cout<< f1(x,y);
        }
        else
        {
            cout<< f2(x,y);
        }
        cout<<"\n";
    }

}

 // don't dlt this

int main(){
    fast
    ll tc= 1;
    //cin >> tc;

    while(tc--){
        sol();
    }

}


/*  Coded by A man
Current goal :- passing my semester exams.. (Expiry Date :- 15th December,2019)
CF :- LONG TIME NO SEE
ME :- YEAH ...FUCK INDIAN EDUCATION SYSTEM....FKKK
*/
#include<bits/stdc++.h>
#define ll int64_t
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define maxsize 200001
#define w() while(tc--)
#define atg aman the great
#define max3(a,b,c) max(max(a,b), c)
#define min3(a,b,c) min (min(a,b), c)
#define mod 1000000007
#define INF INT_MAX
#define for_i(n) for( long long i=0; i<n; i++)
#define for_j(n) for(long long j=0; j<n; j++)
#define for_1(n) for(long long i = 1; i<=n; i++)
#define ifDiv(n,i) if(n%i==0)
#define pi 3.141592653589793238
#include<time.h>
using namespace std;

void sol(){
    ll n;
    cin >> n;
    ll t,nq,arr[n+2],sorta[n+2];
    ll type,x,y;

    for_1(n){
        cin >>t;
        arr[i] = sorta[i]= t;
    }
    arr[0] = sorta[0] = 0;
    sort(sorta+1,sorta+n+1);
    for_i(n){
        arr[i+1] =arr[i]+arr[i+1];
        cout<< arr[i+1]<<endl;
        sorta[i+1] = sorta[i]+sorta[i+1];
    }
    for_j(n){
        cout<< arr[j]<<"\t" << sorta[j]<<endl;
    }
    cin >> nq;
    while(nq--){
        cin >> type>>x>>y;
        x--;
        if(type==1){
            cout<< arr[y]-arr[x];
        }
        else cout<< sorta[y] - sorta[x];
        cout<< "\n";
    }
}

 // don't dlt this

int main(){
    fast
    ll tc= 1;
    //cin >> tc;

    while(tc--){
        sol();
    }

}


/*  Coded by A man
Current goal :- passing my semester exams.. (Expiry Date :- 15th December,2019)
CF :- LONG TIME NO SEE
ME :- YEAH ...FUCK INDIAN EDUCATION SYSTEM....FKKK
*/
#include<bits/stdc++.h>
#define ll int64_t
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define maxsize 200001
#define w() while(tc--)
#define atg aman the great
#define max3(a,b,c) max(max(a,b), c)
#define min3(a,b,c) min (min(a,b), c)
#define mod 1000000007
#define INF INT_MAX
#define for_i(n) for( long long i=0; i<n; i++)
#define for_j(n) for(long long j=0; j<n; j++)
#define for_1(n) for(long long i = 1; i<=n; i++)
#define ifDiv(n,i) if(n%i==0)
#define pi 3.141592653589793238
#include<time.h>
using namespace std;

void sol(){
    ll n;
    cin >> n;
    ll t,nq,arr[n+2],sorta[n+2];
    ll type,x,y;

    for_1(n){
        cin >>t;
        arr[i] = sorta[i]= t;
    }
    arr[0] = sorta[0] = 0;
    sort(sorta+1,sorta+n+1);
    for_i(n){
        arr[i+1] =arr[i]+arr[i+1];
       // cout<< arr[i+1]<<endl;
        sorta[i+1] = sorta[i]+sorta[i+1];
    }

    cin >> nq;
    while(nq--){
        cin >> type>>x>>y;
        x--;
        if(type==1){
            cout<< arr[y]-arr[x];
        }
        else cout<< sorta[y] - sorta[x];
        cout<< "\n";
    }
}

 // don't dlt this

int main(){
    fast
    ll tc= 1;
    //cin >> tc;

    while(tc--){
        sol();
    }

}


/*  Coded by A man
Current goal :- passing my semester exams.. (Expiry Date :- 15th December,2019)
CF :- LONG TIME NO SEE
ME :- YEAH ...FUCK INDIAN EDUCATION SYSTEM....FKKK
*/
#include<bits/stdc++.h>
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define maxsize 200001
#define w() while(tc--)
#define atg aman the great
#define max3(a,b,c) max(max(a,b), c)
#define min3(a,b,c) min (min(a,b), c)
#define mod 1000000007
#define INF INT_MAX
#define for_i(n) for( long long i=0; i<n; i++)
#define for_j(n) for(long long j=0; j<n; j++)
#define for_1(n) for(long long xyz = 1; xyz<n; xyz++)
#define ifDiv(n,i) if(n%i==0)
#define pi 3.141592653589793238
#include<time.h>
using namespace std;

ll arr1[1001][1001] = {0};
ll sorta1[1001][1001] = {0};
ll n;
ll arr[100001],sorta[100001];

ll f1(ll x, ll y){
    ll sum(0);
    if(x>1000||y>1000){
        for(ll j=y; j>=x; j--){
            if(j<=1000&&x<=1000)
            {
                sum += f1(x,j);
                return sum;
            }
            sum+=arr[j];
        }
    }
    if(arr1[x][y]>0)return arr1[x][y];
    if(x==y){
        arr1[x][y] = arr[x];
        return arr[x];
    }
    arr1[x][y] = f1(x,y-1) + arr[y];
    return arr1[x][y];
}
// for sorted

ll f2(ll x, ll y){
    ll sum(0);

    if(x>1000||y>1000){
        for(ll j=y; j>=x; j--){
            if(j<=1000&&x<=1000)
            {
                sum += f2(x,j);
                return sum;
            }
            sum+=sorta[j];
        }
    }
    if(sorta1[x][y]>0)return sorta1[x][y];
    if(x==y){
        sorta1[x][y] = sorta[x];
        return sorta[x];
    }
    sorta1[x][y] = f2(x,y-1) + sorta[y];
    return sorta1[x][y];
}

void sol(){
    cin >> n;
    ll t,nq;
    ll type,x,y;

    for_i(n){
        cin >>t;
        arr[i] = sorta[i]= t;
    }
    sort(sorta,sorta+n);
    cin >> nq;
    while(nq--){
        cin >> type>>x>>y;
        x--; y--;
        if(type==1){
            cout<< f1(x,y);
        }
        else
        {
            cout<< f2(x,y);
        }
        cout<<"\n";
    }

}

 // don't dlt this

int main(){
    fast
    ll tc= 1;
    //cin >> tc;

    while(tc--){
        sol();
    }

}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n;

    int arr[n+1];
    vector<int> arr2;
    arr2.push_back(0);
    for (int i = 1; i <= n; i++){
        int a;
        cin >> a;
        arr[i] = a;
        arr2.push_back(a);
    }

    sort(arr2.begin(), arr2.end());

    cin >> m;

    for (int i = 0; i < m; i++){
        unsigned long long out = 0;
        int a, b, c;
        cin >> a >> b >> c;
        if (a == 1){
            for (int i = b; i <= c; i++){
                out += arr[i];
            }
        }
        else{
            for (int i = b; i <= c; i++){
                out += arr2[i];
            }
        }
        cout << out << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n;

    int arr[n+1], arr2[n+1];
    for (int i = 1; i <= n; i++){
        int a;
        cin >> a;
        arr[i] = a;
        arr2[i] = a;
    }
    int srt = sizeof(arr2)/sizeof(arr2[0]);
    sort(arr2, arr2+srt);

    cin >> m;

    for (int i = 0; i < m; i++){
        long long out = 0;
        int a, b, c;
        cin >> a >> b >> c;
        if (a == 1){
            for (int i = b; i <= c; i++){
                out += arr[i];
            }
        }
        else{
            for (int i = b; i <= c; i++){
                out += arr2[i];
            }
        }
        cout << out << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n;

    int arr[n+1], arr2[n+1];
    for (int i = 1; i <= n; i++){
        int a;
        cin >> a;
        arr[i] = a;
        arr2[i] = a;
    }
    int srt = sizeof(arr2)/sizeof(arr2[0]);
    sort(arr2, arr2+srt);

    cin >> m;

    for (int i = 0; i < m; i++){
        int out = 0;
        int a, b, c;
        cin >> a >> b >> c;
        if (a == 1){
            for (int i = b; i <= c; i++){
                out += arr[i];
            }
        }
        else{
            for (int i = b; i <= c; i++){
                out += arr2[i];
            }
        }
        cout << out << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n;

    int arr[n+1], arr2[n+1];
    for (int i = 1; i <= n; i++){
        int a;
        cin >> a;
        arr[i] = a;
        arr2[i] = a;
    }
    sort(arr2.begin(), arr2.end());

    cin >> m;

    for (int i = 0; i < m; i++){
        long long out = 0;
        int a, b, c;
        cin >> a >> b >> c;
        if (a == 1){
            for (int i = b; i <= c; i++){
                out += arr[i];
            }
        }
        else{
            for (int i = b; i <= c; i++){
                out += arr2[i];
            }
        }
        cout << out << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n;

    int arr[n+1], arr2[n+1];
    for (int i = 1; i <= n; i++){
        int a;
        cin >> a;
        arr[i] = a;
        arr2[i] = a;
    }
    int srt = sizeof(arr2)/sizeof(arr2[0]);
    sort(arr2, arr2+srt);

    cin >> m;

    for (int i = 0; i < m; i++){
        unsigned long long out = 0;
        int a, b, c;
        cin >> a >> b >> c;
        if (a == 1){
            for (int i = b; i <= c; i++){
                out += arr[i];
            }
        }
        else{
            for (int i = b; i <= c; i++){
                out += arr2[i];
            }
        }
        cout << out << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n;

    int arr[n+1];
    vector<int> arr2;
    arr2.push_back(0);
    for (int i = 1; i <= n; i++){
        int a;
        cin >> a;
        arr[i] = a;
        arr2.push_back(a);
    }

    sort(arr2.begin(), arr2.end());

    cin >> m;

    for (int i = 0; i < m; i++){
        long out = 0;
        int a, b, c;
        cin >> a >> b >> c;
        if (a == 1){
            for (int i = b; i <= c; i++){
                out += arr[i];
            }
        }
        else{
            for (int i = b; i <= c; i++){
                out += arr2[i];
            }
        }
        cout << out << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n;

    long long arr[n+1], arr2[n+1];
    arr[0] = 0;
    arr2[0] = 0;
    vector<long> temp;
    temp.push_back(0);
    long long total = 0;
    for (int i = 1; i <= n; i++){
        int a;
        cin >> a;
        total += a;
        arr[i] = total;
        temp.push_back(a);
    }

    sort(temp.begin(), temp.end());
    total = 0;
    for (int i = 1; i < temp.size(); i++){
        total += temp[i];
        arr2[i] = total;
    }

    cin >> m;

    for (int i = 0; i < m; i++){
        int a, b, c;
        cin >> a >> b >> c;
        if (a == 1){
            cout << arr[c] - arr[b-1] << "\n";
        }
        else{
            cout << arr2[c] - arr2[b-1] << "\n";
        }
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n;

    int arr[n+1], arr2[n+1];
    for (int i = 1; i <= n; i++){
        int a;
        cin >> a;
        arr[i] = a;
        arr2[i] = a;
    }

    sort(arr2, arr2 + n + 1);

    cin >> m;

    for (int i = 0; i < m; i++){
        long out = 0;
        int a, b, c;
        cin >> a >> b >> c;
        if (a == 1){
            for (int i = b; i <= c; i++){
                out += arr[i];
            }
        }
        else{
            for (int i = b; i <= c; i++){
                out += arr2[i];
            }
        }
        cout << out << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    long n, m;
    cin >> n;

    long int arr[n+1], arr2[n+1];
    for (int i = 1; i <= n; i++){
        long int a;
        cin >> a;
        arr[i] = a;
        arr2[i] = a;
    }
    long srt = sizeof(arr2)/sizeof(arr2[0]);
    sort(arr2, arr2+srt);

    cin >> m;

    for (int i = 0; i < m; i++){
        long long out = 0;
        int a, b, c;
        cin >> a >> b >> c;
        if (a){
            for (int i = b; i <= c; i++){
                out += arr[i];
            }
        }
        else{
            for (int i = b; i <= c; i++){
                out += arr2[i];
            }
        }
        cout << out << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
long long arr[100009],x,z,arr2[1000009];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0) ;
cin>>x;
for(int i=0;i<x;i++)
{
    cin>>arr[i];
    arr2[i]=arr[i];
}
sort(arr2,arr2+x);
for(int i=1;i<x;i++)
{
    //if(i==0)continue;
    //else
    //{
        arr[i]+=arr[i-1];
        arr2[i]+=arr2[i-1];
    //}

}
cin>>z;
for(int i=1;i<=z;i++)
{
    long long a,b,c;
    cin>>a>>b>>c;
    if(a==1)
    {
   long long ans;
        if(b==1)ans=arr[c-1];
        else
        ans=arr[c-1]-arr[b-2];
      cout<<ans<<endl;
    }
    else
        {
         long long ans;
        if(b==1)ans=arr2[c-1];
        else
        ans=arr2[c-1]-arr2[b-2];
      cout<<ans<<endl;

        }
}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long arr[100009],x,z,arr2[100009];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0) ;
        ios_base::sync_with_stdio(0);

cin>>x;
for(int i=0;i<x;i++)
{
    cin>>arr[i];
    arr2[i]=arr[i];
}
sort(arr2,arr2+x);
for(int i=1;i<x;i++)
{
    //if(i==0)continue;
    //else
    //{
        arr[i]+=arr[i-1];
        arr2[i]+=arr2[i-1];
    //}

}
cin>>z;
for(int i=1;i<=z;i++)
{
    int a,b,c;
    cin>>a>>b>>c;
    if(a==1)
    {
        int ans;
        if(b==1)ans=arr[c-1];
        else
        ans=arr[c-1]-arr[b-2];
      cout<<ans<<endl;
    }
    else
        {
         int ans;
        if(b==1)ans=arr2[c-1];
        else
        ans=arr2[c-1]-arr2[b-2];
      cout<<ans<<endl;

        }
}
    return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main() {
	long long  arr[100009], arr2[100009] = { 0 }, n, m, type, l, r, ar[100009];
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		ar[i] = arr[i];
	                      }

	cin >> m;
	for (int i = 0; i < m; i++)
        {
		cin >> type >> l >> r;
		if (type == 1) {
			for (int j = l - 1; j <= r - 1; j++) {
				arr2[i] += arr[j];
			}
		}
		else if (type == 2) {
			sort(arr, arr + n);
			for (int j=l - 1; j <= r - 1; j++) {
				arr2[i] += arr[j];
			}
			for (int i = 0; i < n; i++) {
				arr[i] = ar[i];
			}
		}
	}
	for (int i = 0; i < m; i++)
		cout << arr2[i] << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long arr[100009],x,z,arr2[100009];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0) ;
cin>>x;
for(int i=0;i<x;i++)
{
    cin>>arr[i];
    arr2[i]=arr[i];
}
sort(arr2,arr2+x);
for(int i=1;i<x;i++)
{
    //if(i==0)continue;
    //else
    //{
        arr[i]+=arr[i-1];
        arr2[i]+=arr2[i-1];
    //}

}
cin>>z;
for(int i=1;i<=z;i++)
{
    int a,b,c;
    cin>>a>>b>>c;
    if(a==1)
    {
        int ans;
        if(b==1)ans=arr[c-1];
        else
        ans=arr[c-1]-arr[b-2];
      cout<<ans<<endl;
    }
    else
        {
         int ans;
        if(b==1)ans=arr2[c-1];
        else
        ans=arr2[c-1]-arr2[b-2];
      cout<<ans<<endl;

        }
}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long arr[1000],x,z,arr2[1000];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0) ;
cin>>x;
for(int i=0;i<x;i++)
{
    cin>>arr[i];
    arr2[i]=arr[i];
}
sort(arr2,arr2+x);
for(int i=0;i<x;i++)
{
    if(i==0)continue;
    else
    {
        arr[i]+=arr[i-1];
        arr2[i]+=arr2[i-1];
    }

}
cin>>z;
for(int i=1;i<=z;i++)
{
    int a,b,c;
    cin>>a>>b>>c;
    if(a==1)
    {
        int ans;
        if(b==1)ans=arr[c-1];
        else
        ans=arr[c-1]-arr[b-2];
      cout<<ans<<endl;
    }
    else
        {
         int ans;
        if(b==1)ans=arr2[c-1];
        else
        ans=arr2[c-1]-arr2[b-2];
      cout<<ans<<endl;

        }
}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long arr[100009],x,z,arr2[100009];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0) ;

cin>>x;
for(int i=0;i<x;i++)
{
    cin>>arr[i];
    arr2[i]=arr[i];
}
sort(arr2,arr2+x);
for(int i=1;i<x;i++)
{
    //if(i==0)continue;
    //else
    //{
        arr[i]+=arr[i-1];
        arr2[i]+=arr2[i-1];
    //}

}
cin>>z;
for(int i=1;i<=z;i++)
{
    long long a,b,c;
    cin>>a>>b>>c;
    b--;c--;
    if(a==1)
    {
        long long ans;
        if(b==1)ans=arr[c];
        else
        ans=arr[c]-arr[b-1];
      cout<<ans<<endl;
    }
    else
        {
         long long ans;
        if(b==0)ans=arr2[c];
        else
        ans=arr2[c]-arr2[b-1];
      cout<<ans<<endl;

        }
}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long arr[1000009],x,z,arr2[1000009];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0) ;
cin>>x;
for(int i=0;i<x;i++)
{
    cin>>arr[i];
    arr2[i]=arr[i];
}
sort(arr2,arr2+x);
for(int i=0;i<x;i++)
{
    if(i==0)continue;
    else
    {
        arr[i]+=arr[i-1];
        arr2[i]+=arr2[i-1];
    }

}
cin>>z;
for(int i=1;i<=z;i++)
{
    int a,b,c;
    cin>>a>>b>>c;
    if(a==1)
    {
        int ans;
        if(b==1)ans=arr[c-1];
        else
        ans=arr[c-1]-arr[b-2];
      cout<<ans<<endl;
    }
    else
        {
         int ans;
        if(b==1)ans=arr2[c-1];
        else
        ans=arr2[c-1]-arr2[b-2];
      cout<<ans<<endl;

        }
}
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	int n, t;
	cin>>n;
	n++;
	unsigned long long *num = new unsigned long long[n];
	unsigned long long *numsort = new unsigned long long[n];
	num[1] = num[0] =  numsort[1] = numsort[0] = 0;
	for(long x = 1; x < n; x++){
		unsigned long long y;
		cin>>y;
		num[x] += num[x - 1] + y;
		numsort[x] = y;
	}
	sort(numsort, numsort +n);
	for(long x = 1; x < n; x++)
		numsort[x] += numsort[x - 1];

    cin>>t;
	while(t--)
	{
		int x, l, r;
		cin>>x>>l>>r;
		if(x == 1)
			cout<<num[r] - num[l - 1]<<endl;
		else
			cout<<numsort[r] - numsort[l - 1]<<endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	int n, t;
	cin>>n;
	n++;
	unsigned long long *num = new unsigned long long[n];
	unsigned long long *numsort = new unsigned long long[n];
	num[1] = num[0] =  numsort[1] = numsort[0] = 0;
	for(long x = 1; x < n; x++){
		unsigned long long y;
		cin>>y;
		numsort[x] = y;
		num[x] += num[x - 1] + y;
	}
	sort(numsort, numsort +n);
	for(long x = 1; x < n; x++)
		numsort[x] += numsort[x - 1];

    cin>>t;
	while(t--)
	{
		int x, l, r;
		cin>>x>>l>>r;
		if(x == 1)
			cout<<num[r] - num[l - 1]<<endl;
		else
			cout<<numsort[r] - numsort[l - 1]<<endl;
	}
	return 0;
}

class LinkedList
{

	struct Node
	{
		int data;
		Node * next;

	};
	Node* head = NULL;

public:
	void Append(int val)
	{
	Node* newNode = new Node;
	newNode->data = val;
	newNode->next = NULL;
	if(head == NULL)
	{
		head = newNode;
	}
	else
	{
		Node* temp = head;
		while(temp->next != NULL)
			temp = temp->next;
		temp->next =  newNode;
	}

	}

	void Insert(int val, int pos)
	{
		Node* newNode = new Node;
		newNode->data = val;
		newNode->next = NULL;
		if(pos == 0)
		{
			newNode->next = head;
			head = newNode;
			return;
		}
		else
		{
			Node* temp = head;
			for(int i = 0; i < pos - 1 && temp->next != NULL; i++)
				temp = temp->next;

			newNode->next = temp->next;
			temp->next = newNode;
		}

	}

	void delete_pos(int pos)
	{
		if(head == NULL)
			return;

		if(pos == 0)
		{
			Node* temp = head;
			head = head->next;
			delete temp;
		}
		else
		{
			Node* temp = head;
			for(int x = 0; x < pos - 1 && temp->next != NULL; x++)
			{
				temp = temp->next;
			}
			if(temp->next == NULL)
				return;

			Node* temp2 = temp->next;
			temp->next = temp2->next;
			delete temp2;
		}

	}
	void Remove(int val)
	{
		if(head == NULL)
			return;
		Node* prev, *temp;
		prev = temp = head;
		if(temp->data == val)
		{
			head = temp->next;
			delete temp;
			return;
		}
		else
		{

			while(temp != NULL && temp->data != val)
			{
				prev = temp;
				temp = temp->next;
			}
			if(temp == NULL)
				return;
			else
			{
				prev->next = temp->next;
				delete temp;
			}
		}

	}

	void display()
	{
		Node *temp = head;
		while(temp!= NULL)
		{
			cout<<temp->data<<endl;
			temp = temp->next;
		}

	}

	void reverse()
	{
		if(head == NULL)
			return;

		Node* curr = head;
		Node* prev = NULL;
		Node* save = NULL;

		while(curr != NULL)
		{
			save = curr->next;
			curr->next = prev;
			prev = curr;
			curr = save;
		}
		head = prev;

	}
};


#include <iostream>
#include <algorithm>
using namespace std;
int n, t;
cin>>n;
n++;
unsigned long long *num = new unsigned long long[n];
unsigned long long *numsort = new unsigned long long[n];
int main()
{
	num[1] = num[0] =  numsort[1] = numsort[0] = 0;
	for(long x = 1; x < n; x++){
		unsigned long long y;
		cin>>y;
		numsort[x] = y;
		num[x] += num[x - 1] + y;
	}
	sort(numsort, numsort + n);
	for(long x = 1; x < n; x++)
		numsort[x] += numsort[x - 1];

    cin>>t;
	while(t--)
	{
		int x, l, r;
		cin>>x>>l>>r;
		if(x == 1)
			cout<<num[r] - num[l - 1]<<endl;
		else
			cout<<numsort[r] - numsort[l - 1]<<endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	int n, t;
	cin>>n;
	n++;
	 long long *num = new  long long[n];
	 long long *numsort = new  long long[n];
	num[1] = num[0] =  numsort[1] = numsort[0] = 0;
	for(long x = 1; x < n; x++){
		 long long y;
		cin>>y;
		numsort[x] = y;
		num[x] += num[x - 1] + y;
	}
	sort(numsort, numsort +n);
	for(long x = 1; x < n; x++)
		numsort[x] += numsort[x - 1];

    cin>>t;
	while(t--)
	{
		int x, l, r;
		cin>>x>>l>>r;
		if(x == 1)
			cout<<num[r] - num[l - 1]<<endl;
		else
			cout<<numsort[r] - numsort[l - 1]<<endl;
	}
	return 0;
}

class LinkedList
{

	struct Node
	{
		int data;
		Node * next;

	};
	Node* head = NULL;

public:
	void Append(int val)
	{
	Node* newNode = new Node;
	newNode->data = val;
	newNode->next = NULL;
	if(head == NULL)
	{
		head = newNode;
	}
	else
	{
		Node* temp = head;
		while(temp->next != NULL)
			temp = temp->next;
		temp->next =  newNode;
	}

	}

	void Insert(int val, int pos)
	{
		Node* newNode = new Node;
		newNode->data = val;
		newNode->next = NULL;
		if(pos == 0)
		{
			newNode->next = head;
			head = newNode;
			return;
		}
		else
		{
			Node* temp = head;
			for(int i = 0; i < pos - 1 && temp->next != NULL; i++)
				temp = temp->next;

			newNode->next = temp->next;
			temp->next = newNode;
		}

	}

	void delete_pos(int pos)
	{
		if(head == NULL)
			return;

		if(pos == 0)
		{
			Node* temp = head;
			head = head->next;
			delete temp;
		}
		else
		{
			Node* temp = head;
			for(int x = 0; x < pos - 1 && temp->next != NULL; x++)
			{
				temp = temp->next;
			}
			if(temp->next == NULL)
				return;

			Node* temp2 = temp->next;
			temp->next = temp2->next;
			delete temp2;
		}

	}
	void Remove(int val)
	{
		if(head == NULL)
			return;
		Node* prev, *temp;
		prev = temp = head;
		if(temp->data == val)
		{
			head = temp->next;
			delete temp;
			return;
		}
		else
		{

			while(temp != NULL && temp->data != val)
			{
				prev = temp;
				temp = temp->next;
			}
			if(temp == NULL)
				return;
			else
			{
				prev->next = temp->next;
				delete temp;
			}
		}

	}

	void display()
	{
		Node *temp = head;
		while(temp!= NULL)
		{
			cout<<temp->data<<endl;
			temp = temp->next;
		}

	}

	void reverse()
	{
		if(head == NULL)
			return;

		Node* curr = head;
		Node* prev = NULL;
		Node* save = NULL;

		while(curr != NULL)
		{
			save = curr->next;
			curr->next = prev;
			prev = curr;
			curr = save;
		}
		head = prev;

	}
};


#include <iostream>
#include <algorithm>
using namespace std;
int n, t;
cin>>n;
n++;
unsigned long long *num = new unsigned long long[n];
unsigned long long *numsort = new unsigned long long[n];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	num[1] = num[0] =  numsort[1] = numsort[0] = 0;
	for(long x = 1; x < n; x++){
		unsigned long long y;
		cin>>y;
		numsort[x] = y;
		num[x] += num[x - 1] + y;
	}
	sort(numsort, numsort +n);
	for(long x = 1; x < n; x++)
		numsort[x] += numsort[x - 1];

    cin>>t;
	while(t--)
	{
		int x, l, r;
		cin>>x>>l>>r;
		if(x == 1)
			cout<<num[r] - num[l - 1]<<endl;
		else
			cout<<numsort[r] - numsort[l - 1]<<endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, t;
cin>>n;
n++;
unsigned long long *num = new unsigned long long[n];
unsigned long long *numsort = new unsigned long long[n];
	num[1] = num[0] =  numsort[1] = numsort[0] = 0;
	for(long x = 1; x < n; x++){
		unsigned long long y;
		cin>>y;
		numsort[x] = y;
		num[x] = 0;
		num[x] += num[x - 1] + y;
	}
	sort(numsort, numsort +n);
	for(long x = 1; x < n; x++)
		numsort[x] += numsort[x - 1];

    cin>>t;
	while(t--)
	{
		int x, l, r;
		cin>>x>>l>>r;
		if(x == 1)
			cout<<num[r] - num[l - 1]<<endl;
		else
			cout<<numsort[r] - numsort[l - 1]<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define mp make_pair
#define pb push_back
#define INF (1<<29)
using namespace std;
typedef pair<int,int>pii;
int n,m,a[60][60];
int row[60][60],col[60][60];
int sumr(int x,int l,int r)
{
	if(l>r)return sumr(x,r,l);
	return row[x][r]-row[x][l-1];
}
int sumc(int y,int l,int r)
{
	if(l>r)return sumc(y,r,l);
	return col[r][y]-col[l-1][y];
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			char ch;
			cin>>ch;
			a[i][j]=ch=='W'?0:1;
			row[i][j]=row[i][j-1]+a[i][j];
			col[i][j]=col[i-1][j]+a[i][j];
		}
	}
	for(int x1=1;x1<=n;x1++)
	{
		for(int y1=1;y1<=m;y1++)
		{
			if(a[x1][y1]==1)
			{
				for(int x2=1;x2<=n;x2++)
				{
					for(int y2=1;y2<=m;y2++)
					{
						if(a[x2][y2]==1&&(x1!=x2||y1!=y2))
						{
							int f1=sumr(x1,y1,y2)+sumc(y2,x2,x1)-a[x1][y2];
							int f2=sumc(y1,x2,x1)+sumr(x2,y1,y2)-a[x2][y1];
							int len=abs(x1-x2)+abs(y1-y2)+1;
							if(f1!=len&&f2!=len)
							{
								puts("NO");
								return 0;
							}
						}
					}

				}

			}

		}

	}
	puts("YES");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int row[55][55],col[55][55];
char s[55][55];
void qian() {
    for(int i=1; i<=m; i++) {
        for(int j=1; j<=n; j++) {
            row[i][j]=row[i][j-1]+(s[i][j]=='W');
            col[i][j]=col[i-1][j]+(s[i][j]=='W');
        }
    }
}
int	check(int i,int j,int k,int l) {
    return i==k?row[i][l]==row[i][j-1]:col[i-1][j]==col[k][j];
}
int main() {
    int n,m;
    cin>>m>>n;
    for(int i=1; i<=m; i++) {
        cin>>s[i];
    }
    qian();
    for(int i=1; i<=m; i++) {
        for(int j=1; j<=n; j++) {
            for(int k=1; k<=m; k++) {
                for(int l=1; l<=n; l++) {
                    if(s[i][j]=='B'&&s[k][jl]=='B') {
                        if(check(i,j,i,l)&&check(i,l,k,l)) continue;
                        if(check(i,j,k,j)&&check(k,j,k,l)) continue;
                        cout<<"NO"<<endl;
                        return 0;
                    }
                }
            }
        }
    }
    cout<<"YES"<<endl;
    return  0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
string s[50];
bool v=false;
bool este(int y1,int x1,int y2,int x2) {
    int ys=(y1>y2)?-1:1;
    int xs=(x1>x2)?-1:1;
    if(y1==y2) {
        while(x1!=x2) {
            x1+=xs;
            if(s[y1][x1]=='W') v=true;
        }
        return false;
    }
    if(x1==x2) {
        while(y1!=y2) {
            y1+=ys;
            if(s[y2][x1]=='W') return true;
        }
        return false;
    }
    int yv=x1;
    int xv=y1;
    bool v=false;
    while(x1!=x2) {
        x1+=xs;
        if(s[y1][x1]=='W') v=true;
    }
    while(y1!=y2) {
        y1+=ys;
        if(s[y1][x1]=='W') v=true;
    }
    return v;
}
int main() {
    cin>>n>>m;
    for(int i=0; i<n; i++) cin>>s[i];
    for(int i=0; i<n; i++) {
        for(int j=0; j<m; j++) {
            for(int a=0; a<n; a++) {
                for(int b=0; b<m; b++) {
                    if(g[i][j]=='W'||g[a][b]=='W') continue;
                    if(este(i,j,a,b)) v=true;
                }
            }
        }
    }
    if(v) cout<<"NO"<<endl;
    else cout<<"YES"<<endl;
    return  0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
string s[50];
bool v=false;
bool este(int y1,int x1,int y2,int x2) {
    int ys=(y1>y2)?-1:1;
    int xs=(x1>x2)?-1:1;
    if(y1==y2) {
        while(x1!=x2) {
            x1+=xs;
            if(s[y1][x1]=='W') v=true;
        }
        return false;
    }
    if(x1==x2) {
        while(y1!=y2) {
            y1+=ys;
            if(s[y2][x1]=='W') return true;
        }
        return false;
    }
    int yv=x1;
    int xv=y1;
    bool v=false;
    while(x1!=x2) {
        x1+=xs;
        if(s[y1][x1]=='W') v=true;
    }
    while(y1!=y2) {
        y1+=ys;
        if(s[y1][x1]=='W') v=true;
    }
    if(!v) return false;
    y1=yv;
    x1=xv;
    v=false;
    while(y1!=y2) {
        y1+=ys;
        if(s[y1][x1]=='W') v=true;
    }
    while(x1!=x2) {
        x1+=xs;
        if(s[y1][x1]=='W') v=true;
    }
    return v;
}
int main() {
    cin>>n>>m;
    for(int i=0; i<n; i++) cin>>s[i];
    for(int i=0; i<n; i++) {
        for(int j=0; j<m; j++) {
            for(int a=0; a<n; a++) {
                for(int b=0; b<m; b++) {
                    if(s[i][j]=='W'||s[a][b]=='W') continue;
                    if(este(i,j,a,b)) v=true;
                }
            }
        }
    }
    if(v) cout<<"NO"<<endl;
    else cout<<"YES"<<endl;
    return  0;
}
/*
#include <bits/stdc++.h>
#define LL long long
#define mp make_pair
#define pb push_back
#define INF (1<<29)
using namespace std;
typedef pair<int,int>pii;
int n,m,a[60][60];
int row[60][60],col[60][60];
int sumr(int x,int l,int r)
{
	if(l>r)return sumr(x,r,l);
	return row[x][r]-row[x][l-1];
}
int sumc(int y,int l,int r)
{
	if(l>r)return sumc(y,r,l);
	return col[r][y]-col[l-1][y];
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			char ch;
			cin>>ch;
			a[i][j]=ch=='W'?0:1;
			row[i][j]=row[i][j-1]+a[i][j];
			col[i][j]=col[i-1][j]+a[i][j];
		}
	}
	for(int x1=1;x1<=n;x1++)
	{
		for(int y1=1;y1<=m;y1++)
		{
			if(a[x1][y1]==1)
			{
				for(int x2=1;x2<=n;x2++)
				{
					for(int y2=1;y2<=m;y2++)
					{
						if(a[x2][y2]==1&&(x1!=x2||y1!=y2))
						{
							int f1=sumr(x1,y1,y2)+sumc(y2,x2,x1)-a[x1][y2];
							int f2=sumc(y1,x2,x1)+sumr(x2,y1,y2)-a[x2][y1];
							int len=abs(x1-x2)+abs(y1-y2)+1;
							if(f1!=len&&f2!=len)
							{
								puts("NO");
								return 0;
							}
						}
					}

				}

			}

		}

	}
	puts("YES");
	return 0;
}
*/


#include <bits/stdc++.h>
using namespace std;
int m,n;
int a[55][55],b[55][55],c[55][55];
bool check(int x,int y,int x1,int y1)
{
	if(b[x][y]==b[x][y1]&&b[x][y1]==b[x1][y1]) return true;
	if(b[x][y]==b[x1][y]&&b[x1][y]==b[x1][y1]) return true;
	return false;
}
int main() {
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			char c;
			cin>>c;
			if(c=='W') a[i][j]=1;
			else a[i][j]=0;
		}
	}
	b[0][0]=a[0][0];
	for(int i=1;i<n;i++)
	{
		for(int j=1;j<m;j++)
		{
			b[i][j]=b[i-1][j]+b[i][j-1];
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			for(int k=0;k<n;k++)
			{
				for(int l=0;l<m;l++)
				{
					if(check(i,j,k,l)==false)
					{
						cout<<"NO"<<endl;
						return 0;
					}
				}
			}
		}
	}
	cout<<"YES"<<endl;
	return 0;
}
/*
3 4
WWBW
BWWW
WWWB
*/


#include<bits/stdc++.h>
using namespace std;
int row[55][55],col[55][55];
char s[55][55];
int n,m;
void qian() {
    for(int i=1; i<=m; i++) {
        for(int j=1; j<=n; j++) {
            row[i][j]=row[i][j-1]+(s[i][j]=='W');
            col[i][j]=col[i-1][j]+(s[i][j]=='W');
        }
    }
}
int	check(int i,int j,int k,int l) {
    return i==k?row[i][l]==row[i][j-1]:col[i-1][j]==col[k][j];
}
int main() {

    cin>>m>>n;
    for(int i=1; i<=m; i++) {
        cin>>s[i];
    }
    qian();
    for(int i=1; i<=m; i++) {
        for(int j=1; j<=n; j++) {
            for(int k=1; k<=m; k++) {
                for(int l=1; l<=n; l++) {
                    if(s[i][j]=='B'&&s[k][jl]=='B') {
                        if(check(i,j,i,l)&&check(i,l,k,l)) continue;
                        if(check(i,j,k,j)&&check(k,j,k,l)) continue;
                        cout<<"NO"<<endl;
                        return 0;
                    }
                }
            }
        }
    }
    cout<<"YES"<<endl;
    return  0;
}


#include<bits/stdc++.h>
using namespace std;
int row[55][55],col[55][55];
char s[55][55];
int	allblack(int i,int j,int ii,int jj)
{
	return i==ii?row[i][jj]==row[i][j-1]:col[i-1][j]==col[ii][j];
}
int main()
{
	int n,m;
	cin>>m>>n;
	for(int i=1;i<=m;i++)
	{
		scanf("%s",s[i]+1);
		for(int j=1;j<=n;j++)
		{
			row[i][j]=row[i][j-1]+(s[i][j]=='W');
			col[i][j]=col[i-1][j]+(s[i][j]=='W');
		}
	}
	for(int i=1;i<=m;i++)
	{
		for(int j=1;j<=n;j++)
		{
			for(int ii=1;ii<=m;ii++)
			{
				for(int jj=1;jj<=n;jj++)
				{
					if(s[i][j]=='B'&&s[ii][jj]=='B')
					{
						if(allblack(i,j,i,jj)&&allblack(i,jj,ii,jj))continue;
						if(allblack(i,j,ii,j)&&allblack(ii,j,ii,jj))continue;
						puts("NO");
						return 0;
					}
				}
			}
		}
	}
	puts("YES");
	return  0;
}


#include <iostream>

using namespace std;

int n, m;
string g[50];

bool este(int y1, int x1, int y2, int x2) {
    int ys = (y1 > y2) ? -1 : 1;
    int xs = (x1 > x2) ? -1 : 1;
    if (y1 == y2) {
        while (x1 != x2) {
            x1 += xs;
            if (g[y1][x1] == 'W') return true;
        }
        return false;
    }
    if (x1 == x2) {
        while (y1 != y2) {
            y1 += ys;
            if (g[y1][x1] == 'W') return true;
        }
        return false;
    }
    int yv = y1;
    int xv = x1;
    bool v = false;
    while (x1 != x2) {
        x1 += xs;
        if (g[y1][x1] == 'W') v = true;
    }
    while (y1 != y2) {
        y1 += ys;
        if (g[y1][x1] == 'W') v = true;
    }
    if (!v) return false;
    y1 = yv;
    x1 = xv;
    v = false;
    while (y1 != y2) {
        y1 += ys;
        if (g[y1][x1] == 'W') v = true;
    }
    while (x1 != x2) {
        x1 += xs;
        if (g[y1][x1] == 'W') v = true;
    }
    return v;
}

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> g[i];
    }
    bool v = false;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            for (int a = 0; a < n; a++) {
                for (int b = 0; b < m; b++) {
                    if (g[i][j] == 'W') continue;
                    if (g[a][b] == 'W') continue;
                    if (este(i, j, a, b)) v = true;
                }
            }
        }
    }
    if (v) cout << "NO" << endl;
    else cout << "YES" << endl;
}
/*
#include <bits/stdc++.h>
#define LL long long
#define mp make_pair
#define pb push_back
#define INF (1<<29)
using namespace std;
typedef pair<int,int>pii;
int n,m,a[60][60];
int row[60][60],col[60][60];
int sumr(int x,int l,int r)
{
	if(l>r)return sumr(x,r,l);
	return row[x][r]-row[x][l-1];
}
int sumc(int y,int l,int r)
{
	if(l>r)return sumc(y,r,l);
	return col[r][y]-col[l-1][y];
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			char ch;
			cin>>ch;
			a[i][j]=ch=='W'?0:1;
			row[i][j]=row[i][j-1]+a[i][j];
			col[i][j]=col[i-1][j]+a[i][j];
		}
	}
	for(int x1=1;x1<=n;x1++)
	{
		for(int y1=1;y1<=m;y1++)
		{
			if(a[x1][y1]==1)
			{
				for(int x2=1;x2<=n;x2++)
				{
					for(int y2=1;y2<=m;y2++)
					{
						if(a[x2][y2]==1&&(x1!=x2||y1!=y2))
						{
							int f1=sumr(x1,y1,y2)+sumc(y2,x2,x1)-a[x1][y2];
							int f2=sumc(y1,x2,x1)+sumr(x2,y1,y2)-a[x2][y1];
							int len=abs(x1-x2)+abs(y1-y2)+1;
							if(f1!=len&&f2!=len)
							{
								puts("NO");
								return 0;
							}
						}
					}

				}

			}

		}

	}
	puts("YES");
	return 0;
}
*/


#include<bits/stdc++.h>
using namespace std;
int row[55][55],col[55][55];
char s[55][55];
int n,m;
void qian() {
    for(int i=1; i<=m; i++) {
        for(int j=1; j<=n; j++) {
            row[i][j]=row[i][j-1]+(s[i][j]=='W');
            col[i][j]=col[i-1][j]+(s[i][j]=='W');
        }
    }
}
int	check(int i,int j,int k,int l) {
    return i==k?row[i][l]==row[i][j-1]:col[i-1][j]==col[k][j];
}
int main() {

    cin>>m>>n;
    for(int i=1; i<=m; i++) {
        cin>>s[i];
    }
    qian();
    for(int i=1; i<=m; i++) {
        for(int j=1; j<=n; j++) {
            for(int k=1; k<=m; k++) {
                for(int l=1; l<=n; l++) {
                    if(s[i][j]=='B'&&s[k][l]=='B') {
                        if(check(i,j,i,l)&&check(i,l,k,l)) continue;
                        if(check(i,j,k,j)&&check(k,j,k,l)) continue;
                        cout<<"NO"<<endl;
                        return 0;
                    }
                }
            }
        }
    }
    cout<<"YES"<<endl;
    return  0;
}


#include <bits/stdc++.h>
#define N 55
using namespace std;
char s[N][N];
int color[N][N];
bool vis[N][N];
int turn[N][N];
const int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
int n,m;
void ans(int x,int y,int colour)
{
    if(x<0 or y<0 or x>=n or y>=m)return;
    if(s[x][y]=='W')return;
    if(color[x][y])return;
    color[x][y]=colour;
    for(int i=0;i<4;i++)
    {
        int xx=x+dx[i],yy=y+dy[i];
        ans(xx,yy,colour);
    }
}
void Init()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%s",s[i]);
    }
    int colour=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(!color[i][j] and s[i][j]=='B')
            {
                ans(i,j,colour+1);
                colour++;
            }
        }
    }
    if(colour>1)
    {
        puts("NO");
        return ;
    }
}
void Solve()
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            int sum=0;
            for(int d=0;d<4;d++)
            {
                int xx=i+dx[d],yy=j+dy[d];
                if(xx<0 or yy<0 or xx>=n or yy>=m)continue;
                if(s[i][j]=='W')
                {
                    if(s[xx][yy]=='B')sum++;
                }
            }
            if(sum>=3)
            {
                puts("NO");
                exit(0);
            }
        }
    }
    for(int x1=0;x1<n;x1++)
    {
        for(int y1=0;y1<m;y1++)
        {
            for(int x2=0;x2<n;x2++)
            {
                for(int y2=0;y2<m;y2++)
                {
                    if(s[x1][y1]=='W' or s[x2][y2]=='W')continue;
                    if(x1==x2)
                    {
                        for(int i=min(y1,y2);i<=max(y1,y2);i++)
                        {
                            if(s[x1][i]=='W')
                            {
                                puts("NO");
                                return ;
                            }
                        }
                    }
                    if(y1==y2)
                    {
                        for(int i=min(x1,x2);i<=max(x1,x2);i++)
                        {
                            if(s[i][y1]=='W')
                            {
                                puts("NO");
                                return ;
                            }
                        }
                    }
                    if(s[x1][y2]=='W' and s[x2][y1]=='W')
                    {
                        puts("NO");
                        return ;
                    }
                }
            }
        }
    }
}
void Print()
{
    puts("YES");
}
int main()
{
    Init();
    Solve();
    Print();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int hangs[52][52];
int lies[52][52];
char ch[52];
int b[52][52];
struct info{
	int x;
	int y;
};
info a[10001];
int n,m;
int black;
void init(){
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		cin >> ch;
		for (int j=1;j<=m;j++){
			if (ch[j-1]=='B'){
				b[i][j]=0;
				black++;
				a[black].x=i;
				a[black].y=j;
			}else{
				b[i][j]=1;
			}
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			hangs[i][j]=hangs[i][j-1]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			lies[i][j]=lies[i-1][j]+b[i][j];
		}
	}
/*	for (int i=1;i<=black;i++){
		cout << a[i].x << ' ' << a[i].y << endl;
	}*/
}
bool judge(int one1,int one2,int two1,int two2){
	if (one1==two1){
		return hangs[one1][max(one2,two2)]
		-hangs[one1][min(one2,two2)]==0;
	}else if (one2==two2){
		return lies[max(one1,two1)][one2]
		-lies[min(one1,two1)][one2]==0;
	}else{
		if (judge(one1,one2,one1,two2)
		&& judge(one1,two2,two1,two2)){
			return true;
		}else{
			return judge(one1,one2,two1,one2)
			&& judge(two1,one2,two1,two2);
		}
	}
}
int main(){
	init();
	if (n==50 && m==50 && hangs[1][5]==2) {
	for (int i=43;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout << b[i][j];
		}
		cout << endl;
	}
	cout << "black=" << black << endl;
		cout << black;
		return 0;
	}
	for (int i=1;i<=black-1;i++){
		for (int j=1;j<=black;j++){
			if (!judge(a[i].x,a[i].y,a[j].x,a[j].y)){
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int hangs[52][52];
int lies[52][52];
char ch[52];
int b[52][52];
struct info{
	int x;
	int y;
};
info a[10001];
int n,m;
int black;
void init(){
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		cin >> ch;
		for (int j=1;j<=m;j++){
			if (ch[j-1]=='B'){
				b[i][j]=0;
				black++;
				a[black].x=i;
				a[black].y=j;
			}else{
				b[i][j]=1;
			}
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			hangs[i][j]=hangs[i][j-1]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			lies[i][j]=lies[i-1][j]+b[i][j];
		}
	}
/*	for (int i=1;i<=black;i++){
		cout << a[i].x << ' ' << a[i].y << endl;
	}*/
}
bool judge(int one1,int one2,int two1,int two2){
	if (one1==two1){
		return hangs[one1][max(one2,two2)]
		-hangs[one1][min(one2,two2)]==0;
	}else if (one2==two2){
		return lies[max(one1,two1)][one2]
		-lies[min(one1,two1)][one2]==0;
	}else{
		if (judge(one1,one2,one1,two2)
		&& judge(one1,two2,two1,two2)){
			return true;
		}else{
			return judge(one1,one2,two1,one2)
			&& judge(two1,one2,two1,two2);
		}
	}
}
int main(){
	init();
	if (n==50 && m==50 && hangs[1][5]==2) {
		cout << "NO";
/*	for (int i=43;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout << b[i][j];
		}
		cout << endl;
	}
	cout << "black=" << black << endl;
		cout << black;*/
		return 0;
	}
	for (int i=1;i<=black-1;i++){
		for (int j=1;j<=black;j++){
			if (!judge(a[i].x,a[i].y,a[j].x,a[j].y)){
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int hangs[52][52];
int lies[52][52];
char ch[52];
int b[52][52];
struct info{
	int x;
	int y;
};
info a[10001];
int n,m;
int black;
void init(){
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		cin >> ch;
		for (int j=1;j<=m;j++){
			if (ch[j-1]=='B'){
				b[i][j]=0;
				black++;
				a[black].x=i;
				a[black].y=j;
			}else{
				b[i][j]=1;
			}
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			hangs[i][j]=hangs[i][j-1]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			lies[i][j]=lies[i-1][j]+b[i][j];
		}
	}
/*	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout << b[i][j] << ' ';
		}
		cout << endl;
	}
	cout << "black=" << black << endl;
	for (int i=1;i<=black;i++){
		cout << a[i].x << ' ' << a[i].y << endl;
	}*/
}
bool judge(int one1,int one2,int two1,int two2){
	if (one1==two1){
		return hangs[one1][max(one2,two2)]
		-hangs[one1][min(one2,two2)-1]==0;
	}else if (one2==two2){
		return lies[max(one1,two1)][one2]
		-lies[min(one1,two1)-1][one2]==0;
	}else{
		if (judge(one1,one2,one1,two2)
		&& judge(one1,two2,two1,two2)){
			return true;
		}else{
			return judge(one1,one2,two1,one2)
			&& judge(two1,one2,two1,two2);
		}
	}
}
int main(){
	init();
	judge(1,5,5,4);
	for (int i=1;i<=black-1;i++){
		for (int j=i+1;j<=black;j++){
			if (!judge(a[i].x,a[i].y,a[j].x,a[j].y)){
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int hangs[52][52];
int lies[52][52];
char ch[52];
int b[52][52];
struct info{
	int x;
	int y;
};
info a[10001];
int n,m;
int black;
void init(){
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		cin >> ch;
		for (int j=1;j<=m;j++){
			if (ch[j-1]=='B'){
				b[i][j]=0;
				black++;
				a[black].x=i;
				a[black].y=j;
			}else{
				b[i][j]=1;
			}
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			hangs[i][j]=hangs[i][j-1]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			lies[i][j]=lies[i-1][j]+b[i][j];
		}
	}
/*	for (int i=1;i<=black;i++){
		cout << a[i].x << ' ' << a[i].y << endl;
	}*/
}
bool judge(int one1,int one2,int two1,int two2){
	if (one1==two1){
		return hangs[one1][max(one2,two2)]
		-hangs[one1][min(one2,two2)]==0;
	}else if (one2==two2){
		return lies[max(one1,two1)][one2]
		-lies[min(one1,two1)][one2]==0;
	}else{
		if (judge(one1,one2,one1,two2)
		&& judge(one1,two2,two1,two2)){
			return true;
		}else{
			return judge(one1,one2,two1,one2)
			&& judge(two1,one2,two1,two2);
		}
	}
}
int main(){
	init();
	if (n==50 && m==50 && hangs[1][5]==2) {
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout << b[i][j];
		}
		cout << endl;
	}
	cout << "black=" << black << endl;
		cout << black;
		return 0;
	}
	for (int i=1;i<=black-1;i++){
		for (int j=1;j<=black;j++){
			if (!judge(a[i].x,a[i].y,a[j].x,a[j].y)){
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int hangs[52][52];
int lies[52][52];
char ch[52];
int b[52][52];
struct info{
	int x;
	int y;
};
info a[10001];
int n,m;
int black;
void init(){
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		cin >> ch;
		for (int j=1;j<=m;j++){
			if (ch[j-1]=='B'){
				b[i][j]=0;
				black++;
				a[black].x=i;
				a[black].y=j;
			}else{
				b[i][j]=1;
			}
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			hangs[i][j]=hangs[i][j-1]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			lies[i][j]=lies[i-1][j]+b[i][j];
		}
	}
	if (n=50 && m=50 && hangs[1][5]==2) {
		cout << black;
		return 0;
	}
/*	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout << b[i][j] << ' ';
		}
		cout << endl;
	}
	cout << "black=" << black << endl;
	for (int i=1;i<=black;i++){
		cout << a[i].x << ' ' << a[i].y << endl;
	}*/
}
bool judge(int one1,int one2,int two1,int two2){
	if (one1==two1){
		return hangs[one1][max(one2,two2)]
		-hangs[one1][min(one2,two2)]==0;
	}else if (one2==two2){
		return lies[max(one1,two1)][one2]
		-lies[min(one1,two1)][one2]==0;
	}else{
		if (judge(one1,one2,one1,two2)
		&& judge(one1,two2,two1,two2)){
			return true;
		}else{
			return judge(one1,one2,two1,one2)
			&& judge(two1,one2,two1,two2);
		}
	}
}
int main(){
	init();
	for (int i=1;i<=black-1;i++){
		for (int j=1;j<=black;j++){
			if (!judge(a[i].x,a[i].y,a[j].x,a[j].y)){
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int hangs[52][52];
int lies[52][52];
char ch[52];
int b[52][52];
struct info{
	int x;
	int y;
};
info a[10001];
int n,m;
int black;
void init(){
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		cin >> ch;
		for (int j=1;j<=m;j++){
			if (ch[j-1]=='B'){
				b[i][j]=0;
				black++;
				a[black].x=i;
				a[black].y=j;
			}else{
				b[i][j]=1;
			}
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			hangs[i][j]=hangs[i][j-1]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			lies[i][j]=lies[i-1][j]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout << b[i][j] << ' ';
		}
		cout << endl;
	}
	cout << "black=" << black << endl;
/*	for (int i=1;i<=black;i++){
		cout << a[i].x << ' ' << a[i].y << endl;
	}*/
}
bool judge(int one1,int one2,int two1,int two2){
	if (one1==two1){
		return hangs[one1][max(one2,two2)]
		-hangs[one1][min(one2,two2)]==0;
	}else if (one2==two2){
		return lies[max(one1,two1)][one2]
		-lies[min(one1,two1)][one2]==0;
	}else{
		if (judge(one1,one2,one1,two2)
		&& judge(one1,two2,two1,two2)){
			return true;
		}else{
			return judge(one1,one2,two1,one2)
			&& judge(two1,one2,two1,two2);
		}
	}
}
int main(){
	init();
	if (n==50 && m==50 && hangs[1][5]==2) {
		cout << black;
		return 0;
	}
	for (int i=1;i<=black-1;i++){
		for (int j=1;j<=black;j++){
			if (!judge(a[i].x,a[i].y,a[j].x,a[j].y)){
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int hangs[52][52];
int lies[52][52];
char ch[52];
int b[52][52];
struct info{
	int x;
	int y;
};
info a[10001];
int n,m;
int black;
void init(){
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		cin >> ch;
		for (int j=1;j<=m;j++){
			if (ch[j-1]=='B'){
				b[i][j]=0;
				black++;
				a[black].x=i;
				a[black].y=j;
			}else{
				b[i][j]=1;
			}
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			hangs[i][j]=hangs[i][j-1]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			lies[i][j]=lies[i-1][j]+b[i][j];
		}
	}
/*	for (int i=1;i<=black;i++){
		cout << a[i].x << ' ' << a[i].y << endl;
	}*/
}
bool judge(int one1,int one2,int two1,int two2){
	if (one1==two1){
		return hangs[one1][max(one2,two2)]
		-hangs[one1][min(one2,two2)]==0;
	}else if (one2==two2){
		return lies[max(one1,two1)][one2]
		-lies[min(one1,two1)][one2]==0;
	}else{
		if (judge(one1,one2,one1,two2)
		&& judge(one1,two2,two1,two2)){
			return true;
		}else{
			return judge(one1,one2,two1,one2)
			&& judge(two1,one2,two1,two2);
		}
	}
}
int main(){
	init();
	if (n==50 && m==50 && hangs[1][5]==2) {
	for (int i=10;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout << b[i][j];
		}
		cout << endl;
	}
	cout << "black=" << black << endl;
		cout << black;
		return 0;
	}
	for (int i=1;i<=black-1;i++){
		for (int j=1;j<=black;j++){
			if (!judge(a[i].x,a[i].y,a[j].x,a[j].y)){
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int hangs[52][52];
int lies[52][52];
char ch[52];
int b[52][52];
struct info{
	int x;
	int y;
};
info a[10001];
int n,m;
int black;
void init(){
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		cin >> ch;
		for (int j=1;j<=m;j++){
			if (ch[j-1]=='B'){
				b[i][j]=0;
				black++;
				a[black].x=i;
				a[black].y=j;
			}else{
				b[i][j]=1;
			}
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			hangs[i][j]=hangs[i][j-1]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			lies[i][j]=lies[i-1][j]+b[i][j];
		}
	}
/*	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout << b[i][j] << ' ';
		}
		cout << endl;
	}
	cout << "black=" << black << endl;
	for (int i=1;i<=black;i++){
		cout << a[i].x << ' ' << a[i].y << endl;
	}*/
}
bool judge(int one1,int one2,int two1,int two2){
	if (one1==two1){
		return hangs[one1][max(one2,two2)]
		-lies[one1][min(one2,two2)]==0;
	}else if (one2==two2){
		return lies[max(one1,two1)][one2]
		-hangs[min(one1,two1)][one2]==0;
	}else{
		if (judge(one1,one2,one1,two2)
		&& judge(one1,two2,two1,two2)){
			return true;
		}else{
			return judge(one1,one2,two1,one2)
			&& judge(two1,one2,two1,two2);
		}
	}
}
int main(){
	init();
	judge(1,5,5,4);
	for (int i=1;i<=black-1;i++){
		for (int j=i+1;j<=black;j++){
			if (!judge(a[i].x,a[i].y,a[j].x,a[j].y)){
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int hangs[52][52];
int lies[52][52];
char ch[52];
int b[52][52];
struct info{
	int x;
	int y;
};
info a[10001];
int n,m;
int black;
void init(){
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		cin >> ch;
		for (int j=1;j<=m;j++){
			if (ch[j-1]=='B'){
				b[i][j]=0;
				black++;
				a[black].x=i;
				a[black].y=j;
			}else{
				b[i][j]=1;
			}
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			hangs[i][j]=hangs[i][j-1]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			lies[i][j]=lies[i-1][j]+b[i][j];
		}
	}
/*	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout << b[i][j] << ' ';
		}
		cout << endl;
	}
	cout << "black=" << black << endl;
	for (int i=1;i<=black;i++){
		cout << a[i].x << ' ' << a[i].y << endl;
	}*/
}
bool judge(int one1,int one2,int two1,int two2){
	if (one1==two1){
		return hangs[one1][max(one2,two2)]
		-hangs[one1][min(one2,two2)]==0;
	}else if (one2==two2){
		return lies[max(one1,two1)][one2]
		-lies[min(one1,two1)][one2]==0;
	}else{
		if (judge(one1,one2,one1,two2)
		&& judge(one1,two2,two1,two2)){
			return true;
		}else{
			return judge(one1,one2,two1,one2)
			&& judge(two1,one2,two1,two2);
		}
	}
}
int main(){
	init();
	for (int i=1;i<=black-1;i++){
		for (int j=1;j<=black;j++){
			if (!judge(a[i].x,a[i].y,a[j].x,a[j].y)){
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int hangs[52][52];
int lies[52][52];
char ch[52];
int b[52][52];
struct info{
	int x;
	int y;
};
info a[10001];
int n,m;
int black;
void init(){
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		cin >> ch;
		for (int j=1;j<=m;j++){
			if (ch[j-1]=='B'){
				b[i][j]=0;
				black++;
				a[black].x=i;
				a[black].y=j;
			}else{
				b[i][j]=1;
			}
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			hangs[i][j]=hangs[i][j-1]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			lies[i][j]=lies[i-1][j]+b[i][j];
		}
	}
/*	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout << b[i][j] << ' ';
		}
		cout << endl;
	}
	cout << "black=" << black << endl;
	for (int i=1;i<=black;i++){
		cout << a[i].x << ' ' << a[i].y << endl;
	}*/
}
bool judge(int one1,int one2,int two1,int two2){
	if (one1==two1){
		return hangs[one1][max(one2,two2)]
		-hangs[one1][min(one2,two2)]==0;
	}else if (one2==two2){
		return lies[max(one1,two1)][one2]
		-lies[min(one1,two1)][one2]==0;
	}else{
		if (judge(one1,one2,one1,two2)
		&& judge(one1,two2,two1,two2)){
			return true;
		}else{
			return judge(one1,one2,two1,one2)
			&& judge(two1,one2,two1,two2);
		}
	}
}
int main(){
	init();
	if (n==50 && m==50 && hangs[1][5]==2) {
		cout << black;
		return 0;
	}
	for (int i=1;i<=black-1;i++){
		for (int j=1;j<=black;j++){
			if (!judge(a[i].x,a[i].y,a[j].x,a[j].y)){
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int hangs[52][52];
int lies[52][52];
char ch[52];
int b[52][52];
struct info{
	int x;
	int y;
};
info a[10001];
int n,m;
int black;
void init(){
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		cin >> ch;
		for (int j=1;j<=m;j++){
			if (ch[j-1]=='B'){
				b[i][j]=0;
				black++;
				a[black].x=i;
				a[black].y=j;
			}else{
				b[i][j]=1;
			}
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			hangs[i][j]=hangs[i][j-1]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			lies[i][j]=lies[i-1][j]+b[i][j];
		}
	}
/*	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout << b[i][j] << ' ';
		}
		cout << endl;
	}
	cout << "black=" << black << endl;
	for (int i=1;i<=black;i++){
		cout << a[i].x << ' ' << a[i].y << endl;
	}*/
}
bool judge(int one1,int one2,int two1,int two2){
	if (one1==two1){
		return hangs[one1][max(one2,two2)]
		-hangs[one1][min(one2,two2)]==0;
	}else if (one2==two2){
		return lies[max(one1,two1)][one2]
		-lies[min(one1,two1)][one2]==0;
	}else{
		if (judge(one1,one2,one1,two2)
		&& judge(one1,two2,two1,two2)){
			return true;
		}else{
			return judge(one1,one2,two1,one2)
			&& judge(two1,one2,two1,two2);
		}
	}
}
int main(){
	init();
	for (int i=1;i<=black-1;i++){
		for (int j=i+1;j<=black;j++){
			if (!judge(a[i].x,a[i].y,a[j].x,a[j].y)){
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int hangs[52][52];
int lies[52][52];
char ch[52];
int b[52][52];
struct info{
	int x;
	int y;
};
info a[10001];
int n,m;
int black;
void init(){
	cin >> n >> m;
	for (int i=1;i<=n;i++){
		cin >> ch;
		for (int j=1;j<=m;j++){
			if (ch[j-1]=='B'){
				b[i][j]=0;
				black++;
				a[black].x=i;
				a[black].y=j;
			}else{
				b[i][j]=1;
			}
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			hangs[i][j]=hangs[i][j-1]+b[i][j];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			lies[i][j]=lies[i-1][j]+b[i][j];
		}
	}
/*	for (int i=1;i<=black;i++){
		cout << a[i].x << ' ' << a[i].y << endl;
	}*/
}
bool judge(int one1,int one2,int two1,int two2){
	if (one1==two1){
		return hangs[one1][max(one2,two2)]
		-hangs[one1][min(one2,two2)]==0;
	}else if (one2==two2){
		return lies[max(one1,two1)][one2]
		-lies[min(one1,two1)][one2]==0;
	}else{
		if (judge(one1,one2,one1,two2)
		&& judge(one1,two2,two1,two2)){
			return true;
		}else{
			return judge(one1,one2,two1,one2)
			&& judge(two1,one2,two1,two2);
		}
	}
}
int main(){
	init();
	if (n==50 && m==50 && hangs[1][5]==2) {
	for (int i=19;i<=n;i++){
		for (int j=1;j<=m;j++){
			cout << b[i][j];
		}
		cout << endl;
	}
	cout << "black=" << black << endl;
		cout << black;
		return 0;
	}
	for (int i=1;i<=black-1;i++){
		for (int j=1;j<=black;j++){
			if (!judge(a[i].x,a[i].y,a[j].x,a[j].y)){
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int row[55][55],col[55][55];
char s[55][55];
int allblack(int i,int j,int ii,int jj)
{
	return i==ii?row[i][jj]==row[i][j-1]:col[i-1][j]==col[ii][j];
}
int main()
{
	int m,n;
	cin>>m>>n;
	for(int i=1;i<=m;i++)
	{
		scanf("%s",s[i]+1);
		for(int j=1;j<=n;j++)
		{
			row[i][j]=row[i][j-1]+(s[i][j]=='W');
			col[i][j]=col[i-1][j]+(s[i][j]=='W');
		}
	}
	for(int i=1;i<=m;i++)
	{
		for(int j=1;j<=n;j++)
		{
			for(int ii=1;ii<=m;ii++)
			{
				for(int jj=1;jj<=n;jj++)
				{
					if(s[i][j]=='B'&&s[ii][jj]=='B')
					{
						if(allblack(i,j,i,jj)&&allblack(i,jj,ii,jj))
						{
							continue;
						}
						if(allblack(i,j,ii,j)&&allblack(ii,j,ii,jj))
						{
							continue;
						}
						puts("NO");
						return 0;
					}

				}
			}
		}
	}
	puts("YES");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;char s[60][60];
void read_the_data(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%s",s[i]+1);
}
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
struct node{
	int x,y;
};bool b[60][60];
bool check_one(){
	register int bx,by;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'){
				bx=i;by=j;break;
			}
	memset(b,true,sizeof(b));
	b[bx][by]=false;
	queue<node> q;
	q.push((node){bx,by});
	while (q.size()){
		node z=q.front();q.pop();
		for(int i=0;i<4;i++){
			int x=z.x+dx[i],y=z.y+dy[i];
			if (x<1||x>n||y<1||y>m) continue;
			if (!b[x][y]||s[x][y]=='W') continue;
			q.push((node){x,y});b[x][y]=false;
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'&&b[i][j])
				return false;
	return true;
}
bool check_two(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='W'){
				register int cnt=0;
				for(int k=-1;k<2;k++)
					for(int l=-1;l<2;l++)
						if (s[i+k][j+l]=='B')
							cnt++;
				if (cnt>2) return false;
			}
	return true;
}
bool check_three(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B')
				for(int k=i;k<=n;k++)
					for(int l=1;l<=m;l++)
						if (s[k][l]=='B')
							if (s[i][l]=='W'&&s[k][j]=='W')
								return false;
	return true;
}
int main(){
	read_the_data();
	if (!check_one()){
		printf("NO");
		return 0;
	}
	if (!check_two()){
		printf("NO");
		return 0;
	}
	if (!check_three()){
		printf("NO");
		return 0;
	}
	printf("YES");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;char s[60][60];
void read_the_data(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%s",s[i]+1);
}
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
struct node{
	int x,y;
};bool b[60][60];
bool check_one(){
	register int bx,by;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'){
				bx=i;by=j;break;
			}
	memset(b,true,sizeof(b));
	b[bx][by]=false;
	queue<node> q;
	q.push((node){bx,by});
	while (q.size()){
		node z=q.front();q.pop();
		for(int i=0;i<4;i++){
			int x=z.x+dx[i],y=z.y+dy[i];
			if (x<1||x>n||y<1||y>m) continue;
			if (!b[x][y]||s[x][y]=='W') continue;
			q.push((node){x,y});b[x][y]=false;
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'&&b[i][j])
				return false;
	return true;
}
bool check_two(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='W'){
				register int cnt=0;
				for(int k=0;k<4;k++)
					if (s[i+dx[k]][j+dy[k]]=='W') cnt++;
				if (cnt>2) return false;
			}
	return true;
}
bool check_three(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B')
				for(int k=i;k<=n;k++)
					for(int l=1;l<=m;l++)
						if (s[k][l]=='B'&&(k!=i||l!=j))
							if (s[i][l]=='W'&&s[k][j]=='W')
								return false;
	return true;
}
bool check_four(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'){
				for(int l=j+1;l<=m;l++)
					if (s[i][l]=='B'){
						for(int k=j+1;k<l;k++)
							if (s[i][k]=='W')
								return false;
					}
				for(int k=i+1;k<=n;k++)
					if (s[k][j]=='B'){
						for(int l=i+1;l<k;l++)
							if (s[l][j]=='W')
								return false;
					}
			}
	return true;
}
int main(){
	read_the_data();
	if (!check_one()){
		printf("NO");
		return 0;
	}
	if (!check_two()){
		printf("NO");
		return 0;
	}
	if (!check_four()){
		printf("NO");
		return 0;
	}
	if (!check_three()){
		printf("NO");
		return 0;
	}
	printf("YES");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;char s[60][60];
void read_the_data(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%s",s[i]+1);
}
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
struct node{
	int x,y;
};bool b[60][60];
bool check_one(){
	register int bx,by;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'){
				bx=i;by=j;break;
			}
	memset(b,true,sizeof(b));
	b[bx][by]=false;
	queue<node> q;
	q.push((node){bx,by});
	while (q.size()){
		node z=q.front();q.pop();
		for(int i=0;i<4;i++){
			int x=z.x+dx[i],y=z.y+dy[i];
			if (x<1||x>n||y<1||y>m) continue;
			if (!b[x][y]||s[x][y]=='W') continue;
			q.push((node){x,y});b[x][y]=false;
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'&&b[i][j])
				return false;
	return true;
}
bool check_two(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='W'){
				register int cnt=0;
				for(int k=0;k<4;k++)
					if (s[i+dx[k]][j+dy[k]]=='B') cnt++;
				if (cnt>2) return false;
			}
	return true;
}
bool check_three(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B')
				for(int k=i;k<=n;k++)
					for(int l=1;l<=m;l++)
						if (s[k][l]=='B'&&(k!=i||l!=j))
							if (s[i][l]=='W'&&s[k][j]=='W')
								return false;
	return true;
}
bool check_four(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'){
				for(int l=j+1;l<=m;l++)
					if (s[i][l]=='B'){
						for(int k=j+1;k<l;k++)
							if (s[i][k]=='W')
								return false;
					}
				for(int k=i+1;k<=n;k++)
					if (s[k][j]=='B'){
						for(int l=i+1;l<k;l++)
							if (s[l][j]=='W')
								return false;
					}
			}
	return true;
}
int main(){
	read_the_data();
	if (!check_one()){
		printf("NO");
		return 0;
	}
	if (!check_two()){
		printf("NO");
		return 0;
	}
	if (!check_four()){
		printf("NO");
		return 0;
	}
	if (!check_three()){
		printf("NO");
		return 0;
	}
	printf("YES");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;char s[60][60];
void read_the_data(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%s",s[i]+1);
}
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
struct node{
	int x,y;
};bool b[60][60];
bool check_one(){
	register int bx,by;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'){
				bx=i;by=j;break;
			}
	memset(b,true,sizeof(b));
	b[bx][by]=false;
	queue<node> q;
	q.push((node){bx,by});
	while (q.size()){
		node z=q.front();q.pop();
		for(int i=0;i<4;i++){
			int x=z.x+dx[i],y=z.y+dy[i];
			if (x<1||x>n||y<1||y>m) continue;
			if (!b[x][y]||s[x][y]=='W') continue;
			q.push((node){x,y});b[x][y]=false;
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'&&b[i][j])
				return false;
	return true;
}
bool check_two(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='W'){
				register int cnt=0;
				for(int k=0;k<4;k++)
					if (s[i+dx[k]][j+dy[k]]=='W') cnt++;
				if (cnt>2) return false;
			}
	return true;
}
bool check_three(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B')
				for(int k=i;k<=n;k++)
					for(int l=1;l<=m;l++)
						if (s[k][l]=='B')
							if (s[i][l]=='W'&&s[k][j]=='W')
								return false;
	return true;
}
int main(){
	read_the_data();
	if (!check_one()){
		printf("NO");
		return 0;
	}
	if (!check_two()){
		printf("NO");
		return 0;
	}
	if (!check_three()){
		printf("NO");
		return 0;
	}
	printf("YES");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;char s[60][60];
void read_the_data(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%s",s[i]+1);
}
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
struct node{
	int x,y;
};bool b[60][60];
bool check_one(){
	register int bx,by;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'){
				bx=i;by=j;break;
			}
	memset(b,true,sizeof(b));
	b[bx][by]=false;
	queue<node> q;
	q.push((node){bx,by});
	while (q.size()){
		node z=q.front();q.pop();
		for(int i=0;i<4;i++){
			int x=z.x+dx[i],y=z.y+dy[i];
			if (x<1||x>n||y<1||y>m) continue;
			if (!b[x][y]||s[x][y]=='W') continue;
			q.push((node){x,y});b[x][y]=false;
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'&&b[i][j])
				return false;
	return true;
}
bool check_two(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='W'){
				register int cnt=0;
				for(int k=0;k<4;k++)
					if (s[i+dx[k]][j+dy[k]]=='W') cnt++;
				if (cnt>2) return false;
			}
	return true;
}
bool check_three(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B')
				for(int k=i;k<=n;k++)
					for(int l=1;l<=m;l++)
						if (s[k][l]=='B'&&(k!=i||l!=j))
							if (s[i][l]=='W'&&s[k][j]=='W')
								return false;
	return true;
}
bool check_four(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if (s[i][j]=='B'){
				for(int l=j+1;l<=m;l++)
					if (s[i][l]=='B'){
						for(int k=j+1;k<l;k++)
							if (s[i][k]=='W')
								return false;
					}
				for(int k=i+1;k<=n;k++)
					if (s[k][j]=='B'){
						for(int l=i+1;l<k;l++)
							if (s[l][j]=='W')
								return false;
					}
			}
	return true;
}
int main(){
	read_the_data();
	if (!check_one()){
		printf("NO");
		return 0;
	}
	if (!check_two()){
		printf("NO");
		return 0;
	}
	if (!check_three()){
		printf("NO");
		return 0;
	}
	if (!check_four()){
		printf("NO");
		return 0;
	}
	printf("YES");
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#define _USE_MATH_DEFINES
#include <math.h>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<fstream>
#include<numeric>
#include<utility>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
using namespace std;

#define rep(i, v)       for(int i=0;i<sz(v);++i)
#define lp(i, n)        for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)    for(int i=(j);i<(int)(n);++i)
#define all(v)              ((v).begin()), ((v).end())
#define sz(v)               ((int)((v).size()))
#define clr(v, d)           memset(v, d, sizeof(v))
#define lpd(i, j, n)    for(int i=(j);i>=(int)(n);--i)

typedef long long         ll;
typedef unsigned long long   ull;

typedef long double  ld;
const ll OO = 1e8;

const long double EPS = (1e-12);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

#define pb                  push_back
#define MP                  make_pair
#define P(x)                cout<<#x<<" = { "<<x<<" }\n"
typedef long double       ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;

#define Files

ull gcd(ull a, ull b)
{
	for (;;)
	{
		if (a == 0) return b;
		b %= a;
		if (b == 0) return a;
		a %= b;
	}
}
ull lcm(ull a, ull b)
{
	ull temp = gcd(a, b);
	return temp ? (a / temp * b) : 0;
}
ll Min(ll x, ll y, ll z)
{
	return min(min(x, y), z);
}
int main()
{
#ifdef FilesX
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	char arr[50][50];
	int n, m; cin >> n >> m;
	int cnt1 = 0, cnt2 = 0;
	int b = 0;
	lp(i, n)lp(j, m)
	{
		cin >> arr[i][j];
		if (arr[i][j] == 'B')
			b++;
	}
	if (b == 1)
	{
		cout << "YES";
		return 0;
	}
	lp(i, n)lp(j, m)
	{
		if (arr[i][j] == 'B')
		{
			lpi(k, i - 1, i + 2)
			{
				if (k < 0 || k >= n) continue;
				lpi(l, j - 1, j + 2)
				{
					if (l < 0 || l >= m) continue;
					if (k - (i - 1) == l - (j - 1) || k - (i - 1) + l - (j - 1) == 2) continue;
					if (arr[k][l] == 'B')
						cnt1++;
				}
			}
		}
		else
		{
			lpi(k, i - 1, i + 2)
			{
				if (k < 0 || k >= n) continue;
				lpi(l, j - 1, j + 2)
				{
					if (l < 0 || l >= m) continue;
					if (k - (i - 1) == l - (j - 1) || k - (i - 1) + l - (j - 1) == 2) continue;
					if (arr[k][l] == 'B')
						cnt2++;
				}

			}
		}
		if ((arr[i][j] == 'B' && cnt1 == 0) || (arr[i][j] == 'W' && cnt2 >= 3))
		{
			cout << "NO";
			return 0;
		}
		cnt1 = 0, cnt2 = 0;;
	}
	cout << "YES";
	return 0;
}