PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000421. Thoát khỏi đám cháy lan rộng (Escape the Spreading Fire)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Thoát khỏi đám cháy lan rộng (Escape the Spreading Fire)

Escape the Spreading Fire

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

A field has mm rows and nn columns, both numbered from 00. A cell value of 00 means grass, 11 means a cell initially on fire, and 22 means a wall. Adjacency requires a shared edge.

You start at (0,0)(0,0) and must reach the safehouse at (m−1,n−1)(m-1,n-1). Before departure you may wait a nonnegative integer number of minutes at the starting cell. Afterwards, each minute you may move to an adjacent grassy cell that is not yet burning. Neither you nor fire can pass through walls.

In each minute your waiting or movement occurs first. Then fire simultaneously spreads from all burning cells into adjacent non-wall cells. Burning cells remain on fire. You are unsafe if fire reaches your current cell, except when you have just entered the safehouse in that same minute: this counts as a successful escape.

Determine the largest possible waiting time at the starting cell that still allows a safe escape.

Input

The first line contains integers m,nm,n, the number of rows and columns.

Each of the next mm lines contains exactly nn integers ai,ja_{i,j}, describing row ii from column 00 through column n−1n-1 (0≤i<m0\le i<m).

Output

Print one integer, the maximum waiting time; print -1 if immediate departure cannot succeed, or 1000000000 if arbitrarily long waiting is possible.

Subtasks

  • Subtask 1 (20 points): 2≤m,n≤3002\le m,n\le300; 4≤mn≤364\le mn\le 36; ai,j∈{0,1,2}a_{i,j}\in\{0,1,2\} for all 0≤i<m,0≤j<n0\le i<m,0\le j<n; a0,0=am−1,n−1=0a_{0,0}=a_{m-1,n-1}=0.
  • Subtask 2 (30 points): 2≤m,n≤3002\le m,n\le300; 4≤mn≤4004\le mn\le 400; ai,j∈{0,1,2}a_{i,j}\in\{0,1,2\} for all 0≤i<m,0≤j<n0\le i<m,0\le j<n; a0,0=am−1,n−1=0a_{0,0}=a_{m-1,n-1}=0.
  • Subtask 3 (50 points): 2≤m,n≤3002\le m,n\le300; 4≤mn≤200004\le mn\le 20000; ai,j∈{0,1,2}a_{i,j}\in\{0,1,2\} for all 0≤i<m,0≤j<n0\le i<m,0\le j<n; a0,0=am−1,n−1=0a_{0,0}=a_{m-1,n-1}=0.

Examples

Input

5 7
0 2 0 0 0 0 0
0 0 0 2 2 1 0
0 2 0 0 1 2 0
0 0 2 2 2 0 2
0 0 0 0 0 0 0

Output

3

Explanation

Wait three minutes, move down four times and right six times. Reach (4,5) in minute 12, one minute before fire, then enter (4,6) in minute 13. Fire reaches home in minute 14. Its sole grassy neighbor is (4,5), at least nine steps from the start. Waiting four minutes means reaching it no earlier than minute 13, when it burns. Print 3.

Input

3 4
0 0 0 0
0 1 2 0
0 2 0 0

Output

-1

Explanation

Fire starts at (1,1). At the end of minute one, both (0,1) and (1,0) burn. These are the only possible first steps from (0,0), and neither remains safe. Print -1.

Input

3 3
0 0 0
2 2 0
1 2 0

Output

1000000000

Explanation

Fire at (2,0) is completely enclosed by walls at (1,0) and (2,1). The route (0,0), (0,1), (0,2), (1,2), (2,2) never burns. You may wait indefinitely and then take four steps. Print 1000000000.