#include <iostream>
#include <string>
#include <vector>
using namespace std ;
int main() {
   int r, c ;
   cin >> r >> c;
   string b, s ;
   for (int i=0; i<2*c; i++)
      b.push_back('.') ;
   for (int i=0; i<r; i++) {
      cin >> s ;
      b += s ;
      b.push_back('.') ;
   }
   for (int i=0; i<2*c; i++)
      b.push_back('.') ;
   vector<int> q ;
   vector<int> dist(b.size()) ;
   for (int i=c; i+c<(int)b.size(); i++)
      if (b[i] != 'X') {
         q.push_back(i) ;
         dist[i] = 0 ;
      }
   int d[4] ;
   d[0] = -c-1 ;
   d[1] = -1 ;
   d[2] = 1 ;
   d[3] = c+1 ;
   for (int qg=0; qg<(int)q.size(); qg++) {
      int a = q[qg] ;
      for (int dd=0; dd<4; dd++) {
         int t = a + d[dd] ;
         if (b[t] == 'X') {
            b[t] = '.' ;
            dist[t] = dist[a] + 1 ;
            q.push_back(t) ;
         }
      }
   }
   cout << dist[q[q.size()-1]] << endl ;
}
