#include <bits/stdc++.h>
using namespace std;

const int MAXN=2e5+50;
int seg[MAXN*4];
int v[MAXN];
int n, m, x;

void build(int sind, int ini, int fim) {
    seg[sind]=x;
    if(ini==fim) return;
    int m=(ini+fim)/2; int e=sind*2; int d=e+1;
    build(e, ini, m); build(d, m+1, fim);
}

void update(int sind, int ini, int fim, int qind, int val) {
    if(qind<ini||qind>fim) return;
    if(ini==fim) { seg[sind]+=val; return; }
    int m=(ini+fim)/2; int e=sind*2; int d=e+1;
    update(e, ini, m, qind, val); update(d, m+1, fim, qind, val);
    seg[sind]=max(seg[e], seg[d]);
}
int query(int sind, int ini, int fim, int val) {
    if(ini==fim) return ini;
    int m=(ini+fim)/2; int e=sind*2; int d=e+1;
    if(seg[e]>=val) return query(e, ini, m, val);
    else return query(d, m+1, fim, val);
}

bool testa(int k) {
    if(k==0) return 1;
    build(1, 1, m+5);
    for(int i=n-k+1; i<=n; i++) {
        int ind=query(1, 1, m+5, v[i]); //first caixa >= v[i]
        // printf("cabe na %d\n", ind);
        if(ind>m) return 0;
        update(1, 1, m+5, ind, -v[i]); //soma -v[i] na box[ind]
    }
    return 1;
}

int main() {
    scanf("%d %d %d", &n, &m, &x);
    for(int i=1; i<=n; i++) scanf("%d", &v[i]);
    int resp=0;
    // for(int i=1; i<=n; i++) printf("t %d: %d\n", i, testa(i));
    for(int i=1; i<=n; i++) if(testa(i)) resp=i;
    // for(int i=n; i>0; i/=2) while(resp+i<=n&&testa(resp+i)) resp+=i;
    assert(testa(resp));

    printf("%d\n", resp);
}


// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,sse,sse2")
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int n, m;

int id(int x, int y) {
    return x * (m + 2) + y;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m;
    char f[n + 2][m + 2];
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> f[i][j];
        }
    }

    vector<vector<int>> dis(n + 2, vector<int>(m + 2, 1e9));
    queue<pair<int, int>> q;
    for (int i = 0; i <= n + 1; ++i) {
        for (int j = 0; j <= m + 1; ++j) {
            if (f[i][j] != 'X') {
                dis[i][j] = 0;
                q.push({i, j});
            }
        }
    }
    int l = 0, r = 0;
    while (!q.empty()) {
        auto cor = q.front();
        q.pop();
        int x = cor.first;
        int y = cor.second;
        r = max(r, dis[x][y] + 1);
        for (int i = -1; i < 2; ++i) {
            for (int j = -1; j < 2; ++j) {
                if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                if (dis[x][y] + 1 < dis[x + i][y + j]) {
                    dis[x + i][y + j] = dis[x][y] + 1;
                    q.push({x + i, y + j});
                }
            }
        }
    }

    while (r - l > 1) {
        int mid = (r + l) / 2;
        vector<vector<int>> dis2(n + 2, vector<int>(m + 2, 1e9));
        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (f[x][y] == 'X' && dis[x][y] >= mid) {
                    q.push({x, y});
                    dis2[x][y] = 0;
                }
            }
        }
        int disc = 1;
        while (!q.empty()) {
            auto cor = q.front();
            q.pop();
            int x = cor.first;
            int y = cor.second;
            if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
                disc = 0;
                while (!q.empty()) q.pop();
                goto CHECK;
            }
            for (int i = -1; i < 2; ++i) {
                for (int j = -1; j < 2; ++j) {
                    if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                    if (dis2[x][y] + 1 < dis2[x + i][y + j]) {
                        dis2[x + i][y + j] = dis2[x][y] + 1;
                        q.push({x + i, y + j});
                    }
                }
            }
        }

        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
                    disc = 0;
                }
            }
        }
        CHECK:;
        if (disc) {
            l = mid;
        } else {
            r = mid;
        }
    }

    cout << l - 1 << endl;
    for (int x = 1; x <= n; ++x) {
        for (int y = 1; y <= m; ++y) {
            if (f[x][y] == 'X' && dis[x][y] >= l) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
}


#pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,sse,sse2")
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int n, m;

int id(int x, int y) {
    return x * (m + 2) + y;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m;
    char f[n + 2][m + 2];
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> f[i][j];
        }
    }

    vector<vector<int>> dis(n + 2, vector<int>(m + 2, 1e9));
    queue<pair<int, int>> q;
    for (int i = 0; i <= n + 1; ++i) {
        for (int j = 0; j <= m + 1; ++j) {
            if (f[i][j] != 'X') {
                dis[i][j] = 0;
                q.push({i, j});
            }
        }
    }
    int l = 0, r = 0;
    while (!q.empty()) {
        auto cor = q.front();
        q.pop();
        int x = cor.first;
        int y = cor.second;
        r = max(r, dis[x][y] + 1);
        for (int i = -1; i < 2; ++i) {
            for (int j = -1; j < 2; ++j) {
                if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                if (dis[x][y] + 1 < dis[x + i][y + j]) {
                    dis[x + i][y + j] = dis[x][y] + 1;
                    q.push({x + i, y + j});
                }
            }
        }
    }

    while (r - l > 1) {
        int mid = (r + l) / 2;
        vector<vector<int>> dis2(n + 2, vector<int>(m + 2, 1e9));
        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (f[x][y] == 'X' && dis[x][y] >= mid) {
                    q.push({x, y});
                    dis2[x][y] = 0;
                }
            }
        }
        int disc = 1;
        while (!q.empty()) {
            auto cor = q.front();
            q.pop();
            int x = cor.first;
            int y = cor.second;
            // if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
            //     disc = 0;
            //     while (!q.empty()) q.pop();
            //     goto CHECK;
            // }
            for (int i = -1; i < 2; ++i) {
                for (int j = -1; j < 2; ++j) {
                    if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                    if (dis2[x][y] + 1 < dis2[x + i][y + j]) {
                        dis2[x + i][y + j] = dis2[x][y] + 1;
                        q.push({x + i, y + j});
                    }
                }
            }
        }

        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
                    disc = 0;
                }
            }
        }
        CHECK:;
        if (disc) {
            l = mid;
        } else {
            r = mid;
        }
    }

    cout << l - 1 << "\n";
    for (int x = 1; x <= n; ++x) {
        for (int y = 1; y <= m; ++y) {
            if (f[x][y] == 'X' && dis[x][y] >= l) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << '\n';
    }
}


// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,sse,sse2")
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int n, m;

