Under construction

Still being built. Things may change

Harvest Waterloo

Time limit1sMemory limit1024 MB

Summary
Given a grid of pumpkins and hay bales, the task sums the values of all pumpkins reachable by orthogonal moves from a start cell.
Level

Easy2 of 10

Topics
Graph, BFS
Solved
No attempts yet

Problem

A new harvest simulation game, Harvest Waterloo, is wildly popular. The game is played on a rectangular pumpkin patch that contains bales of hay and pumpkins of different sizes. To begin, a farmer is placed on the location of a pumpkin.

The farmer harvests every pumpkin they can reach by moving left, right, up, and down. The farmer cannot move diagonally, cannot move through a bale of hay, and cannot move outside the patch.

A small pumpkin is worth $1, a medium pumpkin is worth $5, and a large pumpkin is worth $10. Determine the total value in dollars of all pumpkins the farmer harvests.

Input

The first line contains an integer RR (R>0R > 0), the number of rows in the patch.

The second line contains an integer CC (C>0C > 0), the number of columns in the patch.

The next RR lines describe the patch. Each line has CC characters. S is a small pumpkin, M is a medium pumpkin, L is a large pumpkin, and * is a bale of hay.

The next line contains an integer AA (0≤A<R0 \le A < R), and the last line contains an integer BB (0≤B<C0 \le B < C). Row AA and column BB is the farmer's starting location. The top-left corner of the patch is row 0, column 0.

Output

Output the integer VV, the total value in dollars of all pumpkins the farmer harvests.

Examples2

  1. Example 1

    Input
    6
    6
    **LMLS
    S*LMMS
    S*SMSM
    ******
    LLM*MS
    SSL*SS
    5
    1
    
    Expected output
    37
    
  2. Example 2

    Input
    6
    6
    **LMLS
    S*LMMS
    S*SMSM
    ***SLL
    LLM*MS
    SSL*SS
    2
    4
    
    Expected output
    88