## An Animal Contest 2 P3 - Koala Balloons

View as PDF

Points: 12 (partial)
Time limit: 0.75s
Java 2.5s
Python 3.0s
Memory limit: 512M

Authors:
Problem types

To further celebrate his birthday, Sanjay is at Koala Land with his best friend, Sam. While Sam is on a scary-looking roller coaster that Sanjay didn't want to go on, Sanjay decides to buy him balloons!

Imagine Koala Land as an by grid. The balloon store is situated at the top left cell, , and Sam will leave the roller coaster at the bottom right cell with coordinates . Scattered throughout the grid are amusement rides that serve as obstacles.

Sanjay wants to bring Sam as many balloons as possible, but is limited by the presence of obstacles. A package of balloons takes up a square space of by . Balloons, as solid objects, can only move through spaces that are not occupied by obstacles. In other words, if Sanjay takes balloons, he can only move through spaces of minimum size by . At any cell, Sanjay is allowed to move up, down, left, or right, as long as the resulting cell is contained in the dimensions of the grid. Given this constraint, what's the maximum number of balloons that Sanjay can bring Sam?

Note that if Sanjay starts with balloons, he will initially occupy the square defined by the coordinates , , , and . Only the bottom right corner of Sanjay's group of balloons must reach for him to reach Sam. Additionally, cells and will never be blocked.

For this problem, Python users are recommended to use PyPy over CPython.

#### Input Specification

The input will contain lines of characters, representing the grid as described in the statement.

An obstacle is represented by X and an empty space is represented by O.

#### Output Specification

Output the maximum number of balloons that Sanjay can bring Sam.

If Sanjay cannot bring Sam any balloons, output .

#### Sample Input 1

4 4
OOOX
OOOX
XOOO
XOOO

#### Sample Output 1

2

#### Explanation for Sample 1

If Sanjay takes two balloons, he initially occupies the square defined by the coordinates, , , , and . If he moves right, down, down, and finally right, he can reach his destination.

#### Sample Input 2

4 5
OOOXX
OOOOO
OOOOO
OOOOO

#### Sample Output 2

3