#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <queue>
#include <memory.h>
#include <stack>
#define mp make_pair
#define pb push_back                     

#if ( _WIN32 || __WIN32__ )
    #define LLD "%I64d"
#else
    #define LLD "%lld"
#endif

using namespace std;

typedef long long ll;
typedef long double ld;

char* s[110000];
int n,m;

int val[7][1100000];
int mod[7];
int cnt[7];
int first[10],last[10];

void error(bool q){
    if (!q){
        printf("-1\n");
        exit(0);
    }
}

void setval(int i,int j,int x){
    x = (x+10)%10;
    error(s[i][j] == '0'+x || s[i][j] == '*');
    s[i][j] = '0'+x;
}

void add(int x,int l,int r){
    l = (l+mod[x]*1000000LL)%mod[x];
    r = (r+mod[x]*1000000LL)%mod[x];
    val[x][l]++; val[x][r+1]--; cnt[x]++;
    if (l > r)
        val[x][0]++;
}

void solve(int id){
    int first = 0;
    for (; s[first][id] == '*'; ++first);


    for (int i = first+1; i < n; i++)
        setval(i,id,s[i-1][id]-'0' + (id == 0 || (s[i-1][id-1] > s[i][id-1])));
    for (int i = first-1; i >= 0 ; i--)
        setval(i,id,s[i+1][id]-'0' - (id == 0 || (s[i][id-1] > s[i+1][id-1])));
}

int main(){
  freopen("calendar.in","r",stdin);
  freopen("calendar.out","w",stdout);
  scanf("%d %d",&n,&m);

  for (int i = 0; i < n; i++){
    s[i] = new char[m+10];
    memset(s[i],0,sizeof(char)*(m+10));
    for (int j = 0; j < m; j++)
        scanf(" %c",&s[i][m-j-1]);
  }


  mod[0] = 10;
  for (int i = 1; i < 6; i++) mod[i] = mod[i-1]*10;
  mod[6] = mod[5];

  for (int i = 0; i < mod[6]; i++) val[6][i] = 1;


  for (int i = m-1; i >= 6; i--){   
     memset(first,-1,sizeof(first));
     memset(last,-1,sizeof(last));
     int cnt = 0;
     
     for (int j = 0; j < n; j++){
        if (s[j][i] != '*'){
            if (first[s[j][i] - '0'] == -1){
                first[s[j][i] - '0'] = j;
                cnt++;
            }
            last[s[j][i] - '0'] = j;
        }
     }
     error(cnt <= 2);

     if (cnt == 0){
         s[0][i] = '1';
         for (int j = 0; j < n; j++)
             val[6][j] = 1;
         continue;
     }

     if (cnt == 1){
         for (int j = 0; j < 10; j++)
             if (first[j] != -1){
                 for (int k = first[j]; k <= last[j]; k++)
                     setval(k,i,j);
                 for (int k = first[j]*(j!=0); k < n+(last[j]-n)*(j!=9); k++)
                     val[6][k] = 1;                                                  
                 for (int k = first[j]; k < last[j]; k++)
                     val[6][k] = 0;
             }
         continue;
     }

     int id = -1;
     for (int j = 0; j < 10; j++)
         if (first[j] != -1 && first[(j+1)%10] != -1)
             id = j;

     error(id != -1);

     int id1 = (id+1)%10;

     for (int j = 0; j <= last[id]; j++)
         setval(j,i,id),val[6][j] = 1;

     for (int j = first[id1]; j < n; j++)
         setval(j,i,id1),val[6][j] = 1;

     if (id != 9){         
         for (int j = 0; j < n; j++)
             val[6][j] = 1;
     }

     setval(first[id1],i-1,0);
     setval(last[id],i-1,9);                          
  }

  for (int j = 0; j < 6; j++){
    if (j < m){
        for (int k = 0; k < n; k++){
            if (s[k][j] == '*')
                continue;
            add(j,((s[k][j] - '0')*mod[j]/10)-k,((s[k][j] - '0'+1)*mod[j]/10-1)-k);
        }
    }
    for (int k = 1; k < mod[j]; k++)
        val[j][k] += val[j][k-1];
    for (int k = 0; k < mod[j]; k++)
        val[j][k] = (val[j][k] == cnt[j]);
  }

  reverse(val[6]+1,val[6]+mod[6]);

  for (int i = 0; i <= 1000000; i++){
    error(i < 1000000);
    bool ok = true;
    for (int j = 0; j < 7; j++)
        ok &= val[j][i%mod[j]];
    if (ok){
        for (int j = 0; j < m && j < 6; j++)
           setval(0,j,i%10), i/=10;
        break; 
    }
  }

  for (int i = 0; i < m; i++){
    solve(i);
  }

  reverse(s[0],s[0]+m);
  puts(s[0]);

  return 0;
}

