#include <iostream>
#include <cstdio>
#include <climits>
#include <algorithm>
#include <math.h>
using namespace std;
int main(){
    int h,w;
    cin>>h>>w;
    // scanf("%d",&h);
    // scanf("%d",&w);
    char paper[52][52]={};
    // string asas;
    // getline(&asas);
    getchar();
    // getchar();
    // getchar();
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            //cin>>paper[j][i];
            paper[j][i]=getchar();
            // scanf("%c",paper[j][i]);
            // printf("-%c-",paper[j][i]);
        }
        getchar();
    }
    double tem,ans=999999999999.9;

    for(int k=1;k<=w;k++){
        tem=0;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                if(paper[j][i]!='0'){
                    tem+=sqrt((k-j)*(k-j)+(i-0)*(i-0));
                }
            }
        }
        ans=min(ans,tem);
    }
    for(int k=1;k<=w;k++){
        tem=0;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                if(paper[j][i]!='0'){
                    tem+=sqrt((k-j)*(k-j)+(i-(h+1))*(i-(h+1)));
                }
            }
        }
        ans=min(ans,tem);
    }
    for(int k=0;k<=w;k++){
        tem=0;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                if(paper[j][i]!='0'){
                    tem+=sqrt((0-j)*(0-j)+(i-k)*(i-k));
                }
            }
        }
        ans=min(ans,tem);
    }
    for(int k=0;k<=w;k++){
        tem=0;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                if(paper[j][i]!='0'){
                    tem+=sqrt(((w+1)-j)*((w+1)-j)+(i-k)*(i-k));
                }
            }
        }
        ans=min(ans,tem);
    }

    // cout<<ans<<endl;;
    printf("%f",ans);
    return 0;
}