import scala.collection.mutable

object Main extends App {

  import scala.io.StdIn._
  solve
  def solve: Unit = {
    val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
    if (n != 0 && m != 0){
      val state = Array.tabulate(m){_ ⇒ readLine.trim.toCharArray.map(State(_))}
      val memo = Array.tabulate(m){_ ⇒ Array.tabulate(n){_ ⇒ Int.MaxValue}}
      val queue: mutable.PriorityQueue[(Coordinate, Int)] = mutable.PriorityQueue[(Coordinate, Int)]()(Ordering.by[(Coordinate, Int), Int](_._2).reverse)
      for (y ← 0 until m) {
        memo(y)(0) = 0
        memo(y)(n - 1) = 0
        queue.enqueue(Coordinate(0, y) → 0, Coordinate(n - 1, y) → 0)
      }
      for (x ← 1 until n - 1){
        memo(0)(x) = 0
        memo(m - 1)(x) = 0
        queue.enqueue(Coordinate(x, 0) → 0, Coordinate(x, m - 1) → 0)
      }

      while (queue.head._1.let(c ⇒ state(c.y)(c.x) != Castle)) {
        val (c, d) = queue.dequeue()
        if (memo(c.y)(c.x) == d){
          for (next@Coordinate(x, y) ← c.neighbors){
            if ((0 until n).contains(x) && (0 until m).contains(y)) {
              (state(c.y)(c.x), state(y)(x)) match{
                case (Moat, Moat) if memo(y)(x) > d ⇒ {
                  memo(y)(x) = d
                  queue.enqueue(next → d)
                }
                case (Moat, _) if memo(y)(x) > d + 1 ⇒ {
                  memo(y)(x) = d + 1
                  queue.enqueue(next → (d + 1))
                }
                case (Plane, _) if memo(y)(x) > d ⇒ {
                  memo(y)(x) = d
                  queue.enqueue(next → d)
                }
                case _ ⇒
              }
            }
          }
        }
      }
      println(
        queue.head._2
      )
      solve
    }
  }
  object State{
    def apply(char: Char): State = char match{
      case '&' ⇒ Castle
      case '.' ⇒ Plane
      case '#' ⇒ Moat
      case _ ⇒ throw new Exception
    }
  }
  sealed trait State
  object Moat extends State
  object Castle extends State
  object Plane extends State
  case class Coordinate(x: Int, y: Int) {
    def neighbors: Iterable[Coordinate] = List(Coordinate(0, 1), Coordinate(0, -1), Coordinate(1, 0), Coordinate(-1, 0)).map(_ + this)
    def +(that: Coordinate): Coordinate = Coordinate(this.x + that.x, this.y + that.y)
  }
  implicit class ScopeFunc[T](val value:T){
    def let[A](func: T ⇒ A): A = func(value)
  }
}
