#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
char a[51][51];

bool v[51][51][51][51] ;

int n , m , x1, y1 ;

bool check(int i , int j , ll k , ll t , ll x, ll y , vector<vector<bool> > &vis)
{
    if(x >= 1 && x <= n  && y >= 1 && y <= m && a[x][y] == 'B' && !vis[x][y])
        return true ;
    else
        return false ;
}
#define pi pair<pair<int , char>, pair<int , int> >

int main()
{

    cin>>n>>m ;

    for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            cin>>a[i][j] ;
        }
    }

    for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            if(a[i][j] == 'B')
            {
                for(ll k = 1 ; k <= n ; k++)
                {
                    for(ll t = 1; t <= m ; t++)
                    {

                        if(a[k][t] == 'B' && !(i== k && j == t))
                        {
                        vector<vector<bool> > vis(n + 1, vector<bool>(m + 1, false)) ;
                        priority_queue<pi , vector<pi> , greater<pi> > q;
                        ll x = i, y = j;
                        vis[x][y] = true ;
                        if(check(i , j , k , t , x - 1 , y , vis))
                        q.push({{0 , 'u'}, {x - 1 , y}}) ;
                        if(check(i , j , k , t , x + 1 , y , vis))
                        q.push({{0 , 'd'}, {x + 1 , y}}) ;
                        if(check(i , j , k , t , x , y  - 1 , vis))
                        q.push({{0 , 'l'}, {x  , y - 1}}) ;
                        if(check(i , j , k , t , x, y + 1 , vis))
                        q.push({{0 , 'r'}, {x  , y + 1}}) ;

                        while(!q.empty())
                        {

                            pi p = q.top() ;
                            q.pop() ;
                            ll x = p.second.first ;
                            ll y = p.second.second ;

                            char c = p.first.second ;
                            ll dir = p.first.first ;

                            if(vis[x][y]){

                              continue ;

                            }

                            vis[x][y] = true ;

                            if(dir <= 1 && x == k && y == t)
                            {
                                v[i][j][k][t] = true ;
                                v[k][t][i][j] = true ;

                                break ;
                            }

                       if(check(i , j , k , t , x - 1 , y , vis)){
                        if(c == 'u')
                        q.push({{dir, 'u'}, {x - 1 , y}}) ;
                        else
                        {
                            q.push({{dir + 1 , 'u'}, {x - 1 , y}}) ;
                        }
                        }

                        if(check(i , j , k , t , x + 1 , y , vis)){
                         if(c == 'd' )
                        q.push({{dir , 'd'}, {x + 1 , y}}) ;
                        else
                        q.push({{dir + 1, 'd'}, {x + 1 , y}}) ;

                        }
                        if(check(i , j , k , t , x , y  - 1, vis))
                        {
                            if(c == 'l')
                              q.push({{dir , 'l'}, {x  , y - 1}}) ;
                            else
                               q.push({{dir , 'l'}, {x  , y - 1}}) ;
                        }

                        if(check(i , j , k , t , x, y + 1, vis))
                        {
                            if(c == 'r')
                                q.push({{dir , 'r'}, {x  , y + 1}}) ;
                            else
                                q.push({{dir + 1 , 'r'}, {x  , y + 1}}) ;
                        }

                        }

                        }

                    }
                }
            }
        }
    }
       for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            if(a[i][j] == 'B')
            {
                for(ll k = 1 ; k <= n ; k++)
                {
                    for(ll t = 1; t <= m ; t++)
                    {
                        if(a[k][t] == 'B' && !(i== k && j== t))
                        {

                            if(!v[i][j][k][t])
                            {
                                //cout<<i<<" "<<j<<" "<<k<<" "<<t<<endl ;
                                cout<<"NO" ;
                                return 0 ;
                            }

                        }

                        }

                    }
                }
            }
        }

        cout<<"YES" ;
    }


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
char a[51][51];
bool vis[51][51][51][51] ;
bool v[51][51][51][51] ;

int n , m , x1, y1 ;

bool check(int x , int y , ll k , ll t , ll i, ll j)
{
    if(i >= 1 && i <= n  && j >= 1 && j <= m && a[i][j] == 'B' && !vis[k][t][i][j])
        return true ;
    else
        return false ;
}
#define pi pair<pair<int , char>, pair<int , int> >

int main()
{

    cin>>n>>m ;

    for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            cin>>a[i][j] ;
        }
    }

    for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            if(a[i][j] == 'B')
            {
                for(ll k = 1 ; k <= n ; k++)
                {
                    for(ll t = 1; t <= m ; t++)
                    {
                        if(a[k][t] == 'B' && i!= k && j!= t)
                        {
                        priority_queue<pi , vector<pi> , greater<pi> > q;
                        ll x = i, y = j;
                        if(check(i , j , k , t , x - 1 , y ))
                        q.push({{0 , 'u'}, {x - 1 , y}}) ;
                        if(check(i , j , k , t , x + 1 , y ))
                        q.push({{0 , 'd'}, {x + 1 , y}}) ;
                        if(check(i , j , k , t , x , y  - 1))
                        q.push({{0 , 'l'}, {x  , y - 1}}) ;
                        if(check(i , j , k , t , x, y + 1))
                        q.push({{0 , 'r'}, {x  , y + 1}}) ;

                        while(!q.empty())
                        {
                            pi p = q.top() ;
                            q.pop() ;
                            ll x = p.second.first ;
                            ll y = p.second.second ;

                            char c = p.first.second ;
                            ll dir = p.first.first ;

                            if(vis[k][t][x][y])
                              continue ;

                            vis[k][t][x][y] = true ;
                            if(dir <= 1 && x == k && y == t)
                            {
                                v[i][j][k][t] = true ;
                                break ;
                            }

                       if(check(i , j , k , t , x - 1 , y )){
                        if(c == 'u')
                        q.push({{dir, c}, {x - 1 , y}}) ;
                        else
                        {
                            q.push({{dir + 1 , c}, {x - 1 , y}}) ;
                        }
                        }

                        if(check(i , j , k , t , x + 1 , y )){
                         if(c == 'd')
                        q.push({{dir , c}, {x + 1 , y}}) ;
                        else
                        q.push({{dir + 1, c}, {x + 1 , y}}) ;

                        }
                        if(check(i , j , k , t , x , y  - 1))
                        {
                            if(c == 'l')
                              q.push({{dir , c}, {x  , y - 1}}) ;
                            else
                               q.push({{dir , c}, {x  , y - 1}}) ;
                        }

                        if(check(i , j , k , t , x, y + 1))
                        {
                            if(c == 'r')
                                q.push({{dir , c}, {x  , y + 1}}) ;
                            else
                                q.push({{dir + 1 , c}, {x  , y + 1}}) ;
                        }

                        }

                        }

                    }
                }
            }
        }
    }
       for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            if(a[i][j] == 'B')
            {
                for(ll k = 1 ; k <= n ; k++)
                {
                    for(ll t = 1; t <= m ; t++)
                    {
                        if(a[k][t] == 'B' && i!= k && j!= t)
                        {
                              if(!v[i][j][k][t]){
                                cout<<"No" ;
                                return 0 ;
                              }

                        }

                        }

                    }
                }
            }
        }

        cout<<"Yes" ;
    }


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
char a[51][51];
bool vis[51][51][51][51] ;
bool v[51][51][51][51] ;

