import sys, time, random from collections import deque, Counter, defaultdict input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 61 - 1 mod = 998244353 h, w = mi() s = [input() for _ in range(h)] def check(start, l): reachable = [[False] * w for _ in range(h)] visit = [[False] * w for _ in range(h)] reachable[0][start] = True visit[0][start] = True for i in range(h - 1): wall = deque() bwall = -1 for j in range(w): if s[i][j] == '#': wall.append(j) wall.append(w) for j in range(w): if s[i][j] == '#': bwall = j wall.popleft() if not reachable[i][j]: continue if s[i + 1][j] == '.': reachable[i + 1][j] = True for k in range(max(0, bwall + 1, j - l), j + 1): if visit[i][k]: break visit[i][k] = True if s[i + 1][k] == '.': reachable[i + 1][k] = True for k in range(j + 1, min(w, wall[0], j + l + 1)): if visit[i][k]: break visit[i][k] = True if s[i + 1][k] == '.': reachable[i + 1][k] = True if any(reachable[-1]): return True return False for i in range(w): ok = w + 1 ng = -1 while abs(ok - ng) > 1: mid = (ok + ng) // 2 if check(i, mid): ok = mid else: ng = mid print(ok)