{"problem":{"name":"Ex - Builder Takahashi (Enhanced version)","description":{"content":"There is a grid with $H \\times W$ squares. Let $(i,j)$ denote the square at the $i$\\-th row from the top and $j$\\-th column from the left.   $C_{i,j}$ represents the state of each square. Each square ","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":4000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"abc243_h"},"statements":[{"statement_type":"Markdown","content":"There is a grid with $H \\times W$ squares. Let $(i,j)$ denote the square at the $i$\\-th row from the top and $j$\\-th column from the left.  \n$C_{i,j}$ represents the state of each square. Each square is in one of the following four states.\n\n*   `S`: The starting point. The grid has exactly one starting point.\n*   `G`: The goal. The grid has exactly one goal.\n*   `.`: A constructible square, where a wall can be built.\n*   `O`: An unconstructible square, where a wall cannot be built.\n\nAoki intends to start at the starting point and get to the goal. When he is at $(i,j)$, he can go to $(i+1,j)$, $(i,j+1)$, $(i-1,j)$, or $(i,j-1)$. It is not allowed to exit the grid or enter a square with a wall.\nTakahashi decides to build a wall on one or more constructible squares of his choice before Aoki starts so that there is no way for Aoki to reach the goal. Here, the starting point and the goal cannot be chosen.\nIs it possible for Takahashi to build walls to prevent Aoki from reaching the goal? If it is possible, also compute the two values below:\n\n*   the minimum number $n$ of walls needed to prevent Aoki from reaching the goal, and\n*   the number of ways modulo $998244353$, $r$, to achieve that minimum number of walls.\n\n## Constraints\n\n*   $2 \\leq H \\leq 100$\n*   $2 \\leq W \\leq 100$\n*   $C_{i,j}$ is `S`, `G`, `.`, or `O`.\n*   Each of `S` and `G` appears exactly once in $C_{i,j}$.\n\n## Input\n\nInput is given from Standard Input in the following format:\n\n$H$ $W$\n$C_{1,1}$$C_{1,2}$$\\dots$$C_{1,W}$\n$C_{2,1}$$C_{2,2}$$\\dots$$C_{2,W}$\n$\\vdots$\n$C_{H,1}$$C_{H,2}$$\\dots$$C_{H,W}$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"abc243_h","tags":[],"sample_group":[["4 3\nS..\nO..\n..O\n..G","Yes\n3 6\n\nLet `#` represent a square to build a wall on. The six ways to achieve the minimum number of walls are as follows:\n\nS#.  S.#  S..  S..  S..  S..\nO#.  O#.  O##  O.#  O.#  O.#\n#.O  #.O  #.O  ##O  .#O  .#O\n..G  ..G  ..G  ..G  #.G  .#G"],["3 2\n.G\n.O\n.S","No\n\nRegardless of how Takahashi builds walls, Aoki can always reach the goal."],["2 2\nS.\n.G","Yes\n2 1"],["10 10\nOOO...OOO.\n.....OOO.O\nOOO.OO.OOO\nOOO..O..S.\n....O.O.O.\n.OO.O.OOOO\n..OOOG.O.O\n.O.O..OOOO\n.O.O.OO...\n...O..O..O","Yes\n10 12"]],"created_at":"2026-03-03 11:01:14"}}