labudovi | 프로그래밍의 벗 PivotOJ
PivotOJ

labudovi

시간 제한: 1000ms메모리 제한: 256MB출처: CHC 2005 National Competition #2 - SeniorsBOJ 3197

문제

Two swans are living on a lake but they are separated with ice that covers parts of the lake. 

Lake is rectangular in shape and consists of squares arranged in R rows and C columns. Some squares are covered with ice. 

Lake is gradually defrosting – in one day all of the squares covered with ice that are in touch with water melt and turn into water. We consider two squares to be in touch if they are neighbors horizontally or vertically (but not diagonally). 

The following figure depicts the lake from the third example: 

...XXXXXX..XX.XXX ....XXXX.......XX .....XX.......... 
....XXXXXXXXX.XXX .....XXXX..X..... ......X.......... 
...XXXXXXXXXXXX.. ....XXX..XXXX.... .....X.....X..... 
..XXXXX..XXXXXX.. ...XXX....XXXX... ....X......XX.... 
.XXXXXX..XXXXXX.. ..XXXX....XXXX... ...XX......XX.... 
XXXXXXX...XXXX... ..XXXX.....XX.... ....X............ 
..XXXXX...XXX.... ....XX.....X..... ................. 
....XXXXX.XXX.... .....XX....X..... ................. 
in the beginning   after first day   after second day 

Swans can move only on water squares in horizontal and vertical (but not diagonal) direction. 

Write a program that will calculate after how many days the swans will be able to meet each other. 

입력

First line of input contains two integers R and C, 1 ≤ R, C ≤ 1500. 

Each of the following R lines contains a sequence of C characters, the description of the lake at the beginning: '.' (dot) denotes a water square, 'X' denotes an ice-covered square, and 'L' denotes a square with a swan.

출력

First and only line of output should contain the number of days from the problem statement. 

예제

예제 1

입력
10 2
.L
..
XX
XX
XX
XX
XX
XX
..
.L
출력
3

예제 2

입력
4 11
..XXX...X..
.X.XXX...L.
....XXX..X.
X.L..XXX...
출력
2

예제 3

입력
8 17
...XXXXXX..XX.XXX
....XXXXXXXXX.XXX
...XXXXXXXXXXXX..
..XXXXX.LXXXXXX..
.XXXXXX..XXXXXX..
XXXXXXX...XXXX...
..XXXXX...XXX....
....XXXXX.XXXL...
출력
2
코드를 제출하려면 로그인하세요.