object Main extends App {
  override def main(args: Array[String]): Unit = {
    import scala.io.{StdIn => in}
    Iterator
      .continually(in.readLine())
      .takeWhile(_ != "0 0")
      .map(str =>
        for (x <- 1 to str.split(" ")(0).toInt) yield in.readLine()
      ).map(new Solver(_).solve).foreach(println)
  }
}

class Solver(input: Seq[String]) {
  val h = input.size
  val w = input(0).size
  val arr = Array.ofDim[Char](h, w)
  for (i <- 0 until h; j <- 0 until w) arr(i)(j) = input(i)(j)

  def solve: String = {
    val list = makeAllList.sorted
    val ans = list.zip(list.drop(1)).map(s => getDuplicate(s._1, s._2)).max(new StringComparator)
    if (ans.size < 2) "0" else ans
  }

  def getDuplicate(str1: String, str2: String): String = str1.zip(str2).takeWhile(c => c._1 == c._2).map(_._1).mkString("")

  class StringComparator extends Ordering[String] {
    override def compare(x: String, y: String) = if (x.length != y.length) x.length.compare(y.length) else y.compareTo(x)
  }

  def makeAllList: Seq[String] = {
    val direction = List((0, 1), (1, 0), (-1, 0), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1))
    for (starti <- 0 until h; startj <- 0 until w; d <- direction) yield {
      val head = arr(starti)(startj)
      val initPosition = (starti, startj)
      val left = Iterator
        .iterate(movePointer(initPosition, d))(pos => movePointer(pos, d))
        .takeWhile(_ != (starti, startj))
        .map(pos => arr(pos._1)(pos._2))
        .mkString("")
      head + left
    }
  }

  def movePointer(pos: (Int, Int), direction: (Int, Int)): (Int, Int) = (((pos._1 + direction._1 + h) % h), ((pos._2 + direction._2 + w) % w))
}