int id(int x, int y) {
    return x * (m + 2) + y;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m;
    char f[n + 2][m + 2];
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> f[i][j];
        }
    }

    vector<vector<int>> dis(n + 2, vector<int>(m + 2, 1e9));
    queue<pair<int, int>> q;
    for (int i = 0; i <= n + 1; ++i) {
        for (int j = 0; j <= m + 1; ++j) {
            if (f[i][j] != 'X') {
                dis[i][j] = 0;
                q.push({i, j});
            }
        }
    }
    int l = 0, r = 0;
    while (!q.empty()) {
        auto cor = q.front();
        q.pop();
        int x = cor.first;
        int y = cor.second;
        r = max(r, dis[x][y] + 1);
        for (int i = -1; i < 2; ++i) {
            for (int j = -1; j < 2; ++j) {
                if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                if (dis[x][y] + 1 < dis[x + i][y + j]) {
                    dis[x + i][y + j] = dis[x][y] + 1;
                    q.push({x + i, y + j});
                }
            }
        }
    }

    while (r - l > 1) {
        int mid = (r + l) / 2;
        vector<vector<int>> dis2(n + 2, vector<int>(m + 2, 1e9));
        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (f[x][y] == 'X' && dis[x][y] >= mid) {
                    q.push({x, y});
                    dis2[x][y] = 0;
                }
            }
        }
        while (!q.empty()) {
            auto cor = q.front();
            q.pop();
            int x = cor.first;
            int y = cor.second;
            for (int i = -1; i < 2; ++i) {
                for (int j = -1; j < 2; ++j) {
                    if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                    if (dis2[x][y] + 1 < dis2[x + i][y + j]) {
                        dis2[x + i][y + j] = dis2[x][y] + 1;
                        q.push({x + i, y + j});
                    }
                }
            }
        }
        int disc = 1;
        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
                    disc = 0;
                }
            }
        }
        if (disc) {
            l = mid;
        } else {
            r = mid;
        }
    }

    cout << l - 1 << endl;
    for (int x = 1; x <= n; ++x) {
        for (int y = 1; y <= m; ++y) {
            if (f[x][y] == 'X' && dis[x][y] >= l) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
}


// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,sse,sse2")
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int n, m;

int id(int x, int y) {
    return x * (m + 2) + y;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m;
    char f[n + 2][m + 2];
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> f[i][j];
        }
    }

    vector<vector<int>> dis(n + 2, vector<int>(m + 2, 1e9));
    queue<pair<int, int>> q;
    for (int i = 0; i <= n + 1; ++i) {
        for (int j = 0; j <= m + 1; ++j) {
            if (f[i][j] != 'X') {
                dis[i][j] = 0;
                q.push({i, j});
            }
        }
    }
    int l = 0, r = 0;
    while (!q.empty()) {
        auto cor = q.front();
        q.pop();
        int x = cor.first;
        int y = cor.second;
        r = max(r, dis[x][y] + 1);
        for (int i = -1; i < 2; ++i) {
            for (int j = -1; j < 2; ++j) {
                if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                if (dis[x][y] + 1 < dis[x + i][y + j]) {
                    dis[x + i][y + j] = dis[x][y] + 1;
                    q.push({x + i, y + j});
                }
            }
        }
    }

    while (r - l > 1) {
        int mid = (r + l) / 2;
        vector<vector<int>> dis2(n + 2, vector<int>(m + 2, 1e9));
        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (f[x][y] == 'X' && dis[x][y] >= mid) {
                    q.push({x, y});
                    dis2[x][y] = 0;
                }
            }
        }
        int disc = 1;
        while (!q.empty()) {
            auto cor = q.front();
            q.pop();
            int x = cor.first;
            int y = cor.second;
            for (int i = -1; i < 2; ++i) {
                for (int j = -1; j < 2; ++j) {
                    if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                    if (dis2[x][y] + 1 < dis2[x + i][y + j]) {
                        dis2[x + i][y + j] = dis2[x][y] + 1;
                        if (f[x + i][y + j] == 'X' && dis2[x + i][y + j] > mid - 1) {
                            disc = 0;
                            queue<pair<int, int>> q2;
                            q.swap(q2);
                            goto CHECK;
                        }
                        q.push({x + i, y + j});
                    }
                }
            }
        }

        // for (int x = 1; x <= n; ++x) {
        //     for (int y = 1; y <= m; ++y) {
        //         if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
        //             disc = 0;
        //         }
        //     }
        // }
        CHECK:;
        if (disc) {
            l = mid;
        } else {
            r = mid;
        }
    }

    cout << l - 1 << endl;
    for (int x = 1; x <= n; ++x) {
        for (int y = 1; y <= m; ++y) {
            if (f[x][y] == 'X' && dis[x][y] >= l) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
}


#pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,sse,sse2")
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int n, m;

int id(int x, int y) {
    return x * (m + 2) + y;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m;
    char f[n + 2][m + 2];
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> f[i][j];
        }
    }

    vector<vector<int>> dis(n + 2, vector<int>(m + 2, 1e9));
    queue<pair<int, int>> q;
    for (int i = 0; i <= n + 1; ++i) {
        for (int j = 0; j <= m + 1; ++j) {
            if (f[i][j] != 'X') {
                dis[i][j] = 0;
                q.push({i, j});
            }
        }
    }
    int l = 0, r = 0;
    while (!q.empty()) {
        auto cor = q.front();
        q.pop();
        int x = cor.first;
        int y = cor.second;
        r = max(r, dis[x][y] + 1);
        for (int i = -1; i < 2; ++i) {
            for (int j = -1; j < 2; ++j) {
                if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                if (dis[x][y] + 1 < dis[x + i][y + j]) {
                    dis[x + i][y + j] = dis[x][y] + 1;
                    q.push({x + i, y + j});
                }
            }
        }
    }

    while (r - l > 1) {
        int mid = (r + l) / 2;
        vector<vector<int>> dis2(n + 2, vector<int>(m + 2, 1e9));
        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (f[x][y] == 'X' && dis[x][y] >= mid) {
                    q.push({x, y});
                    dis2[x][y] = 0;
                }
            }
        }
        int disc = 1;
        while (!q.empty()) {
            auto cor = q.front();
            q.pop();
            int x = cor.first;
            int y = cor.second;
            // if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
            //     disc = 0;
            //     while (!q.empty()) q.pop();
            //     goto CHECK;
            // }
            for (int i = -1; i < 2; ++i) {
                for (int j = -1; j < 2; ++j) {
                    if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                    if (dis2[x][y] + 1 < dis2[x + i][y + j]) {
                        dis2[x + i][y + j] = dis2[x][y] + 1;
                        q.push({x + i, y + j});
                    }
                }
            }
        }

        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
                    disc = 0;
                }
            }
        }
        CHECK:;
        if (disc) {
            l = mid;
        } else {
            r = mid;
        }
    }

    cout << l - 1 << endl;
    for (int x = 1; x <= n; ++x) {
        for (int y = 1; y <= m; ++y) {
            if (f[x][y] == 'X' && dis[x][y] >= l) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
}


// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,sse,sse2")
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int n, m;

int id(int x, int y) {
    return x * (m + 2) + y;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m;
    char f[n + 2][m + 2];
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> f[i][j];
        }
    }

    vector<vector<int>> dis(n + 2, vector<int>(m + 2, 1e9));
    queue<pair<int, int>> q;
    for (int i = 0; i <= n + 1; ++i) {
        for (int j = 0; j <= m + 1; ++j) {
            if (f[i][j] != 'X') {
                dis[i][j] = 0;
                q.push({i, j});
            }
        }
    }
    int l = 0, r = 0;
    while (!q.empty()) {
        auto cor = q.front();
        q.pop();
        int x = cor.first;
        int y = cor.second;
        r = max(r, dis[x][y] + 1);
        for (int i = -1; i < 2; ++i) {
            for (int j = -1; j < 2; ++j) {
                if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                if (dis[x][y] + 1 < dis[x + i][y + j]) {
                    dis[x + i][y + j] = dis[x][y] + 1;
                    q.push({x + i, y + j});
                }
            }
        }
    }

    while (r - l > 1) {
        int mid = (r + l) / 2;
        vector<vector<int>> dis2(n + 2, vector<int>(m + 2, 1e9));
        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (f[x][y] == 'X' && dis[x][y] >= mid) {
                    q.push({x, y});
                    dis2[x][y] = 0;
                }
            }
        }
        int disc = 1;
        while (!q.empty()) {
            auto cor = q.front();
            q.pop();
            int x = cor.first;
            int y = cor.second;
            for (int i = -1; i < 2; ++i) {
                for (int j = -1; j < 2; ++j) {
                    if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1 || f[x + i][y + j] != 'X') continue;
                    if (dis2[x][y] + 1 < dis2[x + i][y + j]) {
                        dis2[x + i][y + j] = dis2[x][y] + 1;
                        if (dis2[x + i][y + j] > mid - 1) {
                            disc = 0;
                            goto CHECK;
                        }
                        q.push({x + i, y + j});
                    }
                }
            }
        }

        // for (int x = 1; x <= n; ++x) {
        //     for (int y = 1; y <= m; ++y) {
        //         if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
        //             disc = 0;
        //         }
        //     }
        // }
        CHECK:;
        if (disc) {
            l = mid;
        } else {
            r = mid;
        }
    }

    cout << l - 1 << endl;
    for (int x = 1; x <= n; ++x) {
        for (int y = 1; y <= m; ++y) {
            if (f[x][y] == 'X' && dis[x][y] >= l) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
}


// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,sse,sse2")
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int n, m;

int id(int x, int y) {
    return x * (m + 2) + y;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m;
    char f[n + 2][m + 2];
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> f[i][j];
        }
    }

    vector<vector<int>> dis(n + 2, vector<int>(m + 2, 1e9));
    queue<pair<int, int>> q;
    for (int i = 0; i <= n + 1; ++i) {
        for (int j = 0; j <= m + 1; ++j) {
            if (f[i][j] != 'X') {
                dis[i][j] = 0;
                q.push({i, j});
            }
        }
    }
    int l = 0, r = 0;
    while (!q.empty()) {
        auto cor = q.front();
        q.pop();
        int x = cor.first;
        int y = cor.second;
        r = max(r, dis[x][y] + 1);
        for (int i = -1; i < 2; ++i) {
            for (int j = -1; j < 2; ++j) {
                if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                if (dis[x][y] + 1 < dis[x + i][y + j]) {
                    dis[x + i][y + j] = dis[x][y] + 1;
                    q.push({x + i, y + j});
                }
            }
        }
    }

    while (r - l > 1) {
        int mid = (r + l) / 2;
        vector<vector<int>> dis2(n + 2, vector<int>(m + 2, 1e9));
        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (f[x][y] == 'X' && dis[x][y] >= mid) {
                    q.push({x, y});
                    dis2[x][y] = 0;
                }
            }
        }
        int disc = 1;
        while (!q.empty()) {
            auto cor = q.front();
            q.pop();
            int x = cor.first;
            int y = cor.second;
            if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
                disc = 0;
                while (!q.empty()) q.pop();
                goto CHECK;
            }
            for (int i = -1; i < 2; ++i) {
                for (int j = -1; j < 2; ++j) {
                    if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                    if (dis2[x][y] + 1 < dis2[x + i][y + j]) {
                        dis2[x + i][y + j] = dis2[x][y] + 1;

                        q.push({x + i, y + j});
                    }
                }
            }
        }

        // for (int x = 1; x <= n; ++x) {
        //     for (int y = 1; y <= m; ++y) {
        //         if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
        //             disc = 0;
        //         }
        //     }
        // }
        CHECK:;
        if (disc) {
            l = mid;
        } else {
            r = mid;
        }
    }

    cout << l - 1 << endl;
    for (int x = 1; x <= n; ++x) {
        for (int y = 1; y <= m; ++y) {
            if (f[x][y] == 'X' && dis[x][y] >= l) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
}


// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,sse,sse2")
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int n, m;

int id(int x, int y) {
    return x * (m + 2) + y;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m;
    char f[n + 2][m + 2];
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> f[i][j];
        }
    }

    vector<vector<int>> dis(n + 2, vector<int>(m + 2, 1e9));
    queue<pair<int, int>> q;
    for (int i = 0; i <= n + 1; ++i) {
        for (int j = 0; j <= m + 1; ++j) {
            if (f[i][j] != 'X') {
                dis[i][j] = 0;
                q.push({i, j});
            }
        }
    }
    int l = 0, r = 0;
    while (!q.empty()) {
        auto cor = q.front();
        q.pop();
        int x = cor.first;
        int y = cor.second;
        r = max(r, dis[x][y] + 1);
        for (int i = -1; i < 2; ++i) {
            for (int j = -1; j < 2; ++j) {
                if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                if (dis[x][y] + 1 < dis[x + i][y + j]) {
                    dis[x + i][y + j] = dis[x][y] + 1;
                    q.push({x + i, y + j});
                }
            }
        }
    }

    while (r - l > 1) {
        int mid = (r + l) / 2;
        vector<vector<int>> dis2(n + 2, vector<int>(m + 2, 1e9));
        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (f[x][y] == 'X' && dis[x][y] >= mid) {
                    q.push({x, y});
                    dis2[x][y] = 0;
                }
            }
        }
        int disc = 1;
        while (!q.empty()) {
            auto cor = q.front();
            q.pop();
            int x = cor.first;
            int y = cor.second;
            if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
                disc = 0;
                while (!q.empty()) q.pop()
                goto CHECK;
            }
            for (int i = -1; i < 2; ++i) {
                for (int j = -1; j < 2; ++j) {
                    if (x + i < 0 || x + i > n + 1 || y + j < 0 || y + j > m + 1) continue;
                    if (dis2[x][y] + 1 < dis2[x + i][y + j]) {
                        dis2[x + i][y + j] = dis2[x][y] + 1;

                        q.push({x + i, y + j});
                    }
                }
            }
        }

        // for (int x = 1; x <= n; ++x) {
        //     for (int y = 1; y <= m; ++y) {
        //         if (f[x][y] == 'X' && dis2[x][y] > mid - 1) {
        //             disc = 0;
        //         }
        //     }
        // }
        CHECK:;
        if (disc) {
            l = mid;
        } else {
            r = mid;
        }
    }

    cout << l - 1 << endl;
    for (int x = 1; x <= n; ++x) {
        for (int y = 1; y <= m; ++y) {
            if (f[x][y] == 'X' && dis[x][y] >= l) {
                cout << 'X';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
//#pragma warning(disable : 4996)
// i.e
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define len(x) (int)(x).size()
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define file freopen("buses.in", "r", stdin); freopen("buses.out", "w", stdout)
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
const int maxn = 100000 + 500;
const int mod = 1e9 + 7;
vector<vector<int> >a, pref, cnt, suck;
vector<int>cur;
int get(int i1, int j1, int i2, int j2) {
    return pref[i2][j2] + pref[i1 - 1][j1 - 1] - pref[i2][j1 - 1] - pref[i1 - 1][j2];
}
vector<pair<pair<int, int>, int> >verts;
signed main() {
    fast;
    int n, m;
    cin >> n >> m;
    a.resize(n + 2);
    pref.resize(n + 2);
    suck.resize(n + 2);
    cnt.resize(n + 2);
    cur.resize(m + 2);
    int ans = 1e9;
    for (auto &ii : a) ii.resize(m + 2);
    for (auto &ii : pref) ii.resize(m + 2);
    for (auto &ii : suck) ii.resize(m + 2);
    for (auto &ii : cnt) ii.resize(m + 2);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            char x;
            cin >> x;
            a[i][j] = (x == 'X');
            pref[i][j] = pref[i - 1][j] + pref[i][j - 1] + a[i][j] - pref[i - 1][j - 1];
        }
    }
    int l = 0, r = (min(n, m) - 1) / 2 + 1;
    while (r - l > 1) {
        int md = l + r >> 1;
        verts.clear();
        bool fl = 0;
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                cnt[i][j] = 0;
            }
        }
        for (int j = 0; j <= m; j++) cur[j] = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (i - md >= 1 && i + md <= n && j - md >= 1 && j + md <= m) {
                    int kek = get(i - md, j - md, i + md, j + md);
                    if (kek != (2 * md + 1) * (2 * md + 1));
                    else {
                        verts.pb({{i, j}, md});
                    }
                }
            }
        }
        for (auto ii : verts) {
            int i1 = ii.F.F - ii.S, i2 = ii.F.F + ii.S + 1;
            int j1 = ii.F.S - ii.S, j2 = ii.F.S + ii.S + 1;
            cnt[i1][j1]++;
            cnt[i1][j2]--;
            cnt[i2][j1]--;
            cnt[i2][j2]++;
        }
        for (int i = 1; i <= n; i++) {
            int now = 0;
            for (int j = 1; j <= n; j++) {
                cur[j] += cnt[i][j];
                now += cur[j];
                if (now) suck[i][j] = 1;
                else suck[i][j] = 0;
                if (suck[i][j] != a[i][j]) {
                    fl = 1;
                    break;
                }
            }
            if (fl) break;
        }
        if (fl) r = md;
        else l = md;
    }
    cout << l << '\n';
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (i - l >= 1 && i + l <= n && j - l >= 1 && j + l <= m) {
                int kek = get(i - l, j - l, i + l, j + l);
                if (kek != (2 * l + 1) * (2 * l + 1)) cout << '.';
                else cout << 'X';
            }
            else cout << '.';
        }
        cout << '\n';
    }
    return 0;
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
//#pragma warning(disable : 4996)
// i.e
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define len(x) (int)(x).size()
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define file freopen("buses.in", "r", stdin); freopen("buses.out", "w", stdout)
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
const int maxn = 100000 + 500;
const int mod = 1e9 + 7;
vector<vector<int> >a, pref, cnt, suck;
vector<int>cur;
int get(int i1, int j1, int i2, int j2) {
    return pref[i2][j2] + pref[i1 - 1][j1 - 1] - pref[i2][j1 - 1] - pref[i1 - 1][j2];
}
vector<pair<pair<int, int>, int> >verts;
signed main() {
    fast;
    int n, m;
    cin >> n >> m;
    a.resize(n + 2);
    pref.resize(n + 2);
    suck.resize(n + 2);
    cnt.resize(n + 2);
    cur.resize(m + 2);
    int ans = 1e9;
    for (auto &ii : a) ii.resize(m + 2);
    for (auto &ii : pref) ii.resize(m + 2);
    for (auto &ii : suck) ii.resize(m + 2);
    for (auto &ii : cnt) ii.resize(m + 2);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            char x;
            cin >> x;
            a[i][j] = (x == 'X');
            pref[i][j] = pref[i - 1][j] + pref[i][j - 1] + a[i][j] - pref[i - 1][j - 1];
        }
    }
    int l = 0, r = (min(n, m) - 1) / 2 + 1;
    while (r - l > 1) {
        int md = l + r >> 1;
        verts.clear();
        bool fl = 0;
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                cnt[i][j] = 0;
            }
        }
        for (int j = 0; j <= m; j++) cur[j] = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (i - md >= 1 && i + md <= n && j - md >= 1 && j + md <= m) {
                    int kek = get(i - md, j - md, i + md, j + md);
                    if (kek != (2 * md + 1) * (2 * md + 1));
                    else {
                        verts.pb({{i, j}, md});
                    }
                }
            }
        }
        for (auto ii : verts) {
            int i1 = ii.F.F - ii.S, i2 = ii.F.F + ii.S + 1;
            int j1 = ii.F.S - ii.S, j2 = ii.F.S + ii.S + 1;
            cnt[i1][j1]++;
            cnt[i1][j2]--;
            cnt[i2][j1]--;
            cnt[i2][j2]++;
        }
        for (int i = 1; i <= n; i++) {
            int now = 0;
            for (int j = 1; j <= m; j++) {
                cur[j] += cnt[i][j];
                now += cur[j];
                if (now) suck[i][j] = 1;
                else suck[i][j] = 0;
                if (suck[i][j] != a[i][j]) {
                    fl = 1;
                    break;
                }
            }
            if (fl) break;
        }
        if (fl) r = md;
        else l = md;
    }
    cout << l << '\n';
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (i - l >= 1 && i + l <= n && j - l >= 1 && j + l <= m) {
                int kek = get(i - l, j - l, i + l, j + l);
                if (kek != (2 * l + 1) * (2 * l + 1)) cout << '.';
                else cout << 'X';
            }
            else cout << '.';
        }
        cout << '\n';
    }
    return 0;
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
//#pragma warning(disable : 4996)
// i.e
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define len(x) (int)(x).size()
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define file freopen("buses.in", "r", stdin); freopen("buses.out", "w", stdout)
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
const int maxn = 100000 + 500;
const int mod = 1e9 + 7;
vector<vector<int> >a;
vector<vector<int> >b;
signed main() {
    fast;
    int n, m;
    cin >> n >> m;
    a.resize(n);
    b.resize(n);
    int ans = 1e9;
    for (auto &ii : a) ii.resize(m);
    for (auto &ii : b) ii.resize(m);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            char x;
            cin >> x;
            a[i][j] = (x == 'X');
        }
        int now = 0;
        for (int j = 0; j < m; j++) {
            if (a[i][j]) now++;
            else {
                if (now) ans = min(ans, (now - 1) / 2);
                now = 0;
            }
        }
        if (now) ans = min(ans, (now - 1) / 2);
    }
    for (int j = 0; j < m; j++) {
        int now = 0;
        for (int i = 0; i < n; i++) {
            if (a[i][j]) now++;
            else {
                if (now) ans = min(ans, (now - 1) / 2);
                now = 0;
            }
        }
        if (now) ans = min(ans, (now - 1) / 2);
    }
    cout << ans << '\n';
    for (int i = 0; i < n; i++) {
        int lf;
        int now = 0;
        for (int j = 0; j < m; j++) {
            if (a[i][j]) {
                if (!now) {
                    lf = j;
                }
                now++;
            }
            else {
                if (now) {
                    for (int k = lf + ans; k < j - ans; k++) {
                        b[i][k]++;
                    }
                    now = 0;
                }
            }
        }
        if (now) {
            for (int k = lf + ans; k < m - ans; k++) {
                b[i][k]++;
            }
            now = 0;
        }
    }
    for (int j = 0; j < m; j++) {
        int lf;
        int now = 0;
        for (int i = 0; i < n; i++) {
            if (a[i][j]) {
                if (!now) {
                    lf = i;
                }
                now++;
            }
            else {
                if (now) {
                    for (int k = lf + ans; k < i - ans; k++) {
                        b[k][j]++;
                    }
                    now = 0;
                }
            }
        }
        if (now) {
            for (int k = lf + ans; k < n - ans; k++) {
                b[k][j]++;
            }
            now = 0;
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << (b[i][j] == 2 ? 'X' : '.');
        }
        cout << '\n';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <iomanip>
#define FOR(i, n, m) for(ll i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-15;

template<typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
    os << to_string(p.first) << " " << to_string(p.second);
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
    REP(i, v.size()) {
        if(i) os << " ";
        os << to_string(v[i]);
    }
    return os;
}

