{"raw_statement":[{"iden":"statement","content":"As you probably know, Berhattan is a district of Berland's largest city and it consists of equal square blocks. There are n block lines in the east-west direction and m block lines in the south-north direction. The map shows Berhattan as a rectangle with n rows and m columns, so there are n × m blocks in total.\n\nThere are n + 1 streets running parallel in the east-west direction (horizontally), and there are m + 1 avenues running parallel in the south-north direction (vertically). Streets and avenues split the district into blocks and separate Berhattan from other districts of Berland. Each block in Berhattan is characterized by its _beauty_ bij. \n\nA pedestrian can walk only along streets and avenues. When the pedestrian walks along any of four sides of a block, we say he passes the block. Every time the pedestrian passes a block his _satisfaction_ is increased by bij. If the pedestrian has already passed the block one or more times his satisfaction is increased only by bij / 2 rounded down when he passes the block again.\n\nYou are given the map of Berhattan with the information about the blocks' beauty and the pedestrian's path along the streets and avenues. The path is given as a string containing letters '_L_', '_R_' and '_M_', where '_L_' means a 90 degree left turn, '_R_' means a 90 degree right turn, and '_M_' means walking one block forward by a street or avenue. Facing the east, the pedestrian starts his path in the north-west corner of Berhattan having zero satisfaction level. His path can cross itself and go along the same streets or avenues several times. Pedestrian's satisfaction is increased every time he moves according to the rules described above.\n\nYour task is to calculate the total satisfaction the pedestrian will get after finishing his route.\n\nThe first line of input contains two integers n and m (1 ≤ n, m ≤ 100), where n is a number of block lines in Berhattan running in the east-west direction, and m is a number of block lines in Berhattan running in the south-north direction. The following n lines contain m digits each. The j-th digit of the i-th line represents bij (0 ≤ bij ≤ 9) — the beauty of the corresponding block. The last line of input contains a path in the format specified above. The path consists of 1 up to 500 characters, inclusively. It is guaranteed that the given path doesn't go outside Berhattan.\n\nPrint a single integer to the output — the total pedestrian's satisfaction.\n\n"},{"iden":"input","content":"The first line of input contains two integers n and m (1 ≤ n, m ≤ 100), where n is a number of block lines in Berhattan running in the east-west direction, and m is a number of block lines in Berhattan running in the south-north direction. The following n lines contain m digits each. The j-th digit of the i-th line represents bij (0 ≤ bij ≤ 9) — the beauty of the corresponding block. The last line of input contains a path in the format specified above. The path consists of 1 up to 500 characters, inclusively. It is guaranteed that the given path doesn't go outside Berhattan."},{"iden":"output","content":"Print a single integer to the output — the total pedestrian's satisfaction."},{"iden":"examples","content":"Input3 3123456789MRMMLMOutput22"}],"translated_statement":null,"sample_group":[],"show_order":[],"formal_statement":"**Definitions**  \nLet $ S = s_1 s_2 \\dots s_{27} $ be a string of length 27, where each $ s_i \\in \\{E, I, L\\} $, and $ s_1 = s_{27} = E $.  \n\n**Constraints**  \n1. $ s_1 = E $, $ s_{27} = E $.  \n2. Each $ s_i \\in \\{E, I, L\\} $ for $ i \\in \\{1, \\dots, 27\\} $.  \n\n**Objective**  \nDetermine whether $ S $ represents a solvable Worm Puzzle.  \nThe puzzle is solvable if and only if the sequence of piece types corresponds to a valid Hamiltonian path through the $ 3 \\times 3 \\times 3 $ grid, where:  \n- $ E $: end piece (degree 1 in the path),  \n- $ I $: straight piece (degree 2, linear continuation),  \n- $ L $: corner piece (degree 2, 90° turn).  \n\nThe path must start and end at an $ E $, and the number of $ L $-type pieces must be exactly 12.  \n\n$$\n\\text{Answer} = \n\\begin{cases}\n\\text{YES} & \\text{if } \\# \\{ i \\mid s_i = L \\} = 12 \\\\\n\\text{NO} & \\text{otherwise}\n\\end{cases}\n$$","simple_statement":"Given a string of 27 characters, each being 'E', 'I', or 'L', representing the types of cubic pieces in a worm puzzle, where the first and last are always 'E', determine if the puzzle can be assembled into a 3x3x3 cube. Print \"YES\" if solvable, \"NO\" otherwise.","has_page_source":false}