#P3848. Maximum Jump Distance on a Grid

    ID: 17096 Type: Default 1000ms 256MiB

Maximum Jump Distance on a Grid

Maximum Jump Distance on a Grid

In this problem, you are given a grid (board) consisting of two types of cells: 0 cells and 1 cells. A cell with value 0 is a landing cell, and a cell with value 1 is an obstacle cell that can be jumped over. You start at a given 0 cell, and you can perform a sequence of jump moves. The rules of a jump are as follows:

  • Jump Rule: From a 0 cell, you may jump in one of the four cardinal directions (up, down, left, right). In that direction, you must move over a contiguous block of one or more 1 cells and land on the very next 0 cell. In other words, if you jump over k consecutive 1 cells, the jump's distance is defined by the formula $$d = k + 1$$ (i.e. the number of 1 cells jumped over plus one).
  • No Revisiting: Each 0 cell (including the starting point) can be landed on at most once. Moreover, you are not allowed to jump over a cell that has already been landed on.

Your task is to determine the maximum total jump distance you can achieve starting from the given starting point by making a sequence of valid jumps until no more jumps are possible.

Note: If no jump is possible at all, the total distance is 0.

inputFormat

The input begins with two integers R and C (the number of rows and columns of the grid).
Then follow R lines, each containing C integers (either 0 or 1) separated by spaces, representing the grid.
Finally, there is a line containing two integers sr and sc (0-indexed) representing the starting row and column. It is guaranteed that the cell at (sr, sc) is 0.

outputFormat

Output a single integer representing the maximum total jump distance achievable from the starting cell by performing a sequence of valid jumps.

sample

3 4
0 1 1 0
1 1 1 1
0 1 0 0
0 0
5