int n , m , x1, y1 ;

bool check(int x , int y , ll k , ll t , ll i, ll j)
{
    if(i >= 1 && i <= n  && j >= 1 && j <= m && a[i][j] == 'B' && !vis[k][t][i][j])
        return true ;
    else
        return false ;
}
#define pi pair<pair<int , char>, pair<int , int> >

int main()
{

    cin>>n>>m ;

    for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            cin>>a[i][j] ;
        }
    }

    for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            if(a[i][j] == 'B')
            {
                for(ll k = 1 ; k <= n ; k++)
                {
                    for(ll t = 1; t <= m ; t++)
                    {
                        if(a[k][t] == 'B' && i!= k && j!= t)
                        {
                        priority_queue<pi , vector<pi> , greater<pi> > q;
                        ll x = i, y = j;
                        if(check(i , j , k , t , x - 1 , y ))
                        q.push({{0 , 'u'}, {x - 1 , y}}) ;
                        if(check(i , j , k , t , x + 1 , y ))
                        q.push({{0 , 'd'}, {x + 1 , y}}) ;
                        if(check(i , j , k , t , x , y  - 1))
                        q.push({{0 , 'l'}, {x  , y - 1}}) ;
                        if(check(i , j , k , t , x, y + 1))
                        q.push({{0 , 'r'}, {x  , y + 1}}) ;

                        while(!q.empty())
                        {
                            pi p = q.top() ;
                            q.pop() ;
                            ll x = p.second.first ;
                            ll y = p.second.second ;

                            char c = p.first.second ;
                            ll dir = p.first.first ;

                            if(vis[k][t][x][y])
                              continue ;

                            vis[k][t][x][y] = true ;
                            if(dir <= 1 && x == k && y == t)
                            {
                                v[i][j][k][t] = true ;
                                break ;
                            }

                       if(check(i , j , k , t , x - 1 , y )){
                        if(c == 'u')
                        q.push({{dir, c}, {x - 1 , y}}) ;
                        else
                        {
                            q.push({{dir + 1 , c}, {x - 1 , y}}) ;
                        }
                        }

                        if(check(i , j , k , t , x + 1 , y )){
                         if(c == 'd')
                        q.push({{dir , c}, {x + 1 , y}}) ;
                        else
                        q.push({{dir + 1, c}, {x + 1 , y}}) ;

                        }
                        if(check(i , j , k , t , x , y  - 1))
                        {
                            if(c == 'l')
                              q.push({{dir , c}, {x  , y - 1}}) ;
                            else
                               q.push({{dir , c}, {x  , y - 1}}) ;
                        }

                        if(check(i , j , k , t , x, y + 1))
                        {
                            if(c == 'r')
                                q.push({{dir , c}, {x  , y + 1}}) ;
                            else
                                q.push({{dir + 1 , c}, {x  , y + 1}}) ;
                        }

                        }

                        }

                    }
                }
            }
        }
    }
       for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            if(a[i][j] == 'B')
            {
                for(ll k = 1 ; k <= n ; k++)
                {
                    for(ll t = 1; t <= m ; t++)
                    {
                        if(a[k][t] == 'B' && i!= k && j!= t)
                        {
                              if(!v[i][j][k][t]){
                                cout<<"NO" ;
                                return 0 ;
                              }

                        }

                        }

                    }
                }
            }
        }

        cout<<"YES" ;
    }


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

#define mod 1000000007
char a[51][51];

bool v[51][51][51][51] ;

int n , m , x1, y1 ;

bool check(int i , int j , ll k , ll t , ll x, ll y , vector<vector<bool> > &vis)
{
    if(x >= 1 && x <= n  && y >= 1 && y <= m && a[x][y] == 'B' && !vis[x][y])
        return true ;
    else
        return false ;
}
#define pi pair<pair<int , char>, pair<int , int> >

int main()
{

    cin>>n>>m ;

    for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            cin>>a[i][j] ;
        }
    }

    for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            if(a[i][j] == 'B')
            {

                for(ll k = 1 ; k <= n ; k++)
                {
                    for(ll t = 1; t <= m ; t++)
                    {

                        if(a[k][t] == 'B' && !(i== k && j == t) && !v[i][j][k][t] )
                        {

                        map<char , map<pair<int , int > , int> > mp ;
                        vector<vector<bool> > vis(n + 1 , vector<bool>(m + 1, false)) ;
                        priority_queue<pi , vector<pi> , greater<pi> > q;
                        ll x = i, y = j;
                        vis[x][y] = true ;
                        if(check(i , j , k , t , x - 1 , y , vis))
                        q.push({{0 , 'u'}, {x - 1 , y}}) ;
                        if(check(i , j , k , t , x + 1 , y , vis))
                        q.push({{0 , 'd'}, {x + 1 , y}}) ;
                        if(check(i , j , k , t , x , y  - 1 , vis))
                        q.push({{0 , 'l'}, {x  , y - 1}}) ;
                        if(check(i , j , k , t , x, y + 1 , vis))
                        q.push({{0 , 'r'}, {x  , y + 1}}) ;

                        while(!q.empty())
                        {

                            pi p = q.top() ;
                            q.pop() ;
                            ll x = p.second.first ;
                            ll y = p.second.second ;

                            char c = p.first.second ;
                            ll dir = p.first.first ;

                            if(dir >= 2)
                                break ;
                            if(vis[x][y]  ){

                               if(mp[c].find(make_pair(x, y)) != mp[c].end())
                              continue ;

                            }

                            vis[x][y] = true ;
                            mp[c].insert({{x ,y } , 0} ) ;

                            if(dir <= 1){
                                v[i][j][x][y] = true ;
                                v[x][y][i][j] = true ;
                            }
                            if(dir <= 1 && x == k && y == t)
                            {
                                v[i][j][k][t] = true ;
                                v[k][t][i][j] = true ;

                                 break ;

                            }

                       if(check(i , j , k , t , x - 1 , y , vis)){
                        if(c == 'u')
                        q.push({{dir, 'u'}, {x - 1 , y}}) ;
                        else
                        {
                            q.push({{dir + 1 , 'u'}, {x - 1 , y}}) ;
                        }
                        }

                        if(check(i , j , k , t , x + 1 , y , vis)){
                         if(c == 'd' )
                        q.push({{dir , 'd'}, {x + 1 , y}}) ;
                        else
                        q.push({{dir + 1, 'd'}, {x + 1 , y}}) ;

                        }
                        if(check(i , j , k , t , x , y  - 1, vis))
                        {
                            if(c == 'l')
                              q.push({{dir , 'l'}, {x  , y - 1}}) ;
                            else
                               q.push({{dir + 1, 'l'}, {x  , y - 1}}) ;
                        }

                        if(check(i , j , k , t , x, y + 1, vis))
                        {
                            if(c == 'r'){

                                //if(x == 5 && y + 1 == 4)
                                //cout<<x<<" "<<y<<" "<<dir<<" "<<c<<endl ;
                                q.push({{dir , 'r'}, {x  , y + 1}}) ;

                            }
                            else{

                                //if(x == 5 && y + 1 == 4)
                                //cout<<x<<" "<<y<<" "<<dir<<" "<<c<<endl ;
                                q.push({{dir + 1 , 'r'}, {x  , y + 1}}) ;

                            }
                        }

                        }

                        }

                    }
                }
            }
        }
    }
       for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            if(a[i][j] == 'B')
            {
                for(ll k = 1 ; k <= n ; k++)
                {
                    for(ll t = 1; t <= m ; t++)
                    {
                        if(a[k][t] == 'B' && !(i== k && j== t))
                        {

                            if(!v[i][j][k][t])
                            {
                                //cout<<i<<" "<<j<<" "<<k<<" "<<t<<endl ;
                                cout<<"NO" ;
                                return 0 ;
                            }

                        }

                        }

                    }
                }
            }
        }

        cout<<"YES" ;
    }


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