struct modint {
    ll n;
public:
    modint(const ll n = 0) : n((n % mod + mod) % mod) {}
    static modint pow(modint a, int m) {
        modint r = 1;
        while(m > 0) {
            if(m & 1) { r *= a; }
            a = (a * a); m /= 2;
        }
        return r;
    }
    modint &operator++() { *this += 1; return *this; }
    modint &operator--() { *this -= 1; return *this; }
    modint operator++(int) { modint ret = *this; *this += 1; return ret; }
    modint operator--(int) { modint ret = *this; *this -= 1; return ret; }
    modint operator~() const { return (this -> pow(n, mod - 2)); } // inverse
    friend bool operator==(const modint& lhs, const modint& rhs) {
        return lhs.n == rhs.n;
    }
    friend bool operator<(const modint& lhs, const modint& rhs) {
        return lhs.n < rhs.n;
    }
    friend bool operator>(const modint& lhs, const modint& rhs) {
        return lhs.n > rhs.n;
    }
    friend modint &operator+=(modint& lhs, const modint& rhs) {
        lhs.n += rhs.n;
        if (lhs.n >= mod) lhs.n -= mod;
        return lhs;
    }
    friend modint &operator-=(modint& lhs, const modint& rhs) {
        lhs.n -= rhs.n;
        if (lhs.n < 0) lhs.n += mod;
        return lhs;
    }
    friend modint &operator*=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * rhs.n) % mod;
        return lhs;
    }
    friend modint &operator/=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * (~rhs).n) % mod;
        return lhs;
    }
    friend modint operator+(const modint& lhs, const modint& rhs) {
        return modint(lhs.n + rhs.n);
    }
    friend modint operator-(const modint& lhs, const modint& rhs) {
        return modint(lhs.n - rhs.n);
    }
    friend modint operator*(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * rhs.n);
    }
    friend modint operator/(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * (~rhs).n);
    }
};
istream& operator>>(istream& is, modint m) { is >> m.n; return is; }
ostream& operator<<(ostream& os, modint m) { os << m.n; return os; }

#define MAX_N 10000001
long long extgcd(long long a, long long b, long long& x, long long& y) {
    long long d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}
long long mod_inverse(long long a, long long m) {
    long long x, y;
    if(extgcd(a, m, x, y) == 1) return (m + x % m) % m;
    else return -1;
}
vector<long long> fact(MAX_N+1, inf);
long long mod_fact(long long n, long long& e) {
    if(fact[0] == inf) {
        fact[0]=1;
        if(MAX_N != 0) fact[1]=1;
        for(ll i = 2; i <= MAX_N; ++i) {
            fact[i] = (fact[i-1] * i) % mod;
        }
    }
    e = 0;
    if(n == 0) return 1;
    long long res = mod_fact(n / mod, e);
    e += n / mod;
    if((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod;
    return (res * fact[n % mod]) % mod;
}
// return nCk
long long mod_comb(long long n, long long k) {
    if(n < 0 || k < 0 || n < k) return 0;
    long long e1, e2, e3;
    long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2), a3 = mod_fact(n - k, e3);
    if(e1 > e2 + e3) return 0;
    return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod;
}

using mi = modint;

mi mod_pow(ll a, ll n) {
    mi ret = 1;
    mi tmp = a;
    while(n > 0) {
        if(n % 2) ret *= tmp;
        tmp = tmp * tmp;
        n /= 2;
    }
    return ret;
}

template< class T >
struct CumulativeSum2D
{
  vector< vector< T > > data;

  CumulativeSum2D(int W, int H) : data(W + 1, vector< int >(H + 1, 0)) {}

  void add(int x, int y, int z)
  {
    ++x; ++y;
    if(x >= data.size() || y >= data[0].size()) return;
    data[x][y] += z;
  }

  void build()
  {
    for(int i = 1; i < data.size(); i++) {
      for(int j = 1; j < data[i].size(); j++) {
        data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
      }
    }
  }

  T query(int sx, int sy, int gx, int gy)
  {
    return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]);
  }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n >> m;
    vector<string> g(n);
    REP(i, n) cin >> g[i];
    CumulativeSum2D<int> s1(n, m);
    REP(i, n) REP(j, m) if(g[i][j] == 'X') s1.add(i, j, 1);
    s1.build();
    ll l = 0, r = min(n, m) + 1;
    while(r - l > 1) {
        ll t = (l + r) / 2;
        CumulativeSum2D<int> s2(n, m);
        REP(i, n) REP(j, m) {
            ll sx = i - t, sy = j - t, gx = i + t + 1, gy = j + t + 1;
            if(sx >= 0 && sy >= 0 && gx <= n && gy <= m) {
                ll sum = s1.query(sx, sy, gx, gy);
                if(sum == (gx - sx) * (gy - sy)) {
                    s2.add(sx, sy, 1);
                    if(gx < n - 1) s2.add(gx, sy, -1);
                    if(gy < m - 1) s2.add(sx, gy, -1);
                    if(gx < n - 1 && gy < m - 1) s2.add(gx, gy, 1);
                }
            }
        }
        s2.build();
        bool ok = true;
        REP(i, n) REP(j, m) {
            if(g[i][j] == 'X' && s2.query(0, 0, i + 1, j + 1) == 0) ok = false;
        }
        if(ok) l = t;
        else r = t;
    }
    cout << l << endl;
    vector<vector<char>> ans(n, vector<char>(m));
    REP(i, n) REP(j, m) {
        ll sx = i - l, sy = j - l, gx = i + l + 1, gy = j + l + 1;
        if(sx >= 0 && sy >= 0 && gx <= n && gy <= m) {
            ll sum = s1.query(sx, sy, gx, gy);
            if(sum == (gx - sx) * (gy - sy)) {
                ans[i][j] = 'X';
            } else ans[i][j] = '.';
        } else ans[i][j] = '.';
    }
    REP(i, n) {
        REP(j, m) cout << ans[i][j];
        cout << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <iomanip>
#define FOR(i, n, m) for(ll i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-15;

template<typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
    os << to_string(p.first) << " " << to_string(p.second);
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
    REP(i, v.size()) {
        if(i) os << " ";
        os << to_string(v[i]);
    }
    return os;
}

