This page is still under construction.

Parts of this page are still being built. What you see may change.

Stack Maze

Time limit8sMemory limit256 MB

Summary
You move only right or down through the grid, pick up lettered jewels, and drop them into matching holes in last-in-first-out order for the most matches.
Level

Hard8 of 10

Topics
Dynamic programming, Stack, Graph
Solved
No attempts yet

Problem

The maze is a grid WW cells wide and HH cells tall. The upper left cell is (1,1)(1, 1) and the lower right cell is (W,H)(W, H). You start at (1,1)(1, 1) and have to reach (W,H)(W, H), and you can only move one cell to the right or one cell down.

Here is an example of a maze.

...#......
a###.#####
.bc...A...
##.#C#d#.#
.#B#.#.###
.#...#e.D.
.#A..###.#
..e.c#..E.
####d###.#
#....#.#.#
##E...d.C.

Some cells are free (.) and some are blocked by a rock (#), which you cannot enter. Some free cells hold a jewel (a lowercase letter) and some hold a hole for a jewel (an uppercase letter). Different letters mean different types of jewel, so a cell marked a holds a jewel of type A and a cell marked A holds a hole for a jewel of type A. Placing a jewel into a hole of its own type makes something happy happen.

On a cell with a jewel you choose whether to pick the jewel up. On a cell with a hole you choose whether to place a jewel you carry. You start with no jewels. Your bag is very large, so you can carry any number of jewels. The bag is a stack, so you can only take out the jewel you picked up last.

On the way from (1,1)(1, 1) to (W,H)(W, H), how many jewels can you place into holes of the matching type?

Input

The input holds a sequence of datasets. A line with two zeros ends the input, and that line is not a dataset.

Each dataset has this format.

H W
C11C12...C1W
C21C22...C2W
...
CH1CH2...CHW

HH and WW are the height and the width of the grid, and 1≤W,H≤501 \le W, H \le 50. Each of the next HH lines holds WW characters with no spaces between them. The jj-th character CijC_{ij} of the ii-th line gives the type of the cell in row ii and column jj as described above. The starting cell (row 1, column 1) and the destination cell (row HH, column WW) are never #.

Within one dataset, each lowercase letter and each uppercase letter appears at most 10 times.

Output

For each dataset, print on one line the maximum number of jewels you can place into holes of the matching type. Print -1 if you cannot reach the cell (W,H)(W, H).

Examples2

  1. Example 1

    Input
    3 3
    ac#
    b#C
    .BA
    3 3
    aaZ
    a#Z
    aZZ
    3 3
    ..#
    .#.
    #..
    1 50
    abcdefghijklmnopqrstuvwxyYXWVUTSRQPONMLKJIHGFEDCBA
    1 50
    aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyY
    1 50
    abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY
    1 50
    aaaaaaaaaabbbbbbbbbbcccccCCCCCBBBBBBBBBBAAAAAAAAAA
    10 10
    ...#......
    a###.#####
    .bc...A...
    ##.#C#d#.#
    .#B#.#.###
    .#...#e.D.
    .#A..###.#
    ..e.c#..E.
    ####d###.#
    ##E...D.C.
    0 0
    
    Expected output
    2
    0
    -1
    25
    25
    1
    25
    4
    
  2. Example 2

    Input
    1 4
    abAB
    1 4
    abBA
    1 6
    abcCBA
    1 5
    aAaAa
    0 0
    
    Expected output
    1
    2
    3
    2