#include <bits/stdc++.h>
#include <string>
#include <vector>
#include <algorithm>
#define rep(i,n) for (int i = 0;i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
int INF = 1e9;

int dist[1005][1005];
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};

int main(){
    int h,w;cin >> h >> w;
    queue<int> q;
    rep(i,h)rep(j,w){
        char ch;cin >> ch;
        if(ch == '#'){
            dist[i][j]=0;
            q.push(w*i+j);
        }else dist[i][j] = -1;
    }

    while(q.size()){
        int ind = q.front();
        q.pop();
        int y = ind/w;
        int x = ind%w;
        for(int i = 0;i < 4;i++){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny < 0 || nx < 0 || h <= ny || w <= nx) continue;
            if(dist[ny][nx]!=-1) continue;
            dist[ny][nx] = dist[y][x] + 1;
            q.push(w*ny+nx);
        }
    }
    int ans = 0;
    rep(i,h){
        rep(j,w){
            ans = max(ans,dist[i][j]);
        }
    }
    cout << ans << endl;


}
#include <bits/stdc++.h>
using namespace std;
long long M=1000000007;
vector<int> num;
int isprime[105];
int n,m,d;
string a,b;
long long dp[10005][2][2][105];
long long solve(int idx,int flag,int flag2,int rem)
{
    //cout<<rem<<endl;
    if(idx==b.size())
        {
            if(rem==0)
                return 1;
            else
                return 0;
        }
    if(dp[idx][flag][flag2][rem]!=-1)
        return dp[idx][flag][flag2][rem];

    int lim=flag?9:(b[idx]-'0');
    int lim1=flag2?0:(a[idx]-'0');
    long long ans=0;
    for(int i=lim1;i<=lim;i++)
    {
        ans=(ans+solve(idx+1,flag||(i<lim),flag2||(i>lim1),(rem+i)%m)%M)%M;

    }
    return dp[idx][flag][flag2][rem]=ans%M;
}

 
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//ios_base& scientific (ios_base& str);
{
    memset(dp,-1,sizeof(dp));
    a="1";
    cin>>b;
    cin>>m;
    n=b.size();
    for(int i=0;i<n-1;i++)
        a="0"+a;
    cout<<solve(0,0,0,0)<<endl;
 
 
    
}
 
return 0;
} 