Problem list
Log in
#usaco0188 · USACO 2014-2015 US Open Bronze

Palindromic Paths (Bronze)

Gold III 2.0 s 256 MB

Farmer John's farm is an N x N grid of fields (2 <= N <= 18), each labeled with a letter A..Z.

Each day, Bessie walks from the upper-left field to the lower-right field, each step taking her either one field to the right or one field downward. She keeps track of the string she generates from the letters she walks across, and gets disoriented if this string is a palindrome.

Please help Bessie determine the number of different palindromes she can form. Different ways of forming the same palindrome only count once.

Input

Line 1: N. The remaining N lines contain the N rows of the grid; each row contains N characters in the range A..Z.

Output

The number of distinct palindromes Bessie can form.

Example 1

Input
4
ABCD
BXZX
CDXB
WCBA
Output
4

Output details: The four distinct palindromes are ABCDCBA, ABCWCBA, ABXZXBA, ABXDXBA.

Source
USACO 2014-2015 US Open Bronze
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

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