#define mod 1000000007
char a[51][51];

bool v[51][51][51][51] ;

int n , m , x1, y1 ;

bool check(int i , int j , ll k , ll t , ll x, ll y , vector<vector<bool> > &vis)
{
    if(x >= 1 && x <= n  && y >= 1 && y <= m && a[x][y] == 'B' && !vis[x][y])
        return true ;
    else
        return false ;
}
#define pi pair<pair<int , char>, pair<int , int> >

int main()
{

    cin>>n>>m ;

    for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            cin>>a[i][j] ;
        }
    }

    for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            if(a[i][j] == 'B')
            {

                for(ll k = 1 ; k <= n ; k++)
                {
                    for(ll t = 1; t <= m ; t++)
                    {

                        if(a[k][t] == 'B' && !(i== k && j == t))
                        {

                        map<char , vector<pair<int ,int> >> mp ;
                        vector<vector<bool> > vis(n + 1 , vector<bool>(m + 1, false)) ;
                        priority_queue<pi , vector<pi> , greater<pi> > q;
                        ll x = i, y = j;
                        vis[x][y] = true ;
                        if(check(i , j , k , t , x - 1 , y , vis))
                        q.push({{0 , 'u'}, {x - 1 , y}}) ;
                        if(check(i , j , k , t , x + 1 , y , vis))
                        q.push({{0 , 'd'}, {x + 1 , y}}) ;
                        if(check(i , j , k , t , x , y  - 1 , vis))
                        q.push({{0 , 'l'}, {x  , y - 1}}) ;
                        if(check(i , j , k , t , x, y + 1 , vis))
                        q.push({{0 , 'r'}, {x  , y + 1}}) ;

                        while(!q.empty())
                        {

                            pi p = q.top() ;
                            q.pop() ;
                            ll x = p.second.first ;
                            ll y = p.second.second ;

                            char c = p.first.second ;
                            ll dir = p.first.first ;

                            if(vis[x][y]){
                              vector<pair<int ,int> > dummy = mp[c] ;

                              if(find(dummy.begin() , dummy.end() , make_pair(x, y)) != dummy.end())

                              continue ;

                            }

                            vis[x][y] = true ;
                            mp[c].push_back({x ,y }) ;

                            if(dir <= 1 && x == k && y == t)
                            {
                                v[i][j][k][t] = true ;
                                v[k][t][i][j] = true ;
                                break ;

                            }

                       if(check(i , j , k , t , x - 1 , y , vis)){
                        if(c == 'u')
                        q.push({{dir, 'u'}, {x - 1 , y}}) ;
                        else
                        {
                            q.push({{dir + 1 , 'u'}, {x - 1 , y}}) ;
                        }
                        }

                        if(check(i , j , k , t , x + 1 , y , vis)){
                         if(c == 'd' )
                        q.push({{dir , 'd'}, {x + 1 , y}}) ;
                        else
                        q.push({{dir + 1, 'd'}, {x + 1 , y}}) ;

                        }
                        if(check(i , j , k , t , x , y  - 1, vis))
                        {
                            if(c == 'l')
                              q.push({{dir , 'l'}, {x  , y - 1}}) ;
                            else
                               q.push({{dir + 1, 'l'}, {x  , y - 1}}) ;
                        }

                        if(check(i , j , k , t , x, y + 1, vis))
                        {
                            if(c == 'r'){

                                //if(x == 5 && y + 1 == 4)
                                //cout<<x<<" "<<y<<" "<<dir<<" "<<c<<endl ;
                                q.push({{dir , 'r'}, {x  , y + 1}}) ;

                            }
                            else{

                                //if(x == 5 && y + 1 == 4)
                                //cout<<x<<" "<<y<<" "<<dir<<" "<<c<<endl ;
                                q.push({{dir + 1 , 'r'}, {x  , y + 1}}) ;

                            }
                        }

                        }

                        }

                    }
                }
            }
        }
    }
       for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            if(a[i][j] == 'B')
            {
                for(ll k = 1 ; k <= n ; k++)
                {
                    for(ll t = 1; t <= m ; t++)
                    {
                        if(a[k][t] == 'B' && !(i== k && j== t))
                        {

                            if(!v[i][j][k][t])
                            {
                                //cout<<i<<" "<<j<<" "<<k<<" "<<t<<endl ;
                                cout<<"NO" ;
                                return 0 ;
                            }

                        }

                        }

                    }
                }
            }
        }

        cout<<"YES" ;
    }


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

#define mod 1000000007
char a[51][51];

bool v[51][51][51][51] ;

int n , m , x1, y1 ;

bool check(int i , int j , ll k , ll t , ll x, ll y , vector<vector<bool> > &vis)
{
    if(x >= 1 && x <= n  && y >= 1 && y <= m && a[x][y] == 'B' && !vis[x][y])
        return true ;
    else
        return false ;
}
#define pi pair<pair<int , char>, pair<int , int> >

