cho.sh
Notes
Loading...

Paper Racing

Time limit

2s

Memory limit

128 MB

Problem

Jonghyuk and Jaehong play paper racing. The track is drawn on paper divided into square cells. Each cell is either road or an obstacle, and two road cells are marked as the start and the finish. The goal is to start the car at the start cell and reach the finish as quickly as possible.

The car is represented by a single point. Movement happens in turns. At the beginning of each turn, the car is at the center of some cell. During that turn, it moves to the center of another cell. The car's velocity is a vector with integer coordinates. Each turn proceeds as follows.

  1. Just before the turn begins, each coordinate of the velocity may be increased by 1, decreased by 1, or left unchanged. The two coordinates are adjusted independently.
  2. The car moves. If the car is at (r, c) and its velocity is (vr, vc), its new coordinate is (r + vr, c + vc). The car follows the straight line segment from the center of the previous cell to the center of the new cell.

If the movement path passes through the road cell marked as the finish, the game ends immediately, and the number of turns taken so far is the time. If the path passes through an obstacle before the game ends, the car is destroyed and cannot finish. Merely grazing an obstacle is allowed.

The car may not leave the paper; doing so is a disqualification.

Even if the destination of a move is an obstacle or outside the paper, the move succeeds if the path passes through the finish before that happens.

Given the shape of the paper and the initial velocity vr, vc, find the minimum number of turns needed to finish the game. If reaching the finish is impossible, print -1.

Input

The first line contains N and M. The next N lines describe the paper. . is road, S is the start, F is the finish, and X is an obstacle. The last line contains vr and vc.

N and M are positive integers at most 50, and vr and vc are integers whose absolute values are at most 50. The paper contains exactly one S and exactly one F.

Output

Print the answer on the first line.