import java.awt.Point;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * Validator for Window Shopping, making sure that all .'s are reachable from U & D.
 * Everything else is checked by the VIVA pattern.
 *
 * @author vanb
 */
public class windows_validator
{
    /** The Mall! */
    private static char mall[][];
    
    /**
     * Mark a cell at [i,j] as reachable, and move to adjacent cells.
     * 
     * @param i
     * @param j
     */
    private static void mark( int i, int j )
    {
        if( i>=0 && i<mall.length && j>=0 && j<mall[i].length && (mall[i][j]=='.' || mall[i][j]=='U' || mall[i][j]=='D') )
        {
            mall[i][j] = 'X';
            mark( i+1, j );
            mark( i-1, j );
            mark( i, j+1 );
            mark( i, j-1 );
        }
    }
    
    /**
     * Test whether a cell is reachable.
     * 
     * @param i
     * @param j
     * @return true if cell at [i,j] is reachable
     */
    private static boolean reachable( int i, int j )
    {
        return i>=0 && i<mall.length && j>=0 && j<mall[i].length && mall[i][j]=='X';
    }
    
    /**
     * Main.
     *
     * @param args unused
     * @throws Exception the exception
     */
    public static void main( String[] args ) throws Exception
    {
        Scanner sc = new Scanner( System.in );
        
        int r = sc.nextInt();
        int c = sc.nextInt();
        
        mall = new char[r][];
        for( int i=0; i<r; i++ ) mall[i] = sc.next().toCharArray();
       
        int upi = -1;
        int upj = -1;
        for( int i=0; i<r; i++ ) for( int j=0; j<c; j++ ) if(mall[i][j]=='U')
        {
            upi = i;
            upj = j;
        }
        
        mark( upi+1, upj );
        mark( upi-1, upj );
        mark( upi, upj+1 );
        mark( upi, upj-1 );
        
        boolean ok = true;
        for( int i=0; i<r; i++ ) for( int j=0; j<c; j++ ) 
        {
            if( mall[i][j]=='.' || mall[i][j]=='U' || mall[i][j]=='D' ) ok = false;
        }
                        
        sc.close();
        
        System.exit( ok ? 42 : 0 );
    }

}
