#include <algorithm>
#include <vector>
#include <string>

#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

const int MAX = 52;

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

int R, S;

char grid[ MAX ][ MAX ];
bool bio[ MAX ][ MAX ];

int cnt[ 10 ];
int boja[ 10 ];
int sorted[ 10 ];

void flood( int a, int b, int val )
{
    if( a < 0 || a >= R ) return;
    if( b < 0 || b >= S ) return;
    if( grid[a][b] == '0' ) return;

    if( bio[a][b] ) return;
    bio[a][b] = true;

    grid[a][b] = '0' + val + 1;
    ++cnt[val];

    for( int i = 0; i < 4; ++i )
        flood( a + dx[i], b + dy[i], val );
}

bool cmp( int a, int b ) { return cnt[a] < cnt[b]; }

int main( void )
{
    scanf( "%d %d", &R, &S );

    for( int i = 0; i < R; ++i )
        scanf( "%s", grid[i] );

    int n = 0;

    for( int i = 0; i < R; ++i )
        for( int j = 0; j < S; ++j )
            if( grid[i][j] != '0' && !bio[i][j] )
                flood( i, j, n++ );

    for( int i = 0; i < n; ++i )
        sorted[i] = i;

    sort( sorted, sorted + n, cmp );

    for( int i = 0; i < n; ++i )
        boja[ sorted[i] ] = i;

    for( int i = 0; i < R; ++i )
        for( int j = 0; j < S; ++j )
            if( grid[i][j] != '0' )
                grid[i][j] = '0' + boja[ grid[i][j]-1-'0' ] + 1;

    for( int i = 0; i < R; ++i )
        puts( grid[i] );

    return 0;
}

