Problem list
Log in
#coci00045 · COCI 2007/2008 Contest 2

Prva

Silver II 1.0 s 32 MB

Little Ivica solves crossword puzzles every day. In case you haven't seen one, a crossword puzzle starts on a grid of $R \times C$ squares, each of which is either empty or blocked. The player's task is to write words in consecutive empty squares vertically (top down) or horizontally (left to right).

Ivica's sister has a strange habit of looking at crosswords Ivica has finished solving, and finding the lexicographically smallest word in it. She only considers words at least $2$ characters long.

Write a program that, given a crossword puzzle, finds that word.

Input

The first line contains two integers $R$ and $C$ ($2 \le R, C \le 20$), the number of rows and columns in the crossword.

Each of the following $R$ lines contains a string of $C$ characters. Each of those characters is either a lowercase letter of the English alphabet, or the character # representing a blocked square.

The input will be such that a solution will always exist.

Output

Output the lexicographically smallest word in the crossword.

Example 1

Input
4 4
luka
o#a#
kula
i#a#
Output
kala

Example 2

Input
4 4
luka
o#a#
kula
i#as
Output
as

Example 3

Input
4 5
adaca
da##b
abb#b
abbac
Output
abb
Source
COCI 2007/2008 Contest 2
Appears in
Not part of a ladder, contest or set yet.
Accepted 1 Submissions 1 Acceptance 100% Solvers 1

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code