{$Q+,R+,S+}
{$M 16384,0,655360}
program objektu_minavimas;
  const maxn = 100;
        maxm = 100;
        maxq = 5000;
        kryptys4 : array [1..4, 1..2] of integer =
          ((1, 0), (0, -1), (-1, 0), (0, 1));
        kryptys8 : array [1..8, 1..2] of integer =
          ((1, 0), (1, -1), (0, -1), (-1, -1),
           (-1, 0), (-1, 1), (0, 1), (1, 1));
  type laukas = array [1..maxn, 1..maxm] of byte;
       atstumai = array [1..maxn, 1..maxn] of integer;
       {objektu vadinsime tik minuojama objekta, ir kartais minas}
       objektas = record
                    n, m : shortint;
                  end;
       objektai = array [1..maxn * maxm div 3] of objektas;
       minos = array [1..(maxn + maxm) * 2] of objektas;
       iskilusis = array [1..(maxn + maxm) * 2] of integer;
       patikrintos = array [1..(maxn + maxm) * 2] of boolean;
       fifo = array [1..maxq] of objektas;
  var f : text;
      i, ii, j, jj : integer;
      c : char;
      n, m : integer;
      l : laukas;
      atst : atstumai;
      o : objektai;
      ml : minos;
      kiek_o : integer;
      kiek_ml : integer;
      isk : iskilusis;
      p : patikrintos;
      eile : fifo;
      ats : integer;
  procedure surasti_ml (var o : objektai; kiek_o : integer;
                        var ml : minos; var kiek_ml : integer);
  {suranda ypatingas minas pagal tai, ar nubrezus atitinkama tiese
  visi minuojami ojektai lieka vienoje tieses puseje, o ypatingoji
  mina - kitoje}
    var i, ii : integer;
        m : objektas;
    function krastine (m : objektas; kryptis : integer) : boolean;
    {patikrina, ar ojektai yra atitinkamoje tieses puseje}
      var i, skirtumas, suma : integer;
    begin
      krastine := true;
      skirtumas := m.n - m.m;
      suma := m.n + m.m;
      case kryptis of
        1 : for i := 1 to kiek_o do
              if o[i].m >= m.m
                then
                  begin
                    krastine := false;
                    break;
                  end;
        2 : for i := 1 to kiek_o do
              if o[i].m >= o[i].n - skirtumas
                then
                  begin
                    krastine := false;
                    break;
                  end;
        3 : for i := 1 to kiek_o do
              if o[i].n <= m.n
                then
                  begin
                    krastine := false;
                    break;
                  end;
        4 : for i := 1 to kiek_o do
              if o[i].m <= suma - o[i].n
                then
                  begin
                    krastine := false;
                    break;
                  end;
        5 : for i := 1 to kiek_o do
              if o[i].m <= m.m
                then
                  begin
                    krastine := false;
                    break;
                  end;
        6 : for i := 1 to kiek_o do
              if o[i].m <= o[i].n - skirtumas
                then
                  begin
                    krastine := false;
                    break;
                  end;
        7 : for i := 1 to kiek_o do
              if o[i].n >= m.n
                then
                  begin
                    krastine := false;
                    break;
                  end;
        8 : for i := 1 to kiek_o do
              if o[i].m >= suma - o[i].n
                then
                  begin
                    krastine := false;
                    break;
                  end;
      end;
    end;
  begin
   kiek_ml := 0;
   for i := 1 to kiek_o do
     for ii := 1 to 8 do
       begin
         m.n := o[i].n + kryptys8[ii, 2];
         m.m := o[i].m + kryptys8[ii, 1];
         if krastine (m, ii)
           then
             begin
               inc (kiek_ml);
               ml[kiek_ml] := m;
             end;
       end;
  end;
  procedure surasti_iskiluji (var ml : minos; var kiek_ml : integer;
                              var isk : iskilusis);
  {surusiuoja ypatingasias minas, kad jas sujungus paeiliui trumpiausiomis
  atkarpomis gautume iskiluji daugiakampi}
    const epsilon = 1e-9;
    var i, ii : integer;
        kiek, pask, min_y : integer;
        kampas, naujas : real;
  begin
    min_y := 1;
    for i := 1 to kiek_ml do
      if (ml[i].n < ml[min_y].n) or (ml[i].n = ml[min_y].n) and (ml[i].m < ml[min_y].m)
        then min_y := i;
    ii := 1;
    for i := 1 to kiek_ml do
      if (ml[i].n > ml[ii].n) or (ml[i].n = ml[ii].n) and (ml[i].m > ml[ii].m)
        then ii := i;
    fillchar (p, sizeof(p), 0);
    kiek := 0;
    repeat
    {ieskome desiniojo iskilojo daugiakampio sono (maziausi kampai)}
      inc (kiek);
      isk[kiek] := ii;
      p[ii] := true;
      pask := ii;
      kampas := 3 * pi;
      for i := 1 to kiek_ml do
        if not p[i]
          then
            begin
              if (ml[pask].m = ml[i].m)
                then naujas := pi / 2
                else naujas := arctan ((ml[i].n - ml[pask].n) / (ml[pask].m - ml[i].m));
              if (ml[i].m < ml[pask].m)
                then naujas := pi + naujas
                else if (ml[i].n > ml[pask].n)
                  then naujas := 2 * pi - naujas;
              if (naujas > epsilon) and (naujas <= pi)
                then if naujas < kampas
                  then
                    begin
                      kampas := naujas;
                      ii := i;
                    end
                  else if (abs (naujas - kampas) < epsilon) and
                          (sqr(ml[pask].n - ml[i].n) +
                           sqr(ml[pask].m - ml[i].m) <
                           sqr(ml[pask].n - ml[ii].n) +
                           sqr(ml[pask].m - ml[ii].m))
                    then
                      begin
                        kampas := naujas;
                        ii := i;
                      end;
            end;
    until ii = min_y;
    isk[kiek + 1] := ii;
    ii := isk[1];
    kiek := kiek_ml + 2;
    repeat
    {ieskome kairiojo iskilojo daugiakampio sono (didziausi kampai)}
      dec (kiek);
      isk[kiek] := ii;
      p[ii] := true;
      pask := ii;
      kampas := -pi;
      for i := 1 to kiek_ml do
        if not p[i]
          then
            begin
              if (ml[pask].m = ml[i].m)
                then naujas := pi / 2
                else naujas := arctan ((ml[i].n - ml[pask].n) / (ml[pask].m - ml[i].m));
              if (ml[i].m < ml[pask].m)
                then naujas := pi + naujas
                else if (ml[i].n > ml[pask].n)
                  then naujas := 2 * pi - naujas;
              if naujas <= pi
                then if naujas > kampas
                  then
                    begin
                      kampas := naujas;
                      ii := i;
                    end
                  else if (abs (naujas - kampas) < epsilon) and
                          (sqr(ml[pask].n - ml[i].n) +
                           sqr(ml[pask].m - ml[i].m) <
                           sqr(ml[pask].n - ml[ii].n) +
                           sqr(ml[pask].m - ml[ii].m))
                    then
                      begin
                        kampas := naujas;
                        ii := i;
                      end;
            end;
    until ii = min_y;
  end;
  function banga (nuo, iki : integer; var l : atstumai) : integer;
  {iesko trumpiausio kelio tarp atitinkamu ypatinguju minu}
    var head, tail : integer;
        i : integer;
        nauja : objektas;
    procedure itraukti (i, ii : shortint);
    begin
      if head = maxq
        then head := 1
        else inc (head);
      eile[head].n := i;
      eile[head].m := ii;
    end;
    procedure pasalinti;
    begin
      if tail = maxq
        then tail := 1
        else inc (tail);
    end;
  begin
    l[ml[nuo].n, ml[nuo].m] := 1;
    head := 0;
    tail := 0;
    itraukti (ml[nuo].n, ml[nuo].m);
    while (head <> tail) and (l[ml[iki].n, ml[iki].m] = 0) do
      begin
        pasalinti;
        for i := 1 to 4 do
          begin
            nauja.n := eile[tail].n + kryptys4[i, 1];
            nauja.m := eile[tail].m + kryptys4[i, 2];
            if (nauja.n >= 1) and (nauja.n <= n) and
               (nauja.m >= 1) and (nauja.m <= m) and (l[nauja.n, nauja.m] = 0)
              then
                begin
                  l[nauja.n, nauja.m] := l[eile[tail].n, eile[tail].m] + 1;
                  itraukti (nauja.n, nauja.m);
                end;
          end;
      end;
    banga := l[ml[iki].n, ml[iki].m] - 2;
  end;
