#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

#define MAX 50
typedef pair<int,int> par;

int R, C, n;
char a[MAX][MAX+1];
vector<par> polja[9];

bool cmp( const vector<par> &A, const vector<par> &B ) { return A.size() < B.size(); }

void rec( int r, int c, vector<par> &p ) {
   if( r < 0 || r >= R ) return;
   if( c < 0 || c >= C ) return;
   if( a[r][c] == '0' ) return;
   a[r][c] = '0';
   p.push_back( par(r,c) );
   rec( r+1, c, p );
   rec( r-1, c, p );
   rec( r, c+1, p );
   rec( r, c-1, p );
}

int main( void ) {
   scanf( "%d%d", &R, &C );
   for( int r = 0; r < R; ++r ) scanf( "%s", a[r] );

   for( int r = 0; r < R; ++r ) 
      for( int c = 0; c < C; ++c ) 
         if( a[r][c] == '1' )
            rec( r, c, polja[n++] );

   sort( polja, polja+n, cmp );
   
   for( int i = 0; i < n; ++i ) 
      for( vector<par>::iterator it = polja[i].begin(); it != polja[i].end(); ++it ) 
         a[it->first][it->second] = '0' + i+1;

   for( int r = 0; r < R; ++r ) printf( "%s\n", a[r] );

   return 0;
}
