extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::fmt::{Display, Error, Formatter};
use std::ops::{Index, IndexMut};
use std::collections::VecDeque;
use std::f32::MAX;

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
fn read_all(donuts: &Vec<Vec<char>>, height: i32, width: i32) -> Vec<Vec<char>> {
    let mut result = Vec::new();
    for y in 0 .. height {
        for x in 0 .. width {
            for dy in -1_i32 .. 2 {
                for dx in -1_i32 .. 2 {
                    if dx != 0 || dy != 0 {
                        let mut cx = (x + dx + width) % width;
                        let mut cy = (y + dy + height) % height;
                        let mut current = String::new();
                        current.push(donuts[y as usize][x as usize]);
                        while cx != x || cy != y {
                            //println!("x {}, y {}, cx {} dx {} , cy {} dy {}", x, y, cx, dx, cy, dy);
                            current.push(donuts[cy as usize][cx as usize]);
                            cx = (cx + dx + width) % width;
                            cy = (cy + dy + height) % height;
                        }
                        result.push(current.chars().collect());
                    }
                }
            }
        }
    }
    result.sort();
    result
}
fn common_prefix_length(a: &Vec<char>, b: &Vec<char>) -> usize {
    let mut i = 0;
    while i < a.len() && i < b.len() && a[i] == b[i] {
        i += 1;
    }
    i
}
fn main(){
    loop {
        let_all!(height: i32, width: i32);
        if height == 0 && width == 0 {
            return
        }
        let mut donuts = Vec::with_capacity(height as usize);
        for _ in 0 .. height {
            donuts.push(read_line!().chars().collect::<Vec<char>>());
        }
        let words = read_all(&donuts, height, width);
        let mut from = 0;
        let mut length = 0;
        for i in 1 .. words.len() {
            if length < words[i].len(){
                let prefix = common_prefix_length(&words[i - 1], &words[i]);
                if prefix > length {
                    from = i;
                    length = prefix;
                }
            }
        }
        if length < 2 {
            println!("0");
        }else {
            for i in 0 .. length {
                print!("{}", words[from][i]);
            }
            println!();
        }
    }
}


Measured memory_bytes: 5044