struct modint {
    ll n;
public:
    modint(const ll n = 0) : n((n % mod + mod) % mod) {}
    static modint pow(modint a, int m) {
        modint r = 1;
        while(m > 0) {
            if(m & 1) { r *= a; }
            a = (a * a); m /= 2;
        }
        return r;
    }
    modint &operator++() { *this += 1; return *this; }
    modint &operator--() { *this -= 1; return *this; }
    modint operator++(int) { modint ret = *this; *this += 1; return ret; }
    modint operator--(int) { modint ret = *this; *this -= 1; return ret; }
    modint operator~() const { return (this -> pow(n, mod - 2)); } // inverse
    friend bool operator==(const modint& lhs, const modint& rhs) {
        return lhs.n == rhs.n;
    }
    friend bool operator<(const modint& lhs, const modint& rhs) {
        return lhs.n < rhs.n;
    }
    friend bool operator>(const modint& lhs, const modint& rhs) {
        return lhs.n > rhs.n;
    }
    friend modint &operator+=(modint& lhs, const modint& rhs) {
        lhs.n += rhs.n;
        if (lhs.n >= mod) lhs.n -= mod;
        return lhs;
    }
    friend modint &operator-=(modint& lhs, const modint& rhs) {
        lhs.n -= rhs.n;
        if (lhs.n < 0) lhs.n += mod;
        return lhs;
    }
    friend modint &operator*=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * rhs.n) % mod;
        return lhs;
    }
    friend modint &operator/=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * (~rhs).n) % mod;
        return lhs;
    }
    friend modint operator+(const modint& lhs, const modint& rhs) {
        return modint(lhs.n + rhs.n);
    }
    friend modint operator-(const modint& lhs, const modint& rhs) {
        return modint(lhs.n - rhs.n);
    }
    friend modint operator*(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * rhs.n);
    }
    friend modint operator/(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * (~rhs).n);
    }
};
istream& operator>>(istream& is, modint m) { is >> m.n; return is; }
ostream& operator<<(ostream& os, modint m) { os << m.n; return os; }

#define MAX_N 10000001
long long extgcd(long long a, long long b, long long& x, long long& y) {
    long long d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}
long long mod_inverse(long long a, long long m) {
    long long x, y;
    if(extgcd(a, m, x, y) == 1) return (m + x % m) % m;
    else return -1;
}
vector<long long> fact(MAX_N+1, inf);
long long mod_fact(long long n, long long& e) {
    if(fact[0] == inf) {
        fact[0]=1;
        if(MAX_N != 0) fact[1]=1;
        for(ll i = 2; i <= MAX_N; ++i) {
            fact[i] = (fact[i-1] * i) % mod;
        }
    }
    e = 0;
    if(n == 0) return 1;
    long long res = mod_fact(n / mod, e);
    e += n / mod;
    if((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod;
    return (res * fact[n % mod]) % mod;
}
// return nCk
long long mod_comb(long long n, long long k) {
    if(n < 0 || k < 0 || n < k) return 0;
    long long e1, e2, e3;
    long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2), a3 = mod_fact(n - k, e3);
    if(e1 > e2 + e3) return 0;
    return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod;
}

using mi = modint;

mi mod_pow(ll a, ll n) {
    mi ret = 1;
    mi tmp = a;
    while(n > 0) {
        if(n % 2) ret *= tmp;
        tmp = tmp * tmp;
        n /= 2;
    }
    return ret;
}

template< class T >
struct CumulativeSum2D
{
  vector< vector< T > > data;

  CumulativeSum2D(int W, int H) : data(W + 1, vector< ll >(H + 1, 0)) {}

  void add(int x, int y, int z)
  {
    ++x; ++y;
    if(x >= data.size() || y >= data[0].size()) return;
    data[x][y] += z;
  }

  void build()
  {
    for(int i = 1; i < data.size(); i++) {
      for(int j = 1; j < data[i].size(); j++) {
        data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
      }
    }
  }

  T query(int sx, int sy, int gx, int gy)
  {
    return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]);
  }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n >> m;
    vector<string> g(n);
    REP(i, n) cin >> g[i];
    CumulativeSum2D<ll> s1(n + 10, m + 10);
    REP(i, n) REP(j, m) if(g[i][j] == 'X') s1.add(i, j, 1);
    s1.build();
    ll l = 0, r = inf + 1;
    while(r - l > 1) {
        ll t = (l + r) / 2;
        CumulativeSum2D<ll> s2(n + 10, m + 10);
        REP(i, n) REP(j, m) {
            ll sx = i - t, sy = j - t, gx = i + t + 1, gy = j + t + 1;
            if(sx >= 0 && sy >= 0 && gx <= n && gy <= m) {
                ll sum = s1.query(sx, sy, gx, gy);
                if(sum == (gx - sx) * (gy - sy)) {
                    s2.add(sx, sy, 1);
                    if(gx < n) s2.add(gx, sy, -1);
                    if(gy < m) s2.add(sx, gy, -1);
                    if(gx < n && gy < m) s2.add(gx, gy, 1);
                }
            }
        }
        s2.build();
        bool ok = true;
        REP(i, n) REP(j, m) {
            if(g[i][j] == 'X' && s2.query(0, 0, i + 1, j + 1) == 0) ok = false;
        }
        if(ok) l = t;
        else r = t;
    }
    cout << l << endl;
    vector<vector<char>> ans(n, vector<char>(m));
    REP(i, n) REP(j, m) {
        ll sx = i - l, sy = j - l, gx = i + l + 1, gy = j + l + 1;
        if(sx >= 0 && sy >= 0 && gx <= n && gy <= m) {
            ll sum = s1.query(sx, sy, gx, gy);
            if(sum == (gx - sx) * (gy - sy)) {
                ans[i][j] = 'X';
            } else ans[i][j] = '.';
        } else ans[i][j] = '.';
    }
    REP(i, n) {
        REP(j, m) cout << ans[i][j];
        cout << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <iomanip>
#define FOR(i, n, m) for(ll i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-15;

template<typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
    os << to_string(p.first) << " " << to_string(p.second);
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
    REP(i, v.size()) {
        if(i) os << " ";
        os << to_string(v[i]);
    }
    return os;
}

struct modint {
    ll n;
public:
    modint(const ll n = 0) : n((n % mod + mod) % mod) {}
    static modint pow(modint a, int m) {
        modint r = 1;
        while(m > 0) {
            if(m & 1) { r *= a; }
            a = (a * a); m /= 2;
        }
        return r;
    }
    modint &operator++() { *this += 1; return *this; }
    modint &operator--() { *this -= 1; return *this; }
    modint operator++(int) { modint ret = *this; *this += 1; return ret; }
    modint operator--(int) { modint ret = *this; *this -= 1; return ret; }
    modint operator~() const { return (this -> pow(n, mod - 2)); } // inverse
    friend bool operator==(const modint& lhs, const modint& rhs) {
        return lhs.n == rhs.n;
    }
    friend bool operator<(const modint& lhs, const modint& rhs) {
        return lhs.n < rhs.n;
    }
    friend bool operator>(const modint& lhs, const modint& rhs) {
        return lhs.n > rhs.n;
    }
    friend modint &operator+=(modint& lhs, const modint& rhs) {
        lhs.n += rhs.n;
        if (lhs.n >= mod) lhs.n -= mod;
        return lhs;
    }
    friend modint &operator-=(modint& lhs, const modint& rhs) {
        lhs.n -= rhs.n;
        if (lhs.n < 0) lhs.n += mod;
        return lhs;
    }
    friend modint &operator*=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * rhs.n) % mod;
        return lhs;
    }
    friend modint &operator/=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * (~rhs).n) % mod;
        return lhs;
    }
    friend modint operator+(const modint& lhs, const modint& rhs) {
        return modint(lhs.n + rhs.n);
    }
    friend modint operator-(const modint& lhs, const modint& rhs) {
        return modint(lhs.n - rhs.n);
    }
    friend modint operator*(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * rhs.n);
    }
    friend modint operator/(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * (~rhs).n);
    }
};
istream& operator>>(istream& is, modint m) { is >> m.n; return is; }
ostream& operator<<(ostream& os, modint m) { os << m.n; return os; }

#define MAX_N 10000001
long long extgcd(long long a, long long b, long long& x, long long& y) {
    long long d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}
long long mod_inverse(long long a, long long m) {
    long long x, y;
    if(extgcd(a, m, x, y) == 1) return (m + x % m) % m;
    else return -1;
}
vector<long long> fact(MAX_N+1, inf);
long long mod_fact(long long n, long long& e) {
    if(fact[0] == inf) {
        fact[0]=1;
        if(MAX_N != 0) fact[1]=1;
        for(ll i = 2; i <= MAX_N; ++i) {
            fact[i] = (fact[i-1] * i) % mod;
        }
    }
    e = 0;
    if(n == 0) return 1;
    long long res = mod_fact(n / mod, e);
    e += n / mod;
    if((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod;
    return (res * fact[n % mod]) % mod;
}
// return nCk
long long mod_comb(long long n, long long k) {
    if(n < 0 || k < 0 || n < k) return 0;
    long long e1, e2, e3;
    long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2), a3 = mod_fact(n - k, e3);
    if(e1 > e2 + e3) return 0;
    return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod;
}

using mi = modint;

mi mod_pow(ll a, ll n) {
    mi ret = 1;
    mi tmp = a;
    while(n > 0) {
        if(n % 2) ret *= tmp;
        tmp = tmp * tmp;
        n /= 2;
    }
    return ret;
}

template< class T >
struct CumulativeSum2D
{
  vector< vector< T > > data;

  CumulativeSum2D(int W, int H) : data(W + 1, vector< ll >(H + 1, 0)) {}

  void add(int x, int y, int z)
  {
    ++x; ++y;
    if(x >= data.size() || y >= data[0].size()) return;
    data[x][y] += z;
  }

  void build()
  {
    for(int i = 1; i < data.size(); i++) {
      for(int j = 1; j < data[i].size(); j++) {
        data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
      }
    }
  }

  T query(int sx, int sy, int gx, int gy)
  {
    return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]);
  }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n >> m;
    vector<string> g(n);
    REP(i, n) cin >> g[i];
    CumulativeSum2D<ll> s1(n + 10, m + 10);
    REP(i, n) REP(j, m) if(g[i][j] == 'X') s1.add(i, j, 1);
    s1.build();
    ll l = 0, r = inf + 1;
    while(r - l > 1) {
        ll t = (l + r) / 2;
        CumulativeSum2D<ll> s2(n + 10, m + 10);
        REP(i, n) REP(j, m) {
            ll sx = i - t, sy = j - t, gx = i + t + 1, gy = j + t + 1;
            if(sx >= 0 && sy >= 0 && gx <= n && gy <= m) {
                ll sum = s1.query(sx, sy, gx, gy);
                if(sum == (gx - sx) * (gy - sy)) {
                    s2.add(sx, sy, 1);
                    if(gx < n - 1) s2.add(gx, sy, -1);
                    if(gy < m - 1) s2.add(sx, gy, -1);
                    if(gx < n - 1 && gy < m - 1) s2.add(gx, gy, 1);
                }
            }
        }
        s2.build();
        bool ok = true;
        REP(i, n) REP(j, m) {
            if(g[i][j] == 'X' && s2.query(0, 0, i + 1, j + 1) == 0) ok = false;
        }
        if(ok) l = t;
        else r = t;
    }
    cout << l << endl;
    vector<vector<char>> ans(n, vector<char>(m));
    REP(i, n) REP(j, m) {
        ll sx = i - l, sy = j - l, gx = i + l + 1, gy = j + l + 1;
        if(sx >= 0 && sy >= 0 && gx <= n && gy <= m) {
            ll sum = s1.query(sx, sy, gx, gy);
            if(sum == (gx - sx) * (gy - sy)) {
                ans[i][j] = 'X';
            } else ans[i][j] = '.';
        } else ans[i][j] = '.';
    }
    REP(i, n) {
        REP(j, m) cout << ans[i][j];
        cout << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <iomanip>
#define FOR(i, n, m) for(ll i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-15;

template<typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
    os << to_string(p.first) << " " << to_string(p.second);
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
    REP(i, v.size()) {
        if(i) os << " ";
        os << to_string(v[i]);
    }
    return os;
}

