import scala.annotation.tailrec
import scala.collection.mutable

object Main extends App {
  import scala.io.StdIn._
  val directions = Array(Up, Left, Down, Right)

  val Array(h, w) = readLine.trim.split(' ').map(_.toInt)
  val state = Array.tabulate(h){_ ⇒ readLine.trim.map(_ == '#').toArray}
  val allBeaches = exploreAllBeach()
  val map = IslandMap.byBeach(allBeaches)
  var lands = Nil: List[Island]
  for (y ← 0 until h; x ← 0 until w){
    if (state(y)(x)) lands ::= takeLands(Coordinate(x, y))
  }
  val start = lands.find(map.isMatch).get.leftTop + map.origin
  println(
    s"! ${start.y} ${start.x}"
  )
  def takeLands(start: Coordinate): Island = {
    val queue = mutable.Queue[Coordinate](start)
    var res = List(start): List[Coordinate]
    state(start.y)(start.x) = false
    while(queue.nonEmpty){
      val top = queue.dequeue()
      for (n ← directions.map(top.move) if state.indices.contains(n.y) && state(n.y).indices.contains(n.x)){
        if (state(n.y)(n.x)){
          state(n.y)(n.x) = false
          res ::= n
          queue.enqueue(n)
        }
      }
    }
    new Island(res)
  }

  class Island(lands: List[Coordinate]) {
    val minX = lands.minBy(_.x).x
    val minY = lands.minBy(_.y).y
    val leftTop = Coordinate(minX, minY)
    val size = lands.length
    val relationalLands = lands.map(_ - leftTop)
  }
  object IslandMap {
    def byBeach(beaches: List[Coordinate]): IslandMap = {
      val minX = beaches.minBy(_.x).x
      val minY = beaches.minBy(_.y).y
      val maxX = beaches.maxBy(_.x).x
      val maxY = beaches.maxBy(_.y).y
      val state = Array.tabulate(maxY - minY + 3){_ ⇒ Array.tabulate(maxX - minX + 3){_ ⇒ -1}}
      val leftTop = Coordinate(minX, minY)
      for (b ← beaches.map(_ - leftTop)){
        state(b.y + 1)(b.x + 1) = 1
      }
      val queue = mutable.Queue[Coordinate](Coordinate(0, 0))
      while (queue.nonEmpty){
        val top = queue.dequeue()
        for (n ← directions.map(top.move)) {
          if (state.indices.contains(n.y) && state(n.y).indices.contains(n.x) && state(n.y)(n.x) == -1) {
            state(n.y)(n.x) = 0
            queue.enqueue(n)
          }
        }
      }
      new IslandMap((1 to maxY - minY + 1).map(i ⇒ (1 to maxX - minX + 1).map(state(i)(_) != 0).toArray).toArray, Coordinate(-minX, -minY))
    }
  }
  class IslandMap(private[this] val lands: Array[Array[Boolean]], val origin: Coordinate) {
    val size = lands.map(_.count(i ⇒ i)).sum
    def isMatch(islands: Island): Boolean = {
      if (size != islands.size) false
      else islands.relationalLands.forall(c ⇒ lands.indices.contains(c.y) && lands(c.y).indices.contains(c.x) && lands(c.y)(c.x))
    }
  }
  @tailrec
  def exploreAllBeach(position: Coordinate = Coordinate(0, 0)): List[Coordinate] = {
    if (com(Left)) exploreAllBeach(position.move(Left))
    else allBeach(position, position, Left, Nil)
  }
  @tailrec
  def allBeach(init: Coordinate, current: Coordinate, direction: Direction, result: List[Coordinate]): List[Coordinate] = {
    if (init == current && direction == Left && result.nonEmpty) result
    else if (com(direction.clockwiseNext)){
      if (com(direction)) allBeach(init, current.move(direction.clockwiseNext).move(direction), direction.counterClockwiseNext, current::result)
      else allBeach(init, current.move(direction.clockwiseNext), direction, current::result)
    }else {
      allBeach(init, current, direction.clockwiseNext, if (result.isEmpty) current::result else result)
    }
  }
  def com(direction: Direction): Boolean = {
    println(direction match {
      case Up ⇒ "? U"
      case Right ⇒ "? R"
      case Down ⇒ "? D"
      case Left ⇒ "? L"
    })
    readLine.trim == "YES"
  }

  sealed trait Direction {
    def clockwiseNext: Direction
    def counterClockwiseNext: Direction
  }
  case object Right extends Direction {
    override def clockwiseNext: Direction = Down
    override def counterClockwiseNext: Direction = Up
  }
  case object Down extends Direction {
    override def clockwiseNext: Direction = Left
    override def counterClockwiseNext: Direction = Right
  }
  case object Left extends Direction{
    override def clockwiseNext: Direction = Up
    override def counterClockwiseNext: Direction = Down
  }
  case object Up extends Direction {
    override def clockwiseNext: Direction = Right
    override def counterClockwiseNext: Direction = Left
  }
  case class Coordinate(x: Int, y: Int) {
    def +(that: Coordinate): Coordinate = Coordinate(x + that.x, y + that.y)
    def -(that: Coordinate): Coordinate = Coordinate(x - that.x, y - that.y)
    def move(direction: Direction): Coordinate = direction match {
      case Right ⇒ Coordinate(x + 1, y)
      case Up ⇒ Coordinate(x, y - 1)
      case Left ⇒ Coordinate(x - 1, y)
      case Down ⇒ Coordinate(x, y + 1)
    }
  }
  implicit class Scope[T](val value: T) extends AnyVal {
    @inline
    def also(@inline func: T ⇒ Unit): T = {
      func(value)
      value
    }
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
  }
}



Measured memory_bytes: 344200