H, W = list(map(int, input().split()))

wall = "#"
road = "."
mp = [list(input()) for _ in range(H)]
mp = list(zip(*(mp[::-1])))
mp = [list(x) for x in mp]
# print(mp)

def get_mass(x, y):
  # print("get_mass", x, y, 0 < x < W, )
  if 0 <= x < W and 0 <= y < H:
    return mp[x][y]
  else:
    return wall
def set_mass(val, x, y):
  if 0 <= x < W and 0 <= y < H:
    if (mp[x][y] == road) or (type(mp[x][y]) == int and mp[x][y] > val):
      # print("set", val,(x, y))
      mp[x][y] = val

def north(x, y):
  return x, y+1
def east(x, y):
  return x+1, y
def west(x, y):
  return x, y-1
def south(x, y):
  return x-1, y

goals = []
  
def movable(now_pos, go_pos):
  now = get_mass(*now_pos)
  go = get_mass(*go_pos)
  # print("movalbe calc", now_pos, now, go_pos, go)
  if go == road:
    return True
  if type(now) == int and type(go) == int and now < go:
    # 行く先の方が値でかいなら行くべき
    return True
  return False
  
def arrived(cnt, pos):
  # 今posにいて、そこに来るまでにcnt回移動してきた。
  set_mass(cnt, *pos)
  # print("cnt", cnt, "pos", pos)
  
  if (H - 1) == pos[1] and (W - 1) == pos[0]:
	# posがゴールならcnt回をゴール回数集に足す
    # print("goal", pos)
    goals.append(cnt)  
  else:
    # posの東西南北が移動できるなら、cnt+1を渡して再帰させる
    cnt += 1
    if movable(pos, north(*pos)):
      arrived(cnt, north(*pos))
    if movable(pos, east(*pos)):
      arrived(cnt, east(*pos))
    if movable(pos, south(*pos)):
      arrived(cnt, south(*pos))
    if movable(pos, west(*pos)):
      arrived(cnt, west(*pos))

arrived(0, (0,0))
print(min(goals))
# print("goals", goals)
