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(solve).foreach(println)
  }

  def solve(input: Seq[String]): String = {
    val list = makeString(toArray(input)).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
  }

  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 toArray(input: Seq[String]): Array[Array[Char]] = {
    val h = input.size
    val w = input(0).size
    val ret = Array.ofDim[Char](h, w)
    for (i <- 0 until h; j <- 0 until w) {
      ret(i)(j) = input(i)(j)
    }
    ret
  }

  def makeString(arr: Array[Array[Char]]): Seq[String] = {
    val h = arr.size
    val w = arr(0).size
    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 {
      arr(starti)(startj) + Iterator
        .iterate(((starti + d._1 + h) % h, (startj + d._2 + w) % w))(pos => ((pos._1 + d._1 + h) % h, (pos._2 + d._2 + w) % w))
        .takeWhile(_ != (starti, startj))
        .map(pos => arr(pos._1)(pos._2))
        .mkString("")
    }
  }

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