#include <stdbool.h>
#include <stdio.h>
#include <algorithm>

void unos(int h, int w, bool moze[]) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      do {
        c = getchar();
      } while (c != '.' && c != '#');
      moze[w * i + j] = c == '.';
    }
  }
}
void ispis(int h, int w, bool moze[]) {
  printf("\n");

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c = moze[w * i + j] ? '.' : '#';
      printf("%c", c);
    }
    printf("\n");
  }
}

int manji(int a, int b) {
  if (a == 0) return b;
  return (a < b) ? a : b;
}

int shah(bool moze[], int x, int y, int h, int w, int vreme, int figura) {
  int min = 0;
  if (x == h - 1 && y == w - 1) {
    return vreme;
  }
  moze[x * w + y] = false;

  // kralj
  if (h > x + 1 && w > y + 1 && moze[(x + 1) * w + (y + 1)])
    if (figura == 0)
      min = manji(min, shah(moze, x + 1, y + 1, h, w, vreme + 1, 0));
    else
      min = manji(min, shah(moze, x + 1, y + 1, h, w, vreme + 2, 0));
  if (h > x + 1 && moze[(x + 1) * w + y])
    if (figura == 0)
      min = manji(min, shah(moze, x + 1, y, h, w, vreme + 1, 0));
    else
      min = manji(min, shah(moze, x + 1, y, h, w, vreme + 2, 0));
  if (w > y + 1 && moze[x * w + (y + 1)])
    if (figura == 0)
      min = manji(min, shah(moze, x, y + 1, h, w, vreme + 1, 0));
    else
      min = manji(min, shah(moze, x, y + 1, h, w, vreme + 2, 0));

  // lovac
  int i = 2;
  while (x + i < h && y + i < w && moze[(x + i - 1) * w + (y + i - 1)]) {
    i++;
  }
  if (x + i - 1 < h && y + i - 1 < w && moze[(x + i - 1) * w + (y + i - 1)]) {
    if (figura == 1)
      min = manji(min, shah(moze, x + i - 1, y + i - 1, h, w, vreme + 1, 1));
    else
      min = manji(min, shah(moze, x + i - 1, y + i - 1, h, w, vreme + 3, 1));
  }

  // top
  i = 2;
  while (x + i < h && moze[(x + i - 1) * w + y]) {
    i++;
  }
  if (x + i - 1 < h && moze[(x + i - 1) * w + y]) {
    if (figura == 2)
      min = manji(min, shah(moze, x + i - 1, y, h, w, vreme + 1, 2));
    else
      min = manji(min, shah(moze, x + i - 1, y, h, w, vreme + 4, 2));
  }
  i = 2;
  while (y + i < w && moze[x * w + (y + i - 1)]) {
    i++;
  }
  if (y + i - 1 < w && moze[x * w + (y + i - 1)]) {
    if (figura == 2)
      min = manji(min, shah(moze, x, y + i - 1, h, w, vreme + 1, 2));
    else
      min = manji(min, shah(moze, x, y + i - 1, h, w, vreme + 4, 2));
  }

  // konj
  if (x + 1 < h && y + 2 < w && moze[(x + 1) * w + y + 2]) {
    if (figura == 3)
      min = manji(min, shah(moze, x + 1, y + 2, h, w, vreme + 1, 3));
    else
      min = manji(min, shah(moze, x + 1, y + 2, h, w, vreme + 5, 3));
  }

  if (x + 2 < h && y + 1 < w && moze[(x + 2) * w + y + 1]) {
    if (figura == 3)
      min = manji(min, shah(moze, x + 2, y + 1, h, w, vreme + 1, 3));
    else
      min = manji(min, shah(moze, x + 2, y + 1, h, w, vreme + 5, 3));
  }

  // kraljica
  while (x + i < h && y + i < w && moze[(x + i - 1) * w + (y + i - 1)]) {
    i++;
  }
  if (x + i - 1 < h && y + i - 1 < w && moze[(x + i - 1) * w + (y + i - 1)]) {
    if (figura == 4)
      min = manji(min, shah(moze, x + i - 1, y + i - 1, h, w, vreme + 1, 1));
    else
      min = manji(min, shah(moze, x + i - 1, y + i - 1, h, w, vreme + 6, 1));
  }
  i = 2;
  while (x + i < h && moze[(x + i - 1) * w + y]) {
    i++;
  }
  if (x + i - 1 < h && moze[(x + i - 1) * w + y]) {
    if (figura == 4)
      min = manji(min, shah(moze, x + i - 1, y, h, w, vreme + 1, 2));
    else
      min = manji(min, shah(moze, x + i - 1, y, h, w, vreme + 6, 2));
  }
  i = 2;
  while (y + i < w && moze[x * w + (y + i - 1)]) {
    i++;
  }
  if (y + i - 1 < w && moze[x * w + (y + i - 1)]) {
    if (figura == 4)
      min = manji(min, shah(moze, x, y + i - 1, h, w, vreme + 1, 2));
    else
      min = manji(min, shah(moze, x, y + i - 1, h, w, vreme + 6, 2));
  }

  return min;
}

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  bool moze[h * w];
  unos(h, w, moze);
  printf("%d", shah(moze, 0, 0, h, w, 0, 0));
  return 0;
}