struct modint {
    ll n;
public:
    modint(const ll n = 0) : n((n % mod + mod) % mod) {}
    static modint pow(modint a, int m) {
        modint r = 1;
        while(m > 0) {
            if(m & 1) { r *= a; }
            a = (a * a); m /= 2;
        }
        return r;
    }
    modint &operator++() { *this += 1; return *this; }
    modint &operator--() { *this -= 1; return *this; }
    modint operator++(int) { modint ret = *this; *this += 1; return ret; }
    modint operator--(int) { modint ret = *this; *this -= 1; return ret; }
    modint operator~() const { return (this -> pow(n, mod - 2)); } // inverse
    friend bool operator==(const modint& lhs, const modint& rhs) {
        return lhs.n == rhs.n;
    }
    friend bool operator<(const modint& lhs, const modint& rhs) {
        return lhs.n < rhs.n;
    }
    friend bool operator>(const modint& lhs, const modint& rhs) {
        return lhs.n > rhs.n;
    }
    friend modint &operator+=(modint& lhs, const modint& rhs) {
        lhs.n += rhs.n;
        if (lhs.n >= mod) lhs.n -= mod;
        return lhs;
    }
    friend modint &operator-=(modint& lhs, const modint& rhs) {
        lhs.n -= rhs.n;
        if (lhs.n < 0) lhs.n += mod;
        return lhs;
    }
    friend modint &operator*=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * rhs.n) % mod;
        return lhs;
    }
    friend modint &operator/=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * (~rhs).n) % mod;
        return lhs;
    }
    friend modint operator+(const modint& lhs, const modint& rhs) {
        return modint(lhs.n + rhs.n);
    }
    friend modint operator-(const modint& lhs, const modint& rhs) {
        return modint(lhs.n - rhs.n);
    }
    friend modint operator*(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * rhs.n);
    }
    friend modint operator/(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * (~rhs).n);
    }
};
istream& operator>>(istream& is, modint m) { is >> m.n; return is; }
ostream& operator<<(ostream& os, modint m) { os << m.n; return os; }

#define MAX_N 10000001
long long extgcd(long long a, long long b, long long& x, long long& y) {
    long long d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}
long long mod_inverse(long long a, long long m) {
    long long x, y;
    if(extgcd(a, m, x, y) == 1) return (m + x % m) % m;
    else return -1;
}
vector<long long> fact(MAX_N+1, inf);
long long mod_fact(long long n, long long& e) {
    if(fact[0] == inf) {
        fact[0]=1;
        if(MAX_N != 0) fact[1]=1;
        for(ll i = 2; i <= MAX_N; ++i) {
            fact[i] = (fact[i-1] * i) % mod;
        }
    }
    e = 0;
    if(n == 0) return 1;
    long long res = mod_fact(n / mod, e);
    e += n / mod;
    if((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod;
    return (res * fact[n % mod]) % mod;
}
// return nCk
long long mod_comb(long long n, long long k) {
    if(n < 0 || k < 0 || n < k) return 0;
    long long e1, e2, e3;
    long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2), a3 = mod_fact(n - k, e3);
    if(e1 > e2 + e3) return 0;
    return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod;
}

using mi = modint;

mi mod_pow(ll a, ll n) {
    mi ret = 1;
    mi tmp = a;
    while(n > 0) {
        if(n % 2) ret *= tmp;
        tmp = tmp * tmp;
        n /= 2;
    }
    return ret;
}

template< class T >
struct CumulativeSum2D
{
  vector< vector< T > > data;

  CumulativeSum2D(int W, int H) : data(W + 1, vector< ll >(H + 1, 0)) {}

  void add(int x, int y, int z)
  {
    ++x; ++y;
    if(x >= data.size() || y >= data[0].size()) return;
    data[x][y] += z;
  }

  void build()
  {
    for(int i = 1; i < data.size(); i++) {
      for(int j = 1; j < data[i].size(); j++) {
        data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
      }
    }
  }

  T query(int sx, int sy, int gx, int gy)
  {
    return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]);
  }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n >> m;
    vector<string> g(n);
    REP(i, n) cin >> g[i];
    CumulativeSum2D<ll> s1(n + 10, m + 10);
    REP(i, n) REP(j, m) if(g[i][j] == 'X') s1.add(i, j, 1);
    s1.build();
    ll l = 0, r = max(n, m) + 1;
    while(r - l > 1) {
        ll t = (l + r) / 2;
        CumulativeSum2D<ll> s2(n + 10, m + 10);
        REP(i, n) REP(j, m) {
            ll sx = i - t, sy = j - t, gx = i + t + 1, gy = j + t + 1;
            if(sx >= 0 && sy >= 0 && gx <= n && gy <= m) {
                ll sum = s1.query(sx, sy, gx, gy);
                if(sum == (gx - sx) * (gy - sy)) {
                    s2.add(sx, sy, 1);
                    if(gx < n - 1) s2.add(gx, sy, -1);
                    if(gy < m - 1) s2.add(sx, gy, -1);
                    if(gx < n - 1 && gy < m - 1) s2.add(gx, gy, 1);
                }
            }
        }
        s2.build();
        bool ok = true;
        REP(i, n) REP(j, m) {
            if(g[i][j] == 'X' && s2.query(0, 0, i + 1, j + 1) == 0) ok = false;
        }
        if(ok) l = t;
        else r = t;
    }
    cout << l << endl;
    vector<vector<char>> ans(n, vector<char>(m));
    REP(i, n) REP(j, m) {
        ll sx = i - l, sy = j - l, gx = i + l + 1, gy = j + l + 1;
        if(sx >= 0 && sy >= 0 && gx <= n && gy <= m) {
            ll sum = s1.query(sx, sy, gx, gy);
            if(sum == (gx - sx) * (gy - sy)) {
                ans[i][j] = 'X';
            } else ans[i][j] = '.';
        } else ans[i][j] = '.';
    }
    REP(i, n) {
        REP(j, m) cout << ans[i][j];
        cout << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <iomanip>
#define FOR(i, n, m) for(ll i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-15;

template<typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
    os << to_string(p.first) << " " << to_string(p.second);
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
    REP(i, v.size()) {
        if(i) os << " ";
        os << to_string(v[i]);
    }
    return os;
}

struct modint {
    ll n;
public:
    modint(const ll n = 0) : n((n % mod + mod) % mod) {}
    static modint pow(modint a, int m) {
        modint r = 1;
        while(m > 0) {
            if(m & 1) { r *= a; }
            a = (a * a); m /= 2;
        }
        return r;
    }
    modint &operator++() { *this += 1; return *this; }
    modint &operator--() { *this -= 1; return *this; }
    modint operator++(int) { modint ret = *this; *this += 1; return ret; }
    modint operator--(int) { modint ret = *this; *this -= 1; return ret; }
    modint operator~() const { return (this -> pow(n, mod - 2)); } // inverse
    friend bool operator==(const modint& lhs, const modint& rhs) {
        return lhs.n == rhs.n;
    }
    friend bool operator<(const modint& lhs, const modint& rhs) {
        return lhs.n < rhs.n;
    }
    friend bool operator>(const modint& lhs, const modint& rhs) {
        return lhs.n > rhs.n;
    }
    friend modint &operator+=(modint& lhs, const modint& rhs) {
        lhs.n += rhs.n;
        if (lhs.n >= mod) lhs.n -= mod;
        return lhs;
    }
    friend modint &operator-=(modint& lhs, const modint& rhs) {
        lhs.n -= rhs.n;
        if (lhs.n < 0) lhs.n += mod;
        return lhs;
    }
    friend modint &operator*=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * rhs.n) % mod;
        return lhs;
    }
    friend modint &operator/=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * (~rhs).n) % mod;
        return lhs;
    }
    friend modint operator+(const modint& lhs, const modint& rhs) {
        return modint(lhs.n + rhs.n);
    }
    friend modint operator-(const modint& lhs, const modint& rhs) {
        return modint(lhs.n - rhs.n);
    }
    friend modint operator*(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * rhs.n);
    }
    friend modint operator/(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * (~rhs).n);
    }
};
istream& operator>>(istream& is, modint m) { is >> m.n; return is; }
ostream& operator<<(ostream& os, modint m) { os << m.n; return os; }

#define MAX_N 10000001
long long extgcd(long long a, long long b, long long& x, long long& y) {
    long long d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}