int main()
{

    cin>>n>>m ;

    for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            cin>>a[i][j] ;
        }
    }

    for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            if(a[i][j] == 'B')
            {

                for(ll k = 1 ; k <= n ; k++)
                {
                    for(ll t = 1; t <= m ; t++)
                    {

                        if(a[k][t] == 'B' && !(i== k && j == t)  )
                        {

                        map<char , vector<pair<int ,int> >> mp ;
                        vector<vector<bool> > vis(n + 1 , vector<bool>(m + 1, false)) ;
                        priority_queue<pi , vector<pi> , greater<pi> > q;
                        ll x = i, y = j;
                        vis[x][y] = true ;
                        if(check(i , j , k , t , x - 1 , y , vis))
                        q.push({{0 , 'u'}, {x - 1 , y}}) ;
                        if(check(i , j , k , t , x + 1 , y , vis))
                        q.push({{0 , 'd'}, {x + 1 , y}}) ;
                        if(check(i , j , k , t , x , y  - 1 , vis))
                        q.push({{0 , 'l'}, {x  , y - 1}}) ;
                        if(check(i , j , k , t , x, y + 1 , vis))
                        q.push({{0 , 'r'}, {x  , y + 1}}) ;

                        while(!q.empty())
                        {

                            pi p = q.top() ;
                            q.pop() ;
                            ll x = p.second.first ;
                            ll y = p.second.second ;

                            char c = p.first.second ;
                            ll dir = p.first.first ;

                            if(dir >= 2)
                                continue ;
                            if(vis[x][y]  ){
                              vector<pair<int ,int> > dummy = mp[c] ;

                              if(find(dummy.begin() , dummy.end() , make_pair(x, y)) != dummy.end())

                              continue ;

                            }

                            vis[x][y] = true ;
                            mp[c].push_back({x ,y }) ;

                            if(dir <= 1 && x == k && y == t)
                            {
                                v[i][j][k][t] = true ;
                                v[k][t][i][j] = true ;

                                 break ;

                            }

                       if(check(i , j , k , t , x - 1 , y , vis)){
                        if(c == 'u')
                        q.push({{dir, 'u'}, {x - 1 , y}}) ;
                        else
                        {
                            q.push({{dir + 1 , 'u'}, {x - 1 , y}}) ;
                        }
                        }

                        if(check(i , j , k , t , x + 1 , y , vis)){
                         if(c == 'd' )
                        q.push({{dir , 'd'}, {x + 1 , y}}) ;
                        else
                        q.push({{dir + 1, 'd'}, {x + 1 , y}}) ;

                        }
                        if(check(i , j , k , t , x , y  - 1, vis))
                        {
                            if(c == 'l')
                              q.push({{dir , 'l'}, {x  , y - 1}}) ;
                            else
                               q.push({{dir + 1, 'l'}, {x  , y - 1}}) ;
                        }

                        if(check(i , j , k , t , x, y + 1, vis))
                        {
                            if(c == 'r'){

                                //if(x == 5 && y + 1 == 4)
                                //cout<<x<<" "<<y<<" "<<dir<<" "<<c<<endl ;
                                q.push({{dir , 'r'}, {x  , y + 1}}) ;

                            }
                            else{

                                //if(x == 5 && y + 1 == 4)
                                //cout<<x<<" "<<y<<" "<<dir<<" "<<c<<endl ;
                                q.push({{dir + 1 , 'r'}, {x  , y + 1}}) ;

                            }
                        }

                        }

                        }

                    }
                }
            }
        }
    }
       for(ll i = 1 ; i <= n ; i++)
    {
        for(ll j = 1 ; j <= m ; j++)
        {
            if(a[i][j] == 'B')
            {
                for(ll k = 1 ; k <= n ; k++)
                {
                    for(ll t = 1; t <= m ; t++)
                    {
                        if(a[k][t] == 'B' && !(i== k && j== t))
                        {

                            if(!v[i][j][k][t])
                            {
                                //cout<<i<<" "<<j<<" "<<k<<" "<<t<<endl ;
                                cout<<"NO" ;
                                return 0 ;
                            }

                        }

                        }

                    }
                }
            }
        }

        cout<<"YES" ;
    }


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    clock_t z = clock();

    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    bool valid = true;

    shuffle(allB.begin(), allB.end(), default_random_engine(1238123));
    int clockit = 0;
    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            clockit++;
            set<pii> seen;

            for(pii x: adj(i, j)) {
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) { cout << "YES" << endl; return 0;}
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    seen.emplace(y.fi, y.se);
                    clockit++;
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) { cout << "YES" << endl; return 0;}

                }
            }

                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) { cout << "YES" << endl; return 0;}

            if(seen.size() != cntb) {
                cout << "NO" << endl;
                return 0;
            }
        }

        if(clockit % 3 == 0)
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) { cout << "YES" << endl; return 0;}

    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    clock_t z = clock();

    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    bool valid = true;

    shuffle(allB.begin(), allB.end(), default_random_engine(1238123));
    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            set<pii> seen;

            for(pii x: adj(i, j)) {
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    seen.emplace(y.fi, y.se);
                }
            }

            if(seen.size() != cntb) {
                cout << "NO" << endl;
                return 0;
            }
        }

        if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.9) break;
    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    cerr << "on " << x << "," << y << endl;
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cerr << "pushing " << curx << "," << cury << endl;
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
        }
    }

    bool valid = true;

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(a[i][j] == 'B') {
                clear();

                for(pii x: adj(i, j)) {
                    cerr << x.fi << "," << x.se << endl;
                    for(pii y: adj(x.fi, x.se)) {
                        vis[y.fi][y.se] = true;
                    }
                }

                for(int k = 1; k <= n; k++) {
                    for(int l = 1; l <= m; l++) {
                        if(!vis[k][l] and ok(k, l)) {
                            valid = false;
                        }
                    }
                }
                cerr << endl;

            }
        }
    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') cntb++;
        }
    }

    bool valid = true;

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(a[i][j] == 'B') {
                set<pii> seen;

                for(pii x: adj(i, j)) {
                    for(pii y: adj(x.fi, x.se)) {
                        // vis[y.fi][y.se] = true;
                        seen.emplace(y.fi, y.se);
                    }
                }

                if(seen.size() != cntb) {
                    valid = false;
                }
            }
        }
    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    bool valid = true;

    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            set<pii> seen;

            for(pii x: adj(i, j)) {
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    seen.emplace(y.fi, y.se);
                }
            }

            if(seen.size() != cntb) {
                cout << "NO" << endl;
                return 0;
            }
        }
    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    clock_t z = clock();

    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    bool valid = true;

    shuffle(allB.begin(), allB.end(), default_random_engine(1238123));
    int clockit = 0;
    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            clockit++;
            set<pii> seen;

            for(pii x: adj(i, j)) {
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    seen.emplace(y.fi, y.se);
                    clockit++;
                    if(clockit % 10 == 0) {
                        if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) break;
                    }
                }
            }

            if(seen.size() != cntb) {
                cout << "NO" << endl;
                return 0;
            }
        }

        if(clockit % 3 == 0)
        if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) break;
    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    clock_t z = clock();

    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    if(cntb == n*m) {
        cout << "YES" << endl;
        return 0;
    }
    bool valid = true;

    shuffle(allB.begin(), allB.end(), default_random_engine(1238123));
    int clockit = 0;
    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            clockit++;
            clear();
            set<pii> seen;

            for(pii x: adj(i, j)) {
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    vis[y.fi][y.se] = true;
                }
            }

            for(int i = 1; i<= n; i++) {
                for(int j = 1; j <= m; j++) {
                    if(!vis[i][j] and ok(i, j)) {
                        cout << "NO" << endl;
                        return 0;
                    }
                }
            }
        }

        if(clockit % 3 == 0)
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}

    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/935/problem/C

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define ld long double

ld dist(ld x, ld y, ld a, ld b) {
    return sqrtl(powl(abs(x-a), 2) + powl(abs(b-y), 2));
}

