import scala.io.StdIn._
object Main extends App {
  val Array(h, w) = readLine.trim.split(' ').map(_.toInt)
  val map = Array.fill(h){readLine.trim.toCharArray}
  val buildings = (0 until w).flatMap{x ⇒ (0 until h).collect{
    case y if map(y)(x) == 'B' ⇒ Coordinate(x, y)
  }}.toArray.sortBy(c ⇒ c.x + c.y)
  val building2 = buildings.sortBy(c ⇒ c.x - c.y)
  println(
    math.max(buildings.head.manhattanDistance(buildings.last), building2.head.manhattanDistance(building2.last))
  )
}

case class Coordinate(x: Int, y: Int) {
  def manhattanDistance(that: Coordinate): Int = math.abs(x - that.x) + math.abs(y - that.y)
}
