import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Closeable; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Queue; import java.util.Random; import java.util.TreeMap; public class Main { static ContestScanner in;static Writer out;static StringBuilder sb=new StringBuilder(); public static void main(String[] args) {try{in=new ContestScanner();out=new Writer();Main solve=new Main();solve.solve(); in.close();out.flush();out.close();}catch(IOException e){e.printStackTrace();}} static void dump(int[]a){sb.setLength(0);for(int i=0;i0){if((n&1)==1)r*=a;a*=a;n>>=1;}return r;} static String itob(int a,int l){return String.format("%"+l+"s",Integer.toBinaryString(a)).replace(' ','0');} static void sort(int[]a){m_sort(a,0,a.length,new int[a.length]);} static void sort(int[]a,int l){m_sort(a,0,l,new int[l]);} static void sort(int[]a,int l,int[]buf){m_sort(a,0,l,buf);} static void sort(int[]a,int s,int l,int[]buf){m_sort(a,s,l,buf);} static void m_sort(int[]a,int s,int sz,int[]b) {if(sz<7){for(int i=s;is&&a[j-1]>a[j];j--)swap(a, j, j-1);return;} m_sort(a,s,sz/2,b);m_sort(a,s+sz/2,sz-sz/2,b);int x=s;int l=s,r=s+sz/2;final int le=s+sz/2,re=s+sz; while(la[r])b[x++]=a[r++];else b[x++]=a[l++];} while(rs&&a[j-1]>a[j];j--)swap(a, j, j-1);return;} m_sort(a,s,sz/2,b);m_sort(a,s+sz/2,sz-sz/2,b);int x=s;int l=s,r=s+sz/2;final int le=s+sz/2,re=s+sz; while(la[r])b[x++]=a[r++];else b[x++]=a[l++];} while(rv)r=m-1;else{l=m+1;s=m;}}return s;} static int binarySearchSmallerMax(int[]a,int v,int l,int r) {int s=-1;while(l<=r){int m=(l+r)/2;if(a[m]>v)r=m-1;else{l=m+1;s=m;}}return s;} List[]randomTree(int n,Random r){List[]g=graph(n); for(int i=1;i[]graph(int n){List[]g=new List[n]; for(int i=0;i();return g;} void solve() throws NumberFormatException, IOException{ w = in.nextInt(); h = in.nextInt(); char[][] map = new char[h][]; Queue qu = new ArrayDeque<>(); for(int i=0; i que = new ArrayDeque<>(); que.add(new Pair(sy, sx)); while(!qu.isEmpty()){ Pair p = qu.poll(); final int y = p.a; final int x = p.b; for(int i=0; i<4; i++){ final int ny = y+dy[i]; final int nx = x+dx[i]; if(out(ny, nx) || map[ny][nx]=='#' || dist[ny][nx] != inf) continue; dist[ny][nx] = 0; Pair np = new Pair(ny, nx); qu.add(np); que.add(np); } } qu = que; int ans = inf; out:while(!qu.isEmpty()){ Pair p = qu.poll(); final int y = p.a; final int x = p.b; for(int i=0; i<4; i++){ final int ny = y+dy[i]; final int nx = x+dx[i]; final int nd = dist[y][x]+1; if(out(ny, nx) || dist[ny][nx] != inf) continue; if(dist[ny][nx]==inf && map[ny][nx]=='.'){ ans = nd; break out; } dist[ny][nx] = nd; qu.add(new Pair(ny, nx)); } } System.out.println(ans-1); } int h, w; boolean out(int y, int x){ return y<0 || y>=h || x<0 || x>=w; } } class MultiSet extends HashMap{ @Override public Integer get(Object key){return containsKey(key)?super.get(key):0;} public void add(T key,int v){put(key,get(key)+v);} public void add(T key){put(key,get(key)+1);} public void sub(T key){final int v=get(key);if(v==1)remove(key);else put(key,v-1);} public MultiSet merge(MultiSet set) {MultiSets,l;if(this.size()e:s.entrySet())l.add(e.getKey(),e.getValue());return l;} } class OrderedMultiSet extends TreeMap{ @Override public Integer get(Object key){return containsKey(key)?super.get(key):0;} public void add(T key,int v){put(key,get(key)+v);} public void add(T key){put(key,get(key)+1);} public void sub(T key){final int v=get(key);if(v==1)remove(key);else put(key,v-1);} public OrderedMultiSet merge(OrderedMultiSet set) {OrderedMultiSets,l;if(this.size(){ int a,b;final int hash;Pair(int a,int b){this.a=a;this.b=b;hash=(a<<16|a>>16)^b;} public boolean equals(Object obj){Pair o=(Pair)(obj);return a==o.a&&b==o.b;} public int hashCode(){return hash;} public int compareTo(Pair o){if(a!=o.a)return a