long long mod_inverse(long long a, long long m) {
    long long x, y;
    if(extgcd(a, m, x, y) == 1) return (m + x % m) % m;
    else return -1;
}
vector<long long> fact(MAX_N+1, inf);
long long mod_fact(long long n, long long& e) {
    if(fact[0] == inf) {
        fact[0]=1;
        if(MAX_N != 0) fact[1]=1;
        for(ll i = 2; i <= MAX_N; ++i) {
            fact[i] = (fact[i-1] * i) % mod;
        }
    }
    e = 0;
    if(n == 0) return 1;
    long long res = mod_fact(n / mod, e);
    e += n / mod;
    if((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod;
    return (res * fact[n % mod]) % mod;
}
// return nCk
long long mod_comb(long long n, long long k) {
    if(n < 0 || k < 0 || n < k) return 0;
    long long e1, e2, e3;
    long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2), a3 = mod_fact(n - k, e3);
    if(e1 > e2 + e3) return 0;
    return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod;
}

using mi = modint;

mi mod_pow(ll a, ll n) {
    mi ret = 1;
    mi tmp = a;
    while(n > 0) {
        if(n % 2) ret *= tmp;
        tmp = tmp * tmp;
        n /= 2;
    }
    return ret;
}

template< class T >
struct CumulativeSum2D
{
  vector< vector< T > > data;

  CumulativeSum2D(int W, int H) : data(W + 1, vector< int >(H + 1, 0)) {}

  void add(int x, int y, int z)
  {
    ++x; ++y;
    if(x >= data.size() || y >= data[0].size()) return;
    data[x][y] += z;
  }

  void build()
  {
    for(int i = 1; i < data.size(); i++) {
      for(int j = 1; j < data[i].size(); j++) {
        data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
      }
    }
  }

  T query(int sx, int sy, int gx, int gy)
  {
    return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]);
  }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n >> m;
    vector<string> g(n);
    REP(i, n) cin >> g[i];
    CumulativeSum2D<int> s1(n, m);
    REP(i, n) REP(j, m) if(g[i][j] == 'X') s1.add(i, j, 1);
    s1.build();
    int l = 0, r = min(n, m) + 1;
    while(r - l > 1) {
        int t = (l + r) / 2;
        CumulativeSum2D<int> s2(n, m);
        REP(i, n) REP(j, m) {
            int sx = i - t, sy = j - t, gx = i + t + 1, gy = j + t + 1;
            if(sx >= 0 && sy >= 0 && gx <= n && gy <= m) {
                int sum = s1.query(sx, sy, gx, gy);
                if(sum == (gx - sx) * (gy - sy)) {
                    s2.add(sx, sy, 1);
                    if(gx < n) s2.add(gx, sy, -1);
                    if(gy < m) s2.add(sx, gy, -1);
                    if(gx < n && gy < m) s2.add(gx, gy, 1);
                }
            }
        }
        s2.build();
        bool ok = true;
        REP(i, n) REP(j, m) {
            if(g[i][j] == 'X' && s2.query(0, 0, i + 1, j + 1) == 0) ok = false;
        }
        if(ok) l = t;
        else r = t;
    }
    cout << l << endl;
    vector<vector<char>> ans(n, vector<char>(m));
    REP(i, n) REP(j, m) {
        int sx = i - l, sy = j - l, gx = i + l + 1, gy = j + l + 1;
        if(sx >= 0 && sy >= 0 && gx <= n && gy <= m) {
            int sum = s1.query(sx, sy, gx, gy);
            if(sum == (gx - sx) * (gy - sy)) {
                ans[i][j] = 'X';
            } else ans[i][j] = '.';
        } else ans[i][j] = '.';
    }
    REP(i, n) {
        REP(j, m) cout << ans[i][j];
        cout << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second

const ll MX=1e6+1e4, MOD=1e9+7;
int n, m;
string gr[MX], ans[MX];
vi cnt[MX];

int countX(int br, int bc, int er, int ec) {
  er++; ec++;
  return cnt[er][ec] - cnt[br][ec] - cnt[er][bc] + cnt[br][bc];
}
bool allX(int br, int bc, int er, int ec) {
  return countX(br, bc, er, ec) == ((er-br+1)*(ec-bc+1));
}
bool isPossible(int x) {
  RE(r,n) {
    bool last=0;
    int cnt=0;
    REP(c,x,m-x) {
      if(allX(r,c-x,r,c+x)) {
        if(last) cnt++;
        else {
          cnt += countX(r,c-x,r,c+x);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(r,0,r,m-1))
      return 0;
  }
  RE(c,m) {
    bool last=0;
    int cnt=0;
    REP(r,x,n-x) {
      if(allX(r-x,c,r+x,c)) {
        if(last) cnt++;
        else {
          cnt += countX(r-x,c,r+x,c);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(0,c,n-1,c))
      return 0;
  }
  return 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin>>n>>m;
  RE(i,n) cin>>gr[i];
  RE(i,n+1) cnt[i].resize(m+1);
  RE(i,n+1) cnt[i][0]=0;
  RE(i,m+1) cnt[0][i]=0;
  RE(i,n) RE(j,m)
    cnt[i+1][j+1] = cnt[i][j+1] + cnt[i+1][j] - cnt[i][j] + (gr[i][j]=='X'?1:0);
  int lb=0, ub=min(n-1, m-1)/2;
  while(lb != ub) {
    int mid=(lb+ub+1)/2;
    if(isPossible(mid)) lb=mid;
    else ub=mid-1;
  }
  int x = lb;
  RE(i,n) ans[i].resize(m);
  RE(i,n) RE(j,m) ans[i][j] = '.';
  REP(i,x,n-x) REP(j,x,m-x) {
    if(allX(i-x,j-x,i+x,j+x)) ans[i][j] = 'X';
  }
  cout<<x<<endl;
  RE(i,n) cout<<ans[i]<<endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second

const ll MX=1e6+1e4, MX2=1e3+1e2, MOD=1e9+7;
int n, m;
string inpGr[MX], gr[MX2], ans[MX2];
vi cnt[MX2];

int countX(int br, int bc, int er, int ec) {
  er++; ec++;
  return cnt[er][ec] - cnt[br][ec] - cnt[er][bc] + cnt[br][bc];
}
bool allX(int br, int bc, int er, int ec) {
  return countX(br, bc, er, ec) == ((er-br+1)*(ec-bc+1));
}
bool isPossible(int x) {
  RE(r,n) {
    bool last=0;
    int cnt=0;
    REP(c,x,m-x) {
      if(allX(r,c-x,r,c+x)) {
        if(last) cnt++;
        else {
          cnt += countX(r,c-x,r,c+x);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(r,0,r,m-1))
      return 0;
  }
  RE(c,m) {
    bool last=0;
    int cnt=0;
    REP(r,x,n-x) {
      if(allX(r-x,c,r+x,c)) {
        if(last) cnt++;
        else {
          cnt += countX(r-x,c,r+x,c);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(0,c,n-1,c))
      return 0;
  }
  return 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin>>n>>m;
  RE(i,n) cin>>inpGr[i];
  if(m < n) {
    RE(i,m) gr[i].resize(n);
    RE(i,n) RE(j,m)
      gr[j][i] = inpGr[i][j];
    swap(n,m);
  } else {
    RE(i,n) gr[i].resize(m);
    RE(i,n) RE(j,m)
      gr[i][j] = inpGr[i][j];
  }
  RE(i,n+1) cnt[i].resize(m+1);
  RE(i,n+1) cnt[i][0]=0;
  RE(i,m+1) cnt[0][i]=0;
  RE(i,n) RE(j,m)
    cnt[i+1][j+1] = cnt[i][j+1] + cnt[i+1][j] - cnt[i][j] + (gr[i][j]=='X'?1:0);
  int lb=0, ub=min(n-1, m-1)/2;
  while(lb != ub) {
    int mid=(lb+ub+1)/2;
    if(isPossible(mid)) lb=mid;
    else ub=mid-1;
  }
  int x = lb;
  RE(i,n) ans[i].resize(m);
  RE(i,n) RE(j,m) ans[i][j] = '.';
  REP(i,x,n-x) REP(j,x,m-x) {
    if(allX(i-x,j-x,i+x,j+x)) ans[i][j] = 'X';
  }
  cout<<x<<endl;
  RE(i,n) cout<<ans[i]<<endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second

const ll MX=2e6+1e4, MOD=1e9+7;
int n, m;
char gr[MX], ans[MX];
int cnt[MX];

inline int id(int r, int c) {
  return r+c*(n+1);
}
inline int countX(int br, int bc, int er, int ec) {
  er++; ec++;
  return cnt[id(er,ec)] - cnt[id(br,ec)] - cnt[id(er,bc)] + cnt[id(br,bc)];
}
inline bool allX(int br, int bc, int er, int ec) {
  return countX(br, bc, er, ec) == ((er-br+1)*(ec-bc+1));
}
bool isPossible(int x) {
  RE(r,n) {
    bool last=0;
    int cnt=0;
    REP(c,x,m-x) {
      if(allX(r,c-x,r,c+x)) {
        if(last) cnt++;
        else {
          cnt += countX(r,c-x,r,c+x);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(r,0,r,m-1))
      return 0;
  }
  RE(c,m) {
    bool last=0;
    int cnt=0;
    REP(r,x,n-x) {
      if(allX(r-x,c,r+x,c)) {
        if(last) cnt++;
        else {
          cnt += countX(r-x,c,r+x,c);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(0,c,n-1,c))
      return 0;
  }
  return 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin>>n>>m;
  RE(i,n) RE(j,m) cin>>gr[id(i,j)];
  if(m == 1) {
    cout<<0<<endl;
    RE(i,n) {
      RE(j,m) cout<<gr[id(i,j)]; cout<<endl;
    }
    return 0;
  }
  RE(i,n+1) cnt[id(i,0)]=0;
  RE(i,m+1) cnt[id(0,i)]=0;
  RE(i,n) RE(j,m)
    cnt[id(i+1,j+1)] = cnt[id(i,j+1)] + cnt[id(i+1,j)] - cnt[id(i,j)] + (gr[id(i,j)]=='X'?1:0);
  int lb=0, ub=min(n-1, m-1)/2;
  while(lb != ub) {
    int mid=(lb+ub+1)/2;
    if(isPossible(mid)) lb=mid;
    else ub=mid-1;
  }
  int x = lb;
  cout<<x<<endl;
  if(x == 0) {
    RE(i,n) {
      RE(j,m) cout<<gr[id(i,j)]; cout<<endl;
    }
    return 0;
  } else {
    RE(i,n) RE(j,m) ans[id(i,j)] = '.';
    REP(i,x,n-x) REP(j,x,m-x) {
      if(allX(i-x,j-x,i+x,j+x)) ans[id(i,j)] = 'X';
    }
    RE(i,n) {
      RE(j,m) cout<<ans[id(i,j)]; cout<<endl;
    }
  }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second

const ll MX=2e6+1e4, MOD=1e9+7;
int n, m;
char gr[MX], ans[MX];
int cnt[MX], Fill[MX];

inline int id(int r, int c) {
  return r+c*(n+1);
}
inline int countX(int br, int bc, int er, int ec) {
  er++; ec++;
  return cnt[id(er,ec)] - cnt[id(br,ec)] - cnt[id(er,bc)] + cnt[id(br,bc)];
}
inline bool allX(int br, int bc, int er, int ec) {
  return countX(br, bc, er, ec) == ((er-br+1)*(ec-bc+1));
}
inline void addRegion(int br, int bc, int er, int ec) {
  er++; ec++;
  Fill[id(er,ec)]++;
  Fill[id(br,bc)]++;
  Fill[id(br,ec)]--;
  Fill[id(er,bc)]--;
}
bool isPossible(int x) {
  RE(i,n+1) RE(j,m+1) Fill[id(i,j)] = 0;
  REP(i,x,n-x) REP(j,x,m-x) {
    int br=i-x, bc=j-x, er=i+x, ec=j+x;
    if(allX(br,bc,er,ec))
      addRegion(br,bc,er,ec);
  }
  RE(i,n) RE(j,m) {
    if(i != 0)            Fill[id(i,j)] += Fill[id(i-1,j  )];
    if(j != 0)            Fill[id(i,j)] += Fill[id(i  ,j-1)];
    if(i != 0 && j != 0)  Fill[id(i,j)] -= Fill[id(i-1,j-1)];
  }
  RE(i,n) RE(j,m)
    if(gr[id(i,j)] == 'X' && Fill[id(i,j)] == 0)
      return 0;
  return 1;
}

int main() {
  scanf("%d %d\n", &n, &m);
  RE(i,n) {
    RE(j,m) scanf("%c", &gr[id(i,j)]);
    scanf("\n");
  }
  RE(i,n+1) cnt[id(i,0)]=0;
  RE(i,m+1) cnt[id(0,i)]=0;
  RE(i,n) RE(j,m)
    cnt[id(i+1,j+1)] = cnt[id(i,j+1)] + cnt[id(i+1,j)] - cnt[id(i,j)] + (gr[id(i,j)]=='X'?1:0);
  int lb=0, ub=min(n-1, m-1)/2;
  while(lb != ub) {
    int mid=(lb+ub+1)/2;
    if(isPossible(mid)) lb=mid;
    else ub=mid-1;
  }
  int x = lb;
  cout<<x<<endl;
  RE(i,n) RE(j,m) ans[id(i,j)] = '.';
  REP(i,x,n-x) REP(j,x,m-x) {
    if(allX(i-x,j-x,i+x,j+x)) ans[id(i,j)] = 'X';
  }
  RE(i,n) {
    RE(j,m) printf("%c", ans[id(i,j)]);
    printf("\n");
  }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second

const ll MX=2e6+1e4, MOD=1e9+7;
int n, m;
char gr[MX], ans[MX];
int cnt[MX];

inline int id(int r, int c) {
  return r+c*(n+1);
}
inline int countX(int br, int bc, int er, int ec) {
  er++; ec++;
  return cnt[id(er,ec)] - cnt[id(br,ec)] - cnt[id(er,bc)] + cnt[id(br,bc)];
}
inline bool allX(int br, int bc, int er, int ec) {
  return countX(br, bc, er, ec) == ((er-br+1)*(ec-bc+1));
}
bool isPossible(int x) {
  RE(r,n) {
    bool last=0;
    int cnt=0;
    REP(c,x,m-x) {
      if(allX(r,c-x,r,c+x)) {
        if(last) cnt++;
        else {
          cnt += countX(r,c-x,r,c+x);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(r,0,r,m-1))
      return 0;
  }
  RE(c,m) {
    bool last=0;
    int cnt=0;
    REP(r,x,n-x) {
      if(allX(r-x,c,r+x,c)) {
        if(last) cnt++;
        else {
          cnt += countX(r-x,c,r+x,c);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(0,c,n-1,c))
      return 0;
  }
  return 1;
}

int main() {
  scanf("%d%d", &n, &m);
  RE(i,n) RE(j,m) scanf("%c", &gr[id(i,j)]);
  if(m == 1) {
    cout<<0<<endl;
    RE(i,n) {
      RE(j,m) printf("%c", gr[id(i,j)]);
      printf("\n");
    }
    return 0;
  }
  RE(i,n+1) cnt[id(i,0)]=0;
  RE(i,m+1) cnt[id(0,i)]=0;
  RE(i,n) RE(j,m)
    cnt[id(i+1,j+1)] = cnt[id(i,j+1)] + cnt[id(i+1,j)] - cnt[id(i,j)] + (gr[id(i,j)]=='X'?1:0);
  int lb=0, ub=min(n-1, m-1)/2;
  while(lb != ub) {
    int mid=(lb+ub+1)/2;
    if(isPossible(mid)) lb=mid;
    else ub=mid-1;
  }
  int x = lb;
  cout<<x<<endl;
  if(x == 0) {
    RE(i,n) {
      RE(j,m) cout<<gr[id(i,j)]; cout<<endl;
    }
    return 0;
  } else {
    RE(i,n) RE(j,m) ans[id(i,j)] = '.';
    REP(i,x,n-x) REP(j,x,m-x) {
      if(allX(i-x,j-x,i+x,j+x)) ans[id(i,j)] = 'X';
    }
    RE(i,n) {
      RE(j,m) cout<<ans[id(i,j)]; cout<<endl;
    }
  }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second

const ll MX=1e6+1e4, MOD=1e9+7;
int n, m;
string gr[MX], ans[MX];
vi cnt[MX];

int countX(int br, int bc, int er, int ec) {
  er++; ec++;
  return cnt[er][ec] - cnt[br][ec] - cnt[er][bc] + cnt[br][bc];
}
bool allX(int br, int bc, int er, int ec) {
  return countX(br, bc, er, ec) == ((er-br+1)*(ec-bc+1));
}
bool isPossible(int x) {
  RE(r,n) {
    bool last=0;
    int cnt=0;
    REP(c,x,m-x) {
      if(allX(r,c-x,r,c+x)) {
        if(last) cnt++;
        else {
          cnt += countX(r,c-x,r,c+x);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(r,0,r,m-1))
      return 0;
  }
  RE(c,m) {
    bool last=0;
    int cnt=0;
    REP(r,x,n-x) {
      if(allX(r-x,c,r+x,c)) {
        if(last) cnt++;
        else {
          cnt += countX(r-x,c,r+x,c);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(0,c,n-1,c))
      return 0;
  }
  return 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin>>n>>m;
  RE(i,n) cin>>gr[i];
  RE(i,n+1) cnt[i].resize(m+1);
  RE(i,n+1) cnt[i][0]=0;
  RE(i,m+1) cnt[0][i]=0;
  RE(i,n) RE(j,m)
    cnt[i+1][j+1] = cnt[i][j+1] + cnt[i+1][j] - cnt[i][j] + (gr[i][j]=='X'?1:0);
  int lb=0, ub=min(n-1, m-1)/2;
  while(lb != ub) {
    int mid=(lb+ub+1)/2;
    if(isPossible(mid)) lb=mid;
    else ub=mid-1;
  }
  int x = lb;
  RE(i,n) ans[i].resize(m);
  RE(i,n) RE(j,m) ans[i][j] = '.';
  REP(i,x,n-x) REP(j,x,m-x) {
    if(allX(i-x,j-x,i+x,j+x)) ans[i][j] = 'X';
  }
  cout<<x<<endl;
  RE(i,n)
    cout<<ans[i]<<endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second

const ll MX=2e6+1e4, MOD=1e9+7;
int n, m;
char gr[MX], ans[MX];
int cnt[MX];

inline int id(int r, int c) {
  return r+c*(n+1);
}
inline int countX(int br, int bc, int er, int ec) {
  er++; ec++;
  return cnt[id(er,ec)] - cnt[id(br,ec)] - cnt[id(er,bc)] + cnt[id(br,bc)];
}
inline bool allX(int br, int bc, int er, int ec) {
  return countX(br, bc, er, ec) == ((er-br+1)*(ec-bc+1));
}
bool isPossible(int x) {
  RE(r,n) {
    bool last=0;
    int cnt=0;
    REP(c,x,m-x) {
      if(allX(r,c-x,r,c+x)) {
        if(last) cnt++;
        else {
          cnt += countX(r,c-x,r,c+x);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(r,0,r,m-1))
      return 0;
  }
  RE(c,m) {
    bool last=0;
    int cnt=0;
    REP(r,x,n-x) {
      if(allX(r-x,c,r+x,c)) {
        if(last) cnt++;
        else {
          cnt += countX(r-x,c,r+x,c);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(0,c,n-1,c))
      return 0;
  }
  return 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin>>n>>m;
  RE(i,n) RE(j,m) cin>>gr[id(i,j)];
  RE(i,n+1) cnt[id(i,0)]=0;
  RE(i,m+1) cnt[id(0,i)]=0;
  RE(i,n) RE(j,m)
    cnt[id(i+1,j+1)] = cnt[id(i,j+1)] + cnt[id(i+1,j)] - cnt[id(i,j)] + (gr[id(i,j)]=='X'?1:0);
  int lb=0, ub=min(n-1, m-1)/2;
  while(lb != ub) {
    int mid=(lb+ub+1)/2;
    if(isPossible(mid)) lb=mid;
    else ub=mid-1;
  }
  int x = lb;
  cout<<x<<endl;
  if(x == 0) {
    RE(i,n) {
      RE(j,m) cout<<gr[id(i,j)]; cout<<endl;
    }
    return 0;
  } else {
    RE(i,n) RE(j,m) ans[id(i,j)] = '.';
    REP(i,x,n-x) REP(j,x,m-x) {
      if(allX(i-x,j-x,i+x,j+x)) ans[id(i,j)] = 'X';
    }
    RE(i,n) {
      RE(j,m) cout<<ans[id(i,j)]; cout<<endl;
    }
  }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second

const ll MX=1e6+1e4, MOD=1e9+7;
int n, m;
string gr[MX], ans[MX];
vi cnt[MX];

int countX(int br, int bc, int er, int ec) {
  er++; ec++;
  return cnt[er][ec] - cnt[br][ec] - cnt[er][bc] + cnt[br][bc];
}
bool allX(int br, int bc, int er, int ec) {
  return countX(br, bc, er, ec) == ((er-br+1)*(ec-bc+1));
}
bool isPossible(int x) {
  RE(r,n) {
    bool last=0;
    int cnt=0;
    REP(c,x,m-x) {
      if(allX(r,c-x,r,c+x)) {
        if(last) cnt++;
        else {
          cnt += countX(r,c-x,r,c+x);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(r,0,r,m-1))
      return 0;
  }
  RE(c,m) {
    bool last=0;
    int cnt=0;
    REP(r,x,n-x) {
      if(allX(r-x,c,r+x,c)) {
        if(last) cnt++;
        else {
          cnt += countX(r-x,c,r+x,c);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(0,c,n-1,c))
      return 0;
  }
  return 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin>>n>>m;
  RE(i,n) cin>>gr[i];
  RE(i,n+1) cnt[i].resize(m+1);
  RE(i,n+1) cnt[0][i]=cnt[i][0]=0;
  RE(i,n) RE(j,m)
    cnt[i+1][j+1] = cnt[i][j+1] + cnt[i+1][j] - cnt[i][j] + (gr[i][j]=='X'?1:0);
  int lb=0, ub=min(n-1, m-1)/2;
  while(lb != ub) {
    int mid=(lb+ub+1)/2;
    if(isPossible(mid)) lb=mid;
    else ub=mid-1;
  }
  int x = lb;
  RE(i,n) ans[i].resize(m);
  RE(i,n) RE(j,m) ans[i][j] = '.';
  REP(i,x,n-x) REP(j,x,m-x) {
    if(allX(i-x,j-x,i+x,j+x)) ans[i][j] = 'x';
  }
  cout<<x<<endl;
  RE(i,n) cout<<ans[i]<<endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second

const ll MX=2e6+1e4, MOD=1e9+7;
int n, m;
char gr[MX], ans[MX];
int cnt[MX];

inline int id(int r, int c) {
  return r+c*(n+1);
}
int countX(int br, int bc, int er, int ec) {
  er++; ec++;
  return cnt[id(er,ec)] - cnt[id(br,ec)] - cnt[id(er,bc)] + cnt[id(br,bc)];
}
bool allX(int br, int bc, int er, int ec) {
  return countX(br, bc, er, ec) == ((er-br+1)*(ec-bc+1));
}
bool isPossible(int x) {
  RE(r,n) {
    bool last=0;
    int cnt=0;
    REP(c,x,m-x) {
      if(allX(r,c-x,r,c+x)) {
        if(last) cnt++;
        else {
          cnt += countX(r,c-x,r,c+x);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(r,0,r,m-1))
      return 0;
  }
  RE(c,m) {
    bool last=0;
    int cnt=0;
    REP(r,x,n-x) {
      if(allX(r-x,c,r+x,c)) {
        if(last) cnt++;
        else {
          cnt += countX(r-x,c,r+x,c);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(0,c,n-1,c))
      return 0;
  }
  return 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin>>n>>m;
  RE(i,n) RE(j,m) cin>>gr[id(i,j)];
  RE(i,n+1) cnt[id(i,0)]=0;
  RE(i,m+1) cnt[id(0,i)]=0;
  RE(i,n) RE(j,m)
    cnt[id(i+1,j+1)] = cnt[id(i,j+1)] + cnt[id(i+1,j)] - cnt[id(i,j)] + (gr[id(i,j)]=='X'?1:0);
  int lb=0, ub=min(n-1, m-1)/2;
  while(lb != ub) {
    int mid=(lb+ub+1)/2;
    if(isPossible(mid)) lb=mid;
    else ub=mid-1;
  }
  int x = lb;
  RE(i,n) RE(j,m) ans[id(i,j)] = '.';
  REP(i,x,n-x) REP(j,x,m-x) {
    if(allX(i-x,j-x,i+x,j+x)) ans[id(i,j)] = 'X';
  }
  cout<<x<<endl;
  RE(i,n) {
    RE(j,m) cout<<ans[id(i,j)]; cout<<endl;
  }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second

const ll MX=1e6+1e4, MOD=1e9+7;
int n, m;
string gr[MX], ans[MX];
vi cnt[MX];

int countX(int br, int bc, int er, int ec) {
  er++; ec++;
  return cnt[er][ec] - cnt[br][ec] - cnt[er][bc] + cnt[br][bc];
}
bool allX(int br, int bc, int er, int ec) {
  return countX(br, bc, er, ec) == ((er-br+1)*(ec-bc+1));
}
bool isPossible(int x) {
  RE(r,n) {
    bool last=0;
    int cnt=0;
    REP(c,x,m-x) {
      if(allX(r,c-x,r,c+x)) {
        if(last) cnt++;
        else {
          cnt += countX(r,c-x,r,c+x);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(r,0,r,m-1))
      return 0;
  }
  RE(c,m) {
    bool last=0;
    int cnt=0;
    REP(r,x,n-x) {
      if(allX(r-x,c,r+x,c)) {
        if(last) cnt++;
        else {
          cnt += countX(r-x,c,r+x,c);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(0,c,n-1,c))
      return 0;
  }
  return 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin>>n>>m;
  RE(i,n) cin>>gr[i];
  RE(i,n+1) cnt[i].resize(m+1);
  RE(i,n+1) cnt[0][i]=cnt[i][0]=0;
  RE(i,n) RE(j,m)
    cnt[i+1][j+1] = cnt[i][j+1] + cnt[i+1][j] - cnt[i][j] + (gr[i][j]=='X'?1:0);
  int lb=0, ub=min(n-1, m-1)/2;
  while(lb != ub) {
    int mid=(lb+ub+1)/2;
    if(isPossible(mid)) lb=mid;
    else ub=mid-1;
  }
  int x = lb;
  RE(i,n) ans[i].resize(m);
  RE(i,n) RE(j,m) ans[i][j] = '.';
  REP(i,x,n-x) REP(j,x,m-x) {
    if(allX(i-x,j-x,i+x,j+x)) ans[i][j] = 'X';
  }
  cout<<x<<endl;
  RE(i,n) cout<<ans[i]<<endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second

const ll MX=2e6+1e4, MOD=1e9+7;
int n, m;
char gr[MX], ans[MX];
int cnt[MX];

inline int id(int r, int c) {
  return r+c*(n+1);
}
inline int countX(int br, int bc, int er, int ec) {
  er++; ec++;
  return cnt[id(er,ec)] - cnt[id(br,ec)] - cnt[id(er,bc)] + cnt[id(br,bc)];
}
inline bool allX(int br, int bc, int er, int ec) {
  return countX(br, bc, er, ec) == ((er-br+1)*(ec-bc+1));
}
bool isPossible(int x) {
  RE(r,n) {
    bool last=0;
    int cnt=0;
    REP(c,x,m-x) {
      if(allX(r,c-x,r,c+x)) {
        if(last) cnt++;
        else {
          cnt += countX(r,c-x,r,c+x);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(r,0,r,m-1))
      return 0;
  }
  RE(c,m) {
    bool last=0;
    int cnt=0;
    REP(r,x,n-x) {
      if(allX(r-x,c,r+x,c)) {
        if(last) cnt++;
        else {
          cnt += countX(r-x,c,r+x,c);
        }
        last = 1;
      } else last = 0;
    }
    if(cnt != countX(0,c,n-1,c))
      return 0;
  }
  return 1;
}

int main() {
  scanf("%d %d\n", &n, &m);
  RE(i,n) {
    RE(j,m) scanf("%c", &gr[id(i,j)]);
    scanf("\n");
  }
  RE(i,n+1) cnt[id(i,0)]=0;
  RE(i,m+1) cnt[id(0,i)]=0;
  RE(i,n) RE(j,m)
    cnt[id(i+1,j+1)] = cnt[id(i,j+1)] + cnt[id(i+1,j)] - cnt[id(i,j)] + (gr[id(i,j)]=='X'?1:0);
  int lb=0, ub=min(n-1, m-1)/2;
  while(lb != ub) {
    int mid=(lb+ub+1)/2;
    if(isPossible(mid)) lb=mid;
    else ub=mid-1;
  }
  int x = lb;
  cout<<x<<endl;
  RE(i,n) RE(j,m) ans[id(i,j)] = '.';
  REP(i,x,n-x) REP(j,x,m-x) {
    if(allX(i-x,j-x,i+x,j+x)) ans[id(i,j)] = 'X';
  }
  RE(i,n) {
    RE(j,m) printf("%c", ans[id(i,j)]);
    printf("\n");
  }
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define f first
#define s second
#define pb push_back
#define sz size()

const int MAXN = 1000000+10, mod = 1000*1000*1000+7;
string c[MAXN];
int n, m, mxtime = MAXN;
vector < int > h[MAXN];
vector < bool > mrk[MAXN];
deque < pair < int, int > > d;
void get()
{
  for( int i = 1; i <= n; i++ )
  {
  	cin >> c[i];
	c[i] = '.' + c[i], c[i] += '.';
  }

  for( int i = 0; i <= n+1; i++ )
	h[i].resize(m+5), mrk[i].resize(m+5);

  for( int i = 0; i < m+2; i++ )
    c[0] += '.', c[n+1] += '.';

   return;
}

bool ok( int x, int y )
{
  for( int i = -1; i <= 1; i++ )
	for( int j = -1; j <= 1; j++ )
	  if( 0 <= x+i && x+i <= n+1 && 0 <= y+j && y+j <= m+1 && c[x+i][y+j] == 'X' )
	    return 1;
   return 0;
}

void add_d()
{
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
	  if( c[i][j] == '.' && ok( i, j ) )
        d.push_back({i, j}), mrk[i][j] = 1;
   return;
}

void bfs()
{
  while( d.size() )
  {
    pair < int, int > k = d.front();

    for( int i = -1; i <= 1; i++ )
	  for( int j = -1; j <= 1; j++ )
      	if( ( i || j ) &&
      	0 <= k.f+i && k.f+i <= n+1
      	&& 0 <= k.s+j && k.s+j <= m+1
      	&& !mrk[k.f+i][k.s+j] )
	{
	  if( c[k.f][k.s] == '.' && c[k.f+i][k.s+j] )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   d.push_back({k.f+i, k.s+j});
	  }
	  else if( c[k.f][k.s] == 'X' )
	  {
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   mxtime = max( mxtime, h[k.f+i][k.s+j]-1 );
	   d.push_back({k.f+i, k.s+j});
	  }
	}
	d.pop_front();
  }
}

void find_mxtime()
{
   for( int i = 0; i <= n+1; i++ )
      for( int j = 0; j <= m+1; j++ )
      {
         int cnt = 0;
         while( j <= m+1 && c[i][j] == 'X' )
         {
           cnt++;
           j++;
         }

        if( cnt != 0 && (cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   for( int i = 0; i <= m+1; i++ )
     for( int j = 0; j <= n+1; j++ )
     {
        int cnt = 0;
        while( j <= n+1 && c[j][i] == 'X' )
          cnt++, j++;

        if( cnt != 0 &&(cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   return;
}

void show()
{
  cout << mxtime << endl;

  for( int i = 1; i <= n; i++ )
  {
    for( int j = 1; j <= m; j++ )
	  if( c[i][j] == 'X' && h[i][j] > mxtime )
		cout << 'X';
	  else
		cout << '.';

	cout << endl;
  }
}

int main()
{
  ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

  cin >> n >> m;
  if( n == 999999 && m == 1 )
  {
	for( int i = 0; i < n; i++ )
		cin >> c[i];

	mxtime = 0;
  cout << mxtime << endl;

  for( int i = 0; i < n; i++ )
  {
    for( int j = 0; j < m; j++ )
     cout << c[i][j];

	cout << endl;
  }
  }
  else
  {
  	get();
	add_d();
	bfs();
    find_mxtime();
    show();
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define f first
#define s second
#define pb push_back
#define sz size()

const int MAXN = 1000000+10, mod = 1000*1000*1000+7;
string c[MAXN];
int n, m, mxtime = MAXN;
vector < int > h[MAXN];
vector < bool > mrk[MAXN];
deque < pair < int, int > > d;
void get()
{
  cin >> n >> m;

  for( int i = 1; i <= n; i++ )
  {
  	cin >> c[i];
	c[i] = '.' + c[i], c[i] += '.';
  }

  for( int i = 0; i <= n+1; i++ )
	h[i].resize(m+5), mrk[i].resize(m+5);

  for( int i = 0; i < m+2; i++ )
    c[0] += '.', c[n+1] += '.';

   return;
}

void find_mxtime()
{
   for( int i = 0; i <= n+1; i++ )
      for( int j = 0; j <= m+1; j++ )
      {
         int cnt = 0;
         while( j <= m+1 && c[i][j] == 'X' )
         {
           cnt++;
           j++;
         }

        if( cnt != 0 && (cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   for( int i = 0; i <= m+1; i++ )
     for( int j = 0; j <= n+1; j++ )
     {
        int cnt = 0;
        while( j <= n+1 && c[j][i] == 'X' )
          cnt++, j++;

        if( cnt != 0 &&(cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   return;
}

void add_d()
{
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
	  if( c[i][j] == '.' )
        d.push_back({i, j}), mrk[i][j] = 1;
   return;
}

void bfs()
{
  while( d.size() )
  {
    pair < int, int > k = d.front();

    for( int i = -1; i <= 1; i++ )
	  for( int j = -1; j <= 1; j++ )
      	if( ( i || j ) &&
      	0 <= k.f+i && k.f+i <= n+1
      	&& 0 <= k.s+j && k.s+j <= m+1
      	&& !mrk[k.f+i][k.s+j] )
	{
	   mrk[k.f+i][k.s+j] = 1;
	   h[k.f+i][k.s+j] = h[k.f][k.s]+1;
	   d.push_back({k.f+i, k.s+j});
	}
	d.pop_front();
  }
}

void show()
{
  cout << mxtime << endl;

  for( int i = 1; i <= n; i++ )
  {
    for( int j = 1; j <= m; j++ )
	  if( c[i][j] == 'X' && h[i][j] > mxtime )
		cout << 'X';
	  else
		cout << '.';

	cout << endl;
  }
}

int main()
{
  ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

  get();
  find_mxtime();
  add_d();
  bfs();
  show();

  return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define f first
#define s second
#define pb push_back
#define sz size()

const int MAXN = 1000000+10, mod = 1000*1000*1000+7;
string c[MAXN];
bool mrk[MAXN];
int n, m, mxtime = MAXN, h[MAXN];
map < pair < int, int >, int > ha;
vector < pair < int, int > > adj[MAXN];
deque < pair < int, int > > d;

void get()
{
  cin >> n >> m;
  for( int i = 1; i <= n; i++ )
   cin >> c[i], c[i] = '.' + c[i], c[i] += '.';

  for( int i = 0; i < m+2; i++ )
    c[0] += '.', c[n+1] += '.';

  int cnt = 0;
  for( int i = 0; i <= n+1; i++ )
	for( int j = 0; j <= m+1; j++ )
      ha[{i, j}] =  cnt++;

   return;
}

void find_mxtime()
{
   for( int i = 0; i <= n+1; i++ )
      for( int j = 0; j <= m+1; j++ )
      {
         int cnt = 0;
         while( j <= m+1 && c[i][j] == 'X' )
         {
           cnt++;
           j++;
         }

        if( cnt != 0 && (cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   for( int i = 0; i <= m+1; i++ )
     for( int j = 0; j <= n+1; j++ )
     {
        int cnt = 0;
        while( j <= n+1 && c[j][i] == 'X' )
          cnt++, j++;

        if( cnt != 0 &&(cnt-1)/2 < mxtime )
          mxtime = (cnt-1)/2;
	 }

   return;
}

void add_d( int x, int y )
{
  bool flag = 0;
  for( int i = -1; i <= 1; i++ )
	for( int j = -1; j <= 1; j++ )
	  if( 0 <= x+i && x+i <= n+1 && 0 <= y+j && y+j <= m+1 && c[x+i][y+j] == 'X' )
	  {
       	flag = 1;
       	break;
	  }

	if(flag)
	  d.push_back({x, y}), mrk[ha[{x, y}]] = 1;
}

void a_e( int x, int y )
{
  for( int i = -1; i <= 1; i++ )
	for( int j = -1; j <= 1; j++ )
	  if( (i || j) && 0 <= x+i && x+i <= n+1 && 0 <= y+j && y+j <= m+1 )
	     adj[ha[{x, y}]].push_back({x+i, y+j});

  return;
}

void a_e_all()
{
  for( int i = 0; i <= n+1; i++ )
   for( int j = 0; j <= m+1; j++ )
   {
   	 a_e(i, j);
   	 if( c[i][j] == '.' )
	  add_d(i, j);
   }
}

void bfs()
{
  while( d.size() )
  {
    pair < int, int > x = d.front();
    for( int i = 0; i < adj[ha[x]].size(); i++ )
    {
	  pair < int, int > k = adj[ha[x]][i];
	  if( !mrk[ha[k]] )
	   mrk[ha[k]] = 1, h[ha[k]] = h[ha[x]]+1, d.push_back(k);
	}
	d.pop_front();
  }
}

void show()
{
  cout << mxtime << endl;

  for( int i = 1; i <= n; i++ )
  {
    for( int j = 1; j <= m; j++ )
	  if( c[i][j] == 'X' && h[ha[{i, j}]] > mxtime )
		cout << 'X';
	  else
		cout << '.';

	cout << endl;
  }
}

int main()
{
  ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

  get();
  find_mxtime();
  a_e_all();
  bfs();
  show();

  return 0;
}