Hide

Problem D
Alpine Agility

Alice and Bob have spent a wonderful day hitting the slopes at their favourite ski hill. To make things more interesting they’ve come up with a game. Both start at the Northwest-most point on the hill and race to the Southeast-most point. However, they are not racing to see who can get there faster, rather they are racing to see who can build up the most speed when they get there. This isn’t a fair competition as it is since Alice has been skiing for many more years than Bob so she has decided to impose a handicap on herself. Instead of being free to move down the hill as she pleases, she can only move South, East, or Southeast. When moving South or East she will decelerate by $10$m/s and $15$m/s when moving Southeast due to friction and air resistance but she will accelerate by $1$m/s for each meter downwards she travels (for example if she moves South from a hill with height $100$m to a hill wil height $80$m she will decelerate by $10$m/s but accelerate by $20$m/s, increasing her speed by $10$m/s overall). Likewise she will decelerate by the same $1$m/s when traveling from a shorter hill to a taller hill. If her speed would be less than $0$m/s when she reaches the peak of the next hill she cannot make it to that hill.

Can you predict how much speed she can build up by the end?

Input

The first line of the input will contain two integers $n$ and $m$ ($1\leq n, m \leq 1000$) the number of rows and columns respectively in the map. The following $n$ lines will contain $m$ integers $h_{i,j}$ ($0 \leq h_{i,j} \leq 10^9$) representing the heights of the hills in meters.

Output

Output a single integer $v$ the fastest speed that Alice can reach at the bottom of the hill or "TOO SLOW" if she cannot reach the bottom while following her rule.

Sample Input 1 Sample Output 1
3 3
50 40 30
40 30 20
30 20 10
10
Sample Input 2 Sample Output 2
3 3
50 40 30
50 50 20
30 20 10
5
Sample Input 3 Sample Output 3
3 3
50 40 30
50 50 20
30 20 20
TOO SLOW

Please log in to submit a solution to this problem

Log in