signed main() {
    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);

    ld R, x1, y1, x2, y2;
    cin >> R >> x1 >> y1 >> x2 >> y2;

    ld dis = dist(x1, y1, x2, y2);
    if(dis > R) {
        cout << x1 << " " << y1 << " " << R << endl;
    } else {
        ld r = (dis+R)/2;
        ld x, y;

        if(x1 == x2 and y1 == y2) {
            x = x1+r;
            y = y1;
        } else {
            x = x2 - r*((x2-x1)/dis);
            y = y2 - r*((y2-y1)/dis);
        }

        cout << x << " " << y << " " << r << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    clock_t z = clock();

    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    bool valid = true;

    shuffle(allB.begin(), allB.end(), default_random_engine(1238123));
    int clockit = 0;
    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            clockit++;
            set<pii> seen;

            for(pii x: adj(i, j)) {
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    seen.emplace(y.fi, y.se);
                    clockit++;
                    if(clockit % 3 == 0) {
                        if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) break;
                    }
                }
            }

            if(seen.size() != cntb) {
                cout << "NO" << endl;
                return 0;
            }
        }

        if(clockit % 3 == 0)
        if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) break;
    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    clock_t z = clock();

    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    bool valid = true;

    shuffle(allB.begin(), allB.end(), default_random_engine(1238123));
    int clockit = 0;
    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            clockit++;
            set<pii> seen;

            for(pii x: adj(i, j)) {
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    seen.emplace(y.fi, y.se);
                    clockit++;
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}

                }
            }

                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}

            if(seen.size() != cntb) {
                cout << "NO" << endl;
                return 0;
            }
        }

        if(clockit % 3 == 0)
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}

    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    bool valid = true;

    random_shuffle(allB.begin(), allB.end());
    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            set<pii> seen;

            for(pii x: adj(i, j)) {
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    seen.emplace(y.fi, y.se);
                }
            }

            if(seen.size() != cntb) {
                cout << "NO" << endl;
                return 0;
            }
        }
    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    clock_t z = clock();

    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    bool valid = true;

    shuffle(allB.begin(), allB.end(), default_random_engine(1238123));
    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            set<pii> seen;

            for(pii x: adj(i, j)) {
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    seen.emplace(y.fi, y.se);
                }
            }

            if(seen.size() != cntb) {
                cout << "NO" << endl;
                return 0;
            }
        }

        if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) break;
    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define fi first
#define se second

const int maxn = 105;

char a[maxn][maxn];
bool vis[maxn][maxn];
vector<pii> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            return true;
        }
    }

    return false;
}
signed main() {
    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
        }
    }

    bool valid = true;

    for(int i = 1; i <= n; i++) {
        int cnt = 0;
        for(int j = 1; j <= m; j++) {
            if(a[i][j] == 'B' and a[i][j-1] != 'B') {
                cnt++;
            }
        }

        valid &= (cnt<=1);
    }

    for(int i = 1; i <= m; i++) {
        int cnt = 0;
        for(int j = 1; j <= n; j++) {
            if(a[j][i] == 'B' and a[j-1][i] != 'B') {
                cnt++;
            }
        }

        valid &= (cnt<=1);
    }

    int bfs = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(!vis[i][j] and a[i][j] == 'B') {
                bfs++;
                queue<int> rQ;
                queue<int> cQ;

                rQ.push(i);
                cQ.push(j);
                vis[i][j] = true;
                while(!rQ.empty()) {
                    int r = rQ.front(); rQ.pop();
                    int c = cQ.front(); cQ.pop();

                    for(pii d: dir) {
                        if(ok(r+d.fi, c+d.se) and a[r+d.fi][c+d.se] == 'B') {
                            if(!vis[r+d.fi][c+d.se]) {
                                rQ.push(r+d.fi);
                                cQ.push(c+d.se);
                                vis[r+d.fi][c+d.se] = true;
                            }
                        }
                    }
                }
            }
        }
    }

    valid &= (bfs <= 1);

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    bool valid = true;

    shuffle(allB.begin(), allB.end(), default_random_engine(1238123));
    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            set<pii> seen;

            for(pii x: adj(i, j)) {
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    seen.emplace(y.fi, y.se);
                }
            }

            if(seen.size() != cntb) {
                cout << "NO" << endl;
                return 0;
            }
        }
    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    clock_t z = clock();

    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    for(int i = 1; i <= n-1; i++) {
        for(int j = 1; j <= m-1; j++) {
            int cnt = 0;
            cnt += a[i][j]=='B';
            cnt += a[i][j]=='B';
            cnt += a[i][j]=='B';
            cnt += a[i][j]=='B';

            if(cnt == 3) {
                cout << "NO" << endl;
                return 0;
            }

        }
    }
    if(cntb == n*m) {
        cout << "YES" << endl;
        return 0;
    }
    bool valid = true;

    shuffle(allB.begin(), allB.end(), default_random_engine(1238123));
    int clockit = 0;
    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            clockit++;
            set<pii> seen;

            for(pii x: adj(i, j)) {
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    seen.emplace(y.fi, y.se);
                    clockit++;
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}

                }
            }

                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}

            if(seen.size() != cntb) {
                cout << "NO" << endl;
                return 0;
            }
        }

        if(clockit % 3 == 0)
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}

    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    clock_t z = clock();

    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    for(int i = 1; i <= n-1; i++) {
        for(int j = 1; j <= m-1; j++) {
            int cnt = 0;
            cnt += a[i][j]=='B';
            cnt += a[i][j+1]=='B';
            cnt += a[i+1][j]=='B';
            cnt += a[i+1][j+1]=='B';

            if(cnt == 3) {
                cout << "NO" << endl;
                return 0;
            }

        }
    }
    if(cntb == n*m) {
        cout << "YES" << endl;
        return 0;
    }
    bool valid = true;

    shuffle(allB.begin(), allB.end(), default_random_engine(1238123));
    int clockit = 0;
    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            clockit++;
            set<pii> seen;

            for(pii x: adj(i, j)) {
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    seen.emplace(y.fi, y.se);
                    clockit++;
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}

                }
            }

                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}

            if(seen.size() != cntb) {
                cout << "NO" << endl;
                return 0;
            }
        }

        if(clockit % 3 == 0)
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1) { cout << "YES" << endl; return 0;}

    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') cntb++;
        }
    }

    bool valid = true;

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(a[i][j] == 'B') {
                set<pii> seen;

                for(pii x: adj(i, j)) {
                    for(pii y: adj(x.fi, x.se)) {
                        // vis[y.fi][y.se] = true;
                        seen.emplace(y.fi, y.se);
                    }
                }

                if(seen.size() != cntb) {
                    cout << "NO" << endl;
                    return 0;
                }
            }
        }
    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define pipii pair<int, pii>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;
const int INF = INT_MAX-200;

char a[maxn][maxn];
vector<pair<int, pii>> G[maxn][maxn];
int vis[maxn][maxn];
vector<int> dir = {1, -1, 0};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            vis[i][j] = false;
        }
    }
}

set<pii> adj(int x, int y) {
    set<pii> res;
    int curx, cury;

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury++;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        cury--;
    }

    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx++;
    }
    curx = x;
    cury = y;

    while(ok(curx, cury)) {
        res.emplace(curx, cury);
        curx--;
    }

    return res;
}

signed main() {
    clock_t z = clock();

    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    int cntb = 0;
    vector<pii> allB;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
            if(s[j] == 'B') {
                cntb++;
                allB.emplace_back(i, j+1);
            }
        }
    }

    bool valid = true;

    shuffle(allB.begin(), allB.end(), default_random_engine(1238123));
    int clockit = 0;
    for(pii x: allB) {
        int i = x.fi;
        int j = x.se;
        if(a[i][j] == 'B') {
            clockit++;
            set<pii> seen;

            for(pii x: adj(i, j)) {
                if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) break;
                for(pii y: adj(x.fi, x.se)) {
                    // vis[y.fi][y.se] = true;
                    seen.emplace(y.fi, y.se);
                    clockit++;
                    if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) break;
                }
            }

        if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) break;

            if(seen.size() != cntb) {
                cout << "NO" << endl;
                return 0;
            }
        }

        if(clockit % 3 == 0)
        if(((clock()-z) / (double)CLOCKS_PER_SEC) > 1.6) break;
    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define fi first
