Under construction

Still being built. Things may change

Run-up Jump

Time limit2sMemory limit1024 MB

Summary
Given a grid with traps, decide whether repeated run-up and long jump moves can carry the start cell S to the end cell E.
Level

Medium6 of 10

Topics
Graph, BFS, Simulation
Solved
No attempts yet

Problem

A grid has NN rows and MM columns. Each cell is either an empty cell or a trap. On this grid, a jump consists of a run-up followed by a long jump, in that order.

  1. (Run-up) Choose an integer x≥1x \ge 1, pick one of the four directions (up, down, left, right) from the current cell, and move one cell at a time xx times in that direction. The moves must not enter a trap cell or leave the grid.
  2. (Long jump) Then choose an integer yy with 0≤y≤x+10 \le y \le x + 1, and jump yy cells in the same direction. The landing cell must not be a trap and must lie inside the grid.

Starting from the start cell, determine whether you can reach the end cell after any number of jumps.

Input

The first line contains the number of test cases TT (1≤T≤10 0001 \le T \le 10\,000).

Each test case follows.

The first line contains two integers NN and MM (1≤N,M≤2 0001 \le N, M \le 2\,000).

Each of the next NN lines contains a string of length MM that describes one row of the grid. Each character is one of ., #, S, or E, with these meanings:

  • .: empty cell
  • #: trap
  • S: start cell
  • E: end cell

Each test case contains exactly one S and exactly one E.

The sum of N×MN \times M over all test cases is at most 4 000 0004\,000\,000.

Output

For each test case, print one line. Print YES if you can reach the cell marked E, and NO otherwise.

Examples1

  1. Example 1

    Input
    3
    4 5
    S..#.
    ####.
    .E###
    .#...
    2 3
    S#.
    .#E
    3 3
    S..
    .#.
    ..E
    
    Expected output
    YES
    NO
    YES