program x;

var n,m,br,i,j,t:integer;
    l:array[1..1000,1..1000] of char;

begin
    readln(n,m);
    br:=1;
    for i:=1 to n do begin
        for j:=1 to m do
            read(l[i,j]);
        readln;
    end;
    while br>0 do begin
        br:=0;
        for i:=1 to n do
            for j:=1 to m do begin
                if l[i,j]='1' then l[i,j]:='X';
                if l[i,j]='2' then l[i,j]:='S';
            end;
        for i:=1 to n do
            for j:=1 to m do
                if l[i,j]='X' then begin
                    if l[i+1,j]='.' then l[i+1,j]:='1';
                    if l[i-1,j]='.' then l[i-1,j]:='1';
                    if l[i,j+1]='.' then l[i,j+1]:='1';
                    if l[i,j-1]='.' then l[i,j-1]:='1';
                    l[i,j]:='x';
                end;
        for i:=1 to n do
            for j:=1 to m do
                if l[i,j]='S' then begin
                    if l[i+1,j]='.' then l[i+1,j]:='2';
                    if l[i-1,j]='.' then l[i-1,j]:='2';
                    if l[i,j+1]='.' then l[i,j+1]:='2';
                    if l[i,j-1]='.' then l[i,j-1]:='2';
                    l[i,j]:='s';
                end;
        for i:=1 to n do
            for j:=1 to m do
                if (l[i,j]='S') or (l[i,j]='X') or (l[i,j]='1') or (l[i,j]='2') then br:=br+1;
    end;
    br:=0;
    for i:=1 to n do
        for j:=1 to m do
            if l[i,j]='s' then br:=br+1;
    write(br);
end.