#define se second

const int maxn = 105;

char a[maxn][maxn];
bool vis[maxn][maxn];
vector<pii> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            return true;
        }
    }

    return false;
}
signed main() {
    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
        }
    }

    bool valid = true;

    for(int i = 1; i <= n; i++) {
        int cnt = 0;
        for(int j = 1; j <= m; j++) {
            if(a[i][j] == 'B' and a[i][j-1] == 'W') {
                cnt++;
            }
        }

        valid &= (cnt<=1);
    }

    for(int i = 1; i <= m; i++) {
        int cnt = 0;
        for(int j = 1; j <= n; j++) {
            if(a[j][i] == 'B' and a[j-1][i] == 'W') {
                cnt++;
            }
        }

        valid &= (cnt<=1);
    }

    int bfs = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(!vis[i][j] and a[i][j] == 'B') {
                bfs++;
                queue<int> rQ;
                queue<int> cQ;

                rQ.push(i);
                cQ.push(j);
                vis[i][j] = true;
                while(!rQ.empty()) {
                    int r = rQ.front(); rQ.pop();
                    int c = cQ.front(); cQ.pop();

                    for(pii d: dir) {
                        if(ok(r+d.fi, c+d.se) and a[r+d.fi][c+d.se] == 'B') {
                            if(!vis[r+d.fi][c+d.se]) {
                                rQ.push(r+d.fi);
                                cQ.push(c+d.se);
                                vis[r+d.fi][c+d.se] = true;
                            }
                        }
                    }
                }
            }
        }
    }

    valid &= (bfs <= 1);

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


// https://codeforces.com/contest/275/problem/B

#include <bits/stdc++.h>
#define watch(x) cerr << (#x) << " is " << (x) << endl;
using namespace std;
#define endl '\n'
#define pii pair<int, int>
#define cerr if(false) cerr
#define fi first
#define se second

const int maxn = 105;

char a[maxn][maxn];
int dis[maxn][maxn];
vector<pii> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int n, m;
bool ok(int x, int y) {
    if(1 <= x and x <= n) {
        if(1 <= y and y <= m) {
            if(a[x][y] == 'B') {
                return true;
            }
        }
    }

    return false;
}

void clear() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            dis[i][j] = INT_MAX-200;
        }
    }
}

void dfs(int x, int y, vector<int> state) {
    dis[x][y] = state.size();
    cerr << "made it to " << x << "," << y << endl;
    cerr << "state is: " << endl;
    for(int c: state) {
        cerr << c << " ";
    }
    cerr << endl;
    for(int i = 0; i < dir.size(); i++) {
        if(state.size() <= 2 or (state.size() == 3 and state.back() == i)) {
            cerr << "HIIII" << endl;
            watch(i)
            watch(dis[x+dir[i].fi][y+dir[i].se])
            if(ok(x+dir[i].fi, y+dir[i].se) and dis[x+dir[i].fi][y+dir[i].se] > dis[x][y]+1) {
                cerr << "pushing" << endl;
                vector<int> newState;
                for(int x: state) newState.emplace_back(x);

                if(newState.size() and newState.back() != i) {
                    newState.emplace_back(i);
                } else if(newState.empty()) {
                    newState.emplace_back(i);
                }

                dis[x+dir[i].fi][y+dir[i].se] = dis[x][y]+1;
                dfs(x+dir[i].fi, y+dir[i].se, newState);
            }
        }
    }
}
signed main() {
    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            a[i][j] = '?';
        }
    }

    cin >> n >> m;

    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            a[i][j+1] = s[j];
        }
    }

    bool valid = true;

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(a[i][j] == 'B') {
                clear();
                cerr << "start dfs from " << i << "," << j << endl;
                dfs(i, j, vector<int>());

                cerr << "on " << i << "," << j << endl;
                for(int k = 1; k <= n; k++) {
                    for(int l = 1; l <= m; l++) {
                        if(dis[k][l] == INT_MAX-200 and a[k][l] == 'B') {
                            cerr << "couldnt reach " << k << "," << l << endl;
                            valid = false;
                        } else if(a[k][l] == 'B') {
                            cerr << "can reach " << k << "," << l << endl;
                        }
                    }
                }
                cerr << endl;
            }
        }
    }

    if(valid) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}


#include <stdio.h>
#include <string.h>
#define FOR(i,a ,b) for(int i = a; i <= b; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

inline int readInt(){
    int x = 0, f = 1;
    char ch = getchar();
    while( ch < '0' || ch > '9' ) {
        if ( ch == '-' ) f = 0;
        ch = getchar();
    }
    while( ch >= '0' && ch <= '9' ) {
        x = (x << 1) + (x << 3) + (ch - '0');
        ch = getchar();
    };
    if( f ) return x;
    return -x;
}

const int N = 1000;

int n, m, f[N][N];
char s[N][N];
int _dx[4] = {1, -1 , 0,  0 };
int _dy[4] = {0,  0,  1, -1 };
int _di[4] = {1,  2,  3,  4 };

int in(int x,int y){
    return (x >= 1 && x <= n && y >= 1 && y <= m);
}

void input(){
    n = readInt(); m = readInt();
    FOR(i, 1, n)    scanf("%s", s[i] + 1);
}

void dfs(int x,int y,int dir){
    FOR(i, 0, 3){
        int u = x + _dx[i], v = y + _dy[i], di = _di[i], t = 0;
        if( dir == 0 || dir == di ) t = 0;
        else t = 1;
        if( in(u, v) && f[u][v] >= f[x][y] + t && s[u][v] == 'B' ){
            f[u][v] = f[x][y] + t;
            dfs(u, v, di);
        }
    }
}

void process(){
    FOR(i, 1, n){
        FOR(j, 1, m){
            if( s[i][j] == 'B' ){
                FOR(k, 1, n) FOR(l, 1, m) f[k][l] = 1000;
                f[i][j] = 0;
                dfs(i, j, 0);
                FOR(k, 1, n){
                    FOR(l, 1, m){
                        if( s[k][l] == 'B' && f[k][l] > 1 ) {
                            printf("NO");
                            return;
                        }
                    }
                }
            }
        }
    }
    printf("YES");
}

int main(){
    //freopen("inp.txt", "r", stdin);
    input();
    process();
    return 0;
}


#include <stdio.h>
#include <string.h>
#define FOR(i,a ,b) for(int i = a; i <= b; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

inline int readInt(){
    int x = 0, f = 1;
    char ch = getchar();
    while( ch < '0' || ch > '9' ) {
        if ( ch == '-' ) f = 0;
        ch = getchar();
    }
    while( ch >= '0' && ch <= '9' ) {
        x = (x << 1) + (x << 3) + (ch - '0');
        ch = getchar();
    };
    if( f ) return x;
    return -x;
}

const int N = 1000;

int n, m, f[N][N];
char s[N][N];
int _dx[4] = {1, -1 , 0,  0 };
int _dy[4] = {0,  0,  1, -1 };
int _di[4] = {1,  2,  3,  4 };

