#include <iostream>

using namespace std;

int main()
{
    int h, w;
    char a, b, c;
    cin>>h>>w;
    if(h==1 and w==1) cout<<0;
    else if(h==2 and w==2) cout<<1;
    else if(h*w==2) cout<<1;
    else if(h*w==3){
        cin>>a>>b>>c;
        if(b=='#') cout<<-1;
        else cout<<2;
    }
    else cout<<-1;
    return 0;
}