import java.util.* ;
public class window_tgr {
   final int MAX = 32 ;
   final int BLOCKED = 15 ;
   final int STORE = 14 ;
   final int NEWHALL = 13 ;
   char[][] b = new char[MAX][MAX] ;
   int[] row = new int[MAX] ;
   int closed, w, h ;
   int[] cnts = new int[16] ;
   long packstate() {
      long r = closed ;
      for (int i=0; i<16; i++)
         cnts[i] = -1 ;
      int halln = 0 ;
      for (int i=1; i<=w; i++) {
         if (row[i] < STORE) {
            if (cnts[row[i]] < 0)
               cnts[row[i]] = halln++ ;
            row[i] = cnts[row[i]] ;
         }
         r = (r << 4) + row[i] ;
      }
      return r ;
   }
   void unpackstate(long s) {
      for (int i=0; i<16; i++)
         cnts[i] = 0 ;
      for (int i=w; i>0; i--) {
         int v = (int)(s & 15) ;
         row[i] = v ;
         cnts[v]++ ;
         s >>= 4 ;
      }
      closed = (int)s ;
   }
   int incscore(int cs, int rs, int nv, int left, int up) {
      int r = 0 ;
      if (nv == STORE) {
         if (left < STORE && b[cs][rs-1] == '.')
            r++ ;
         if (up < STORE && b[cs-1][rs] == '.')
            r++ ;
      } else if (nv < STORE && b[cs][rs] == '.') {
         if (left == STORE)
            r++ ;
         if (up == STORE)
            r++ ;
      }
      return r ;
   }
   void rewriterow(int ov, int nv) {
      for (int i=1; i<=w; i++)
         if (row[i] == ov)
            row[i] = nv ;
   }
   void solve() throws Exception {
      Scanner sc = new Scanner(System.in) ;
      h = sc.nextInt() ;
      w = sc.nextInt() ;
      for (int i=0; i<MAX; i++)
         for (int j=0; j<MAX; j++)
            b[i][j] = '#' ;
      for (int i=0; i<MAX; i++)
         row[i] = BLOCKED ;
      for (int i=0; i<h; i++) {
         String s = sc.next() ;
         for (int j=0; j<w; j++)
            b[i+1][j+1] = s.charAt(j) ;
      }
      if (h < w) {
         for (int i=0; i<MAX; i++)
            for (int j=i+1; j<MAX; j++) {
               char t = b[i][j] ;
               b[i][j] = b[j][i] ;
               b[j][i] = t ;
            }
         int t = h ;
         h = w ;
         w = t ;
      }
      HashMap<Long, Integer> states = new HashMap<Long, Integer>() ;
      HashMap<Long, Integer> nstates = new HashMap<Long, Integer>() ;
      long initstate = packstate() ;
      states.put(initstate, 0) ;
      int esccount = 0 ;
      for (int cs=1; cs<=h+1; cs++) {
         for (int rs=1; rs<=w; rs++) {
            int ri = rs ;
            int is = b[cs][rs] ;
            if (is == 'U' || is == 'D')
               esccount++ ;
            nstates.clear() ;
            for (Map.Entry me: states.entrySet()) {
               for (int nv=NEWHALL; nv<=BLOCKED; nv++) {
                  if ((is == '#') != (nv == BLOCKED))
                     continue ;
                  if ((is == 'U' || is == 'D') && nv != NEWHALL)
                     continue ;
                  unpackstate((Long)me.getKey()) ;
                  int closing = 0 ;
                  if (row[ri] < STORE && cnts[row[ri]] == 1 && nv != NEWHALL)
                     closing = 1 ;
                  if (closing != 0 && cnts[1] > 0)
                     continue ;
                  if (closed != 0 && closing != 0)
                     continue ;
                  if (esccount < 2 && closing != 0)
                     continue ;
                  closed |= closing ;
                  int score = (Integer)me.getValue() + incscore(cs, rs, nv, row[ri-1], row[ri]) ;
                  if (nv == NEWHALL) {
                     if (row[ri] < STORE && row[ri-1] < STORE) {
                        if (row[ri] != row[ri-1])
                           rewriterow(row[ri-1], row[ri]) ;
                     } else if (row[ri] < STORE) {
                        // leave it be
                     } else if (row[ri-1] < STORE) {
                        row[ri] = row[ri-1] ;
                     } else {
                        row[ri] = NEWHALL ;
                     }
                  } else {
                     row[ri] = nv ;
                  }
                  long ns = packstate() ;
                  if (nstates.containsKey(ns))
                     nstates.put(ns, Math.max(score, nstates.get(ns))) ;
                  else
                     nstates.put(ns, score) ;
               }
            }
            HashMap<Long, Integer> t = states ;
            states = nstates ;
            nstates = t ;
         }
      }
      int r = 0 ;
      for (Map.Entry me: states.entrySet())
         r = Math.max(r, (Integer)me.getValue()) ;
      System.out.println(r) ;
   }
   public static void main(String[] args) throws Exception {
      new window_tgr().solve() ;
   }
}