int in(int x,int y){
    return (x >= 1 && x <= n && y >= 1 && y <= m);
}

void input(){
    n = readInt(); m = readInt();
    FOR(i, 1, n)    scanf("%s", s[i] + 1);
}

void dfs(int x,int y,int dir){
    FOR(i, 0, 3){
        int u = x + _dx[i], v = y + _dy[i], di = _di[i], t;
        if( dir == 0 || dir == di ) t = 0;
        else t = 1;
        if( in(u, v) && f[u][v] > f[x][y] + t && s[u][v] == 'B' ){
            f[u][v] = f[x][y] + t;
            dfs(u, v, di);
        }
    }
}

void process(){
    FOR(i, 1, n){
        FOR(j, 1, m){
            if( s[i][j] == 'B' ){
                FOR(k, 1, n) FOR(l, 1, m) f[k][l] = 1000;
                f[i][j] = 0;
                dfs(i, j, 0);
                FOR(k, 1, n){
                    FOR(l, 1, m){
                        if( s[k][l] == 'B' && f[k][l] > 1 ) {
                            printf("NO");
                            return;
                        }
                    }
                }
            }
        }
    }
    printf("YES");
}

int main(){
    //freopen("inp.txt", "r", stdin);
    input();
    process();
    return 0;
}


#include <stdio.h>
#include <string.h>
#define FOR(i,a ,b) for(int i = a; i <= b; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

inline int readInt(){
    int x = 0, f = 1;
    char ch = getchar();
    while( ch < '0' || ch > '9' ) {
        if ( ch == '-' ) f = 0;
        ch = getchar();
    }
    while( ch >= '0' && ch <= '9' ) {
        x = (x << 1) + (x << 3) + (ch - '0');
        ch = getchar();
    };
    if( f ) return x;
    return -x;
}

const int N = 1000;

int n, m, f[N][N];
char s[N][N];
int _dx[4] = {1, -1 , 0,  0 };
int _dy[4] = {0,  0,  1, -1 };
int _di[4] = {1,  2,  3,  4 };

int in(int x,int y){
    return (x >= 1 && x <= n && y >= 1 && y <= m);
}

void input(){
    n = readInt(); m = readInt();
    FOR(i, 1, n)    scanf("%s", s[i] + 1);
}

void dfs(int x,int y,int dir){
    FOR(i, 0, 3){
        int u = x + _dx[i], v = y + _dy[i], di = _di[i], t = 0;
        if( dir == 0 || dir == di ) t = 0;
        else t = 1;
        if( in(u, v) && f[u][v] >= f[x][y] + t && s[u][v] == 'B' ){
            f[u][v] = f[x][y] + t;
            dfs(u, v, di);
        }
    }
}

void process(){
    FOR(i, 1, n){
        FOR(j, 1, m){
            if( i == 3 && j == 1 && s[i][j] == 'B' ){
                FOR(k, 1, n) FOR(l, 1, m) f[k][l] = 1000;
                f[i][j] = 0;
                dfs(i, j, 0);
                FOR(k, 1, n){
                    FOR(l, 1, m){
                        if( s[k][l] == 'B' && f[k][l] > 1 ) {
                            printf("NO");
                            return;
                        }
                    }
                }
            }
        }
    }
    printf("YES");
}

int main(){
    //freopen("inp.txt", "r", stdin);
    input();
    process();
    return 0;
}


n, m = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
mab = max(b)
mig = min(g)
if mab > mig:
    print(-1)
    exit()

b = sorted(b, reverse=True)
g = sorted(g)
num = 0
j = 0
for i in range(n):
    k = 0
    l = 1
    while j < m and k < m - l and b[i] <= g[j]:
        if b[i] == g[j]:
            l = 0
        num += g[j]
        j += 1
        k += 1
    num += b[i] * (m - k)

print(num)


def main():
    from sys import stdin, stdout

    def read():
        return stdin.readline().rstrip('\n')

    def read_array(sep=None, maxsplit=-1):
        return read().split(sep, maxsplit)

    def read_int():
        return int(read())

    def read_int_array(sep=None, maxsplit=-1):
        return [int(a) for a in read_array(sep, maxsplit)]

    def write(*args, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in args) + end)

    def write_array(array, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in array) + end)

    n, m = read_int_array()
    bmin = read_int_array()
    gmax = read_int_array()

    bmin.sort()
    gmax.sort()

    max_boy = bmin[-1]
    min_girl = gmax[0]
    if max_boy > min_girl:
        write(-1)
    elif max_boy == min_girl:
        bmin.pop()
        out = sum(gmax) + sum(x * m for x in bmin)
        write(out)
    else:
        bmin.pop()
        out = sum(gmax) - min_girl + max_boy
        out += min_girl + bmin[-1] * (m-1)
        bmin.pop()
        out += sum(x * m for x in bmin)
        write(out)

main()


import sys
input = sys.stdin.readline
n,m=map(int,input().split())
b=[int(k) for k in input().split()]
g=[int(k) for k in input().split()]
if max(b)>min(g):
    print(-1)
elif max(b)==min(g):
    print(sum(b)*m+sum(g)-max(b)*m)
else:
    b.sort()
    print(sum(b) * m + sum(g) - max(b) * (m-1)-b[-2])


import java.util.*

val reader = InputReader()
fun main(args: Array<String>) {
    val n = reader.nextInt()
    val m = reader.nextInt()
    val b = reader.readIntArray(n)
    val g = reader.readIntArray(m)

    g.sort()
    b.sort()
    if (g.first() < b.last()) {
        println(-1)
        return
    }

    val result = m * b.sum() - m * b.last() + g.sum()
    when {
        b.last() == g.first() -> println(result)
        else -> println(result + b.last() - b[b.size - 2])
    }
}

class InputReader {
    var tokens = StringTokenizer(readLine())

    fun next() : String {
        while (!tokens.hasMoreTokens()) {
            tokens = StringTokenizer(readLine())
        }
        return tokens.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextDouble() = next().toDouble()
    fun readIntArray(size: Int) = IntArray(size) { nextInt() }
    fun readLongArray(size: Int) = LongArray(size) { nextLong() }
}

class Graph(val V: Int, val E: Int, edges: Array<List<Int>>) {

}


import java.util.*

val reader = InputReader()
fun main(args: Array<String>) {
    val n = reader.nextInt()
    val m = reader.nextInt()
    val b = reader.readLongArray(n)
    val g = reader.readLongArray(m)

    g.sort()
    b.sort()
    if (g.first() < b.last()) {
        println(-1)
        return
    }

    val result = m.toLong() * b.sum() - m.toLong() * b.last() + g.sum()
    when {
        b.last() == g.first() -> println(result)
        else -> println(result + b.last() - b[b.size - 2])
    }
}

class InputReader {
    var tokens = StringTokenizer(readLine())