begin
  assign (f, 'minos.dat');
  reset (f);
  readln (f, n, m);
  kiek_o := 0;
  for i := 1 to n do
    begin
      for ii := 1 to m do
        begin
          read (f, c);
          if c = 'X'
            then
              begin
                inc (kiek_o);
                o[kiek_o].n := i;
                o[kiek_o].m := ii;
                l[i, ii] := 1;
              end
            else if c = '1'
              then l[i, ii] := 1
              else l[i, ii] := 0;
        end;
      readln (f);
    end;
  close (f);

  surasti_ml (o, kiek_o, ml, kiek_ml);
  surasti_iskiluji (ml, kiek_ml, isk);

  {pasalinam nulius, jei netycia ne visos ypatingosios minos ieina
  i iskiluji daugiakampi}
  i := 1;
  while i <= kiek_ml do
    if isk[i] = 0
      then
        begin
          dec (kiek_ml);
          for ii := i to kiek_ml do
            isk[ii] := isk[ii + 1];
        end
      else inc (i);
  {pasalinam vidurinyji is bet kuriu triju tasku, jei jie yra vienoje tieseje}
  i := 1;
  while i < kiek_ml - 1 do
    if ((ml[isk[i]].n = ml[isk[i + 1]].n) and
        (ml[isk[i + 1]].n = ml[isk[i + 2]].n)) or
       ((ml[isk[i]].m = ml[isk[i + 1]].m) and
        (ml[isk[i + 1]].m = ml[isk[i + 2]].m))
      then
        begin
          dec (kiek_ml);
          for ii := i + 1 to kiek_ml do
            isk[ii] := isk[ii + 1];
        end
      else inc (i);
  if ((ml[isk[kiek_ml - 1]].n = ml[isk[kiek_ml]].n) and
      (ml[isk[kiek_ml]].n = ml[isk[1]].n)) or
     ((ml[isk[kiek_ml - 1]].m = ml[isk[kiek_ml]].m) and
      (ml[isk[kiek_ml]].m = ml[isk[1]].m))
    then dec (kiek_ml);
  isk[kiek_ml + 1] := isk[1];

  {sumuojame surastus atstumus tarp ypatinguju minu}
  ats := 0;
  for i := 1 to kiek_ml do
    begin
      for j := 1 to n do
        for jj := 1 to m do
          atst[j, jj] := l[j, jj];
      ats := ats + banga (isk[i], isk[i + 1], atst);
    end;

  assign (f, 'minos.rez');
  rewrite (f);
  write (f, ats + kiek_ml);
  close (f);
end.