    fun next() : String {
        while (!tokens.hasMoreTokens()) {
            tokens = StringTokenizer(readLine())
        }
        return tokens.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextDouble() = next().toDouble()
    fun readIntArray(size: Int) = IntArray(size) { nextInt() }
    fun readLongArray(size: Int) = LongArray(size) { nextLong() }
}


import java.util.*

val reader = InputReader()
fun main(args: Array<String>) {
    val n = reader.nextInt()
    val m = reader.nextInt()
    val b = reader.readIntArray(n)
    val g = reader.readIntArray(m)

    g.sort()
    b.sort()
    if (g.first() < b.last()) {
        println(-1)
        return
    }

    val result = m.toLong() * b.sum() - m.toLong() * b.last() + g.sum()
    when {
        b.last() == g.first() -> println(result)
        else -> println(result + b.last() - b[b.size - 2])
    }
}

class InputReader {
    var tokens = StringTokenizer(readLine())

    fun next() : String {
        while (!tokens.hasMoreTokens()) {
            tokens = StringTokenizer(readLine())
        }
        return tokens.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextDouble() = next().toDouble()
    fun readIntArray(size: Int) = IntArray(size) { nextInt() }
    fun readLongArray(size: Int) = LongArray(size) { nextLong() }
}


import java.util.*

val reader = InputReader()
fun main(args: Array<String>) {
    val n = reader.nextInt()
    val m = reader.nextInt()
    val b = reader.readIntArray(n)
    val g = reader.readIntArray(m)

    g.sort()
    b.sort()
    if (g.first() < b.last()) {
        println(-1)
        return
    }

    val result = m as Long * b.sum() - m as Long* b.last() + g.sum()
    when {
        b.last() == g.first() -> println(result)
        else -> println(result + b.last() - b[b.size - 2])
    }
}

class InputReader {
    var tokens = StringTokenizer(readLine())

    fun next() : String {
        while (!tokens.hasMoreTokens()) {
            tokens = StringTokenizer(readLine())
        }
        return tokens.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextDouble() = next().toDouble()
    fun readIntArray(size: Int) = IntArray(size) { nextInt() }
    fun readLongArray(size: Int) = LongArray(size) { nextLong() }
}


@file:Suppress("NOTHING_TO_INLINE", "EXPERIMENTAL_FEATURE_WARNING", "OVERRIDE_BY_INLINE")

import java.io.PrintWriter
import java.util.StringTokenizer
import kotlin.math.*
import kotlin.random.*
import kotlin.system.exitProcess
import kotlin.collections.sort as _sort
import kotlin.collections.sortDescending as _sortDescending
import kotlin.io.println as iprintln

/** @author Spheniscine */
fun main() {
    output {
        val n = readInt()
        val m = readInt()

        val B = readIntArray(n)
        val G = readIntArray(m)

        B.sortDescending()
        val gmin = G.min()!!

        val ans = when {
            B[0] > gmin -> -1
            B[0] == gmin -> (B.sumLong() - B[0]) * m + G.sumLong()
            else -> (B.sumLong() - B[0]) * m + G.sumLong() + (B[0] - B[1])
        }

        println(ans)
    }
}

fun IntArray.sumLong() = fold(0L, Long::plus)

/** IO code start */
//@JvmField val INPUT = File("input.txt").inputStream()
//@JvmField val OUTPUT = File("output.txt").outputStream()
@JvmField val INPUT = System.`in`
@JvmField val OUTPUT = System.out

@JvmField val _reader = INPUT.bufferedReader()
fun readLine(): String? = _reader.readLine()
fun readLn() = _reader.readLine()!!
@JvmField var _tokenizer: StringTokenizer = StringTokenizer("")
fun read(): String {
    while (_tokenizer.hasMoreTokens().not()) _tokenizer = StringTokenizer(_reader.readLine() ?: return "", " ")
    return _tokenizer.nextToken()
}
fun readInt() = read().toInt()
fun readDouble() = read().toDouble()
fun readLong() = read().toLong()
fun readStrings(n: Int) = List(n) { read() }
fun readLines(n: Int) = List(n) { readLn() }
fun readInts(n: Int) = List(n) { read().toInt() }
fun readIntArray(n: Int) = IntArray(n) { read().toInt() }
fun readDoubles(n: Int) = List(n) { read().toDouble() }
fun readDoubleArray(n: Int) = DoubleArray(n) { read().toDouble() }
fun readLongs(n: Int) = List(n) { read().toLong() }
fun readLongArray(n: Int) = LongArray(n) { read().toLong() }

@JvmField val _writer = PrintWriter(OUTPUT, false)
inline fun output(block: PrintWriter.() -> Unit) { _writer.apply(block).flush() }

/** shuffles and sort overrides to avoid quicksort attacks */
private inline fun <R, V> R._shuffle(rnd: Random, get: R.(Int) -> V, set: R.(Int, V) -> Unit, size: Int) {
    // Fisher-Yates shuffle algorithm
    for (i in size - 1 downTo 1) {
        val j = rnd.nextInt(i + 1)
        val temp = get(i)
        set(i, get(j))
        set(j, temp)
    }
}

@JvmField var _random: Random? = null
val random get() = _random ?: Random(0x594E215C123 * System.nanoTime()).also { _random = it }

fun IntArray.shuffle(rnd: Random = random) = _shuffle(rnd, IntArray::get, IntArray::set, size)
fun IntArray.sort() { shuffle(); _sort() }
fun IntArray.sortDescending() { shuffle(); _sortDescending() }

fun LongArray.shuffle(rnd: Random = random) = _shuffle(rnd, LongArray::get, LongArray::set, size)
fun LongArray.sort() { shuffle(); _sort() }
fun LongArray.sortDescending() { shuffle(); _sortDescending() }

fun DoubleArray.shuffle(rnd: Random = random) = _shuffle(rnd, DoubleArray::get, DoubleArray::set, size)
fun DoubleArray.sort() { shuffle(); _sort() }
fun DoubleArray.sortDescending() { shuffle(); _sortDescending() }

fun CharArray.shuffle(rnd: Random = random) = _shuffle(rnd, CharArray::get, CharArray::set, size)
inline fun CharArray.sort() { _sort() }
inline fun CharArray.sortDescending() { _sortDescending() }

inline fun <T: Comparable<T>> Array<out T>.sort() = _sort()
inline fun <T: Comparable<T>> Array<out T>.sortDescending() = _sortDescending()
inline fun <T: Comparable<T>> MutableList<out T>.sort() = _sort()
inline fun <T: Comparable<T>> MutableList<out T>.sortDescending() = _sortDescending()

fun `please stop removing these imports IntelliJ`() {
    iprintln(max(1, 2))
}


import java.util.Arrays;
import java.util.Scanner;

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Andy Phan
 */
public class p1159c {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        Long[] a = new Long[n];
        Long[] b = new Long[m];
        for(int i = 0; i < n; i++) a[i] = in.nextLong();
        for(int i = 0; i < m; i++) b[i] = in.nextLong();
        Arrays.sort(b);
        Arrays.sort(a);
        long[] sum = new long[m+1];
        for(int i = m-1; i >= 0; i--) sum[i] = sum[i+1]+b[i];
        if(a[n-1] > b[0]) {
            System.out.println(-1);
            return;
        }
        long tot = 0;
        for(int i = 0; i < n-2; i++) {
            tot += a[i]*m;
        }
        if(a[n-1] < b[0])
            tot += sum[1]+a[n-1]+a[n-2]*(m-1)+b[0];
        else
            tot += sum[0]+a[n-2]*m;
        System.out.println(tot);
    }
}