/* winners never quit and quitters never win.
      #swap                                 */
      
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize ("-O3")
using ll = long long;
using ld = long double;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;
const ll rk = 256;
const ld PI = 3.141592653589793;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

template <typename T>
void make_unique(vector<T>& vec) {
  sort(all(vec));
  vec.erase(unique(all(vec)), vec.end());
}

#define endl '\n'
#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll> 
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(x) (x).begin(), (x).end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


ll nxt() {
    ll TemporaryVariable;
    cin >> TemporaryVariable;
    return TemporaryVariable;
}

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

 
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    ll n=nxt(),m=nxt();
    string s[n];
    rep(a,0,n)
        cin>>s[a];
    ll ans=0;
    rep(a,0,n)
    {
        rep(b,0,m)
        {
            if(a<n-1 and s[a][b]==s[a+1][b] and s[a][b]=='.')
                ans++;
            if(b<m-1 and s[a][b]==s[a][b+1] and s[a][b]=='.')
                ans++;
        }
    }
    cout<<ans<<endl;
	printclock;
	return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <tuple>
#include <numeric>
#include <functional>
#include <fstream>
#include <iostream>
using namespace std; using ll = long long; using ld = long double;  using pll = pair<ll, ll>;
using vl = vector<ll>; using vll = vector<vl>; using vlll = vector<vll>; using vpll = vector<pll>;
using vs = vector<string>; using tll = tuple<ll, ll, ll>; using vtll = vector<tll>;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
const ld PI = 3.1415926535897932;
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define co(i) cout << i << endl;
void in() {}
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
	cerr << " " << h;
	if (sizeof...(t) > 0) cerr << " :";
	debug_out(t...);
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> vec) {
	for (size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
	return os;
}
ll ctoi(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2;
	case '3': return 3; case '4': return 4; case '5': return 5;
	case '6': return 6; case '7': return 7; case '8': return 8;
	case '9': return 9;  default: return 0;
	}
}
bool pairCompare(const pll firstElof, pll secondElof)
{
	return firstElof.second < secondElof.second;
}
//AtCoder以外ではコメントアウトを忘れずに！
//#include <atcoder/all>
//using namespace atcoder;
//**Snippetリスト**//
//rep, vin, all, iteLoop, bitSearch, bitList, nod, LIS, digitDP, treeDP, Rerooting//
//gcdlcm, isPrime, eratos, primeFactorize, Npow, combination, divisor, modinv, doubling//
//dfs, bfs, dijkstra, WarshallFloyd, BellmanFord, UnionFind, Kruskal, RMQ, LCA, FordFulkerson//
//**AC Libraryリスト**//
//pow_mod, inv_mod, floor_sum, convolution, convolution_ll, modint//
//fenwicktree, segtree, lazysegtree, string//
ll i, j, k, l, m; ll N, M, K, H, W, X, Y, Z, R, Q;
ll MOD = 1000000007, INF = 1LL << 60, ans = 0, z = 0, o = 1;
vll path;
//***********//
int main() {
	string S, T, U;
	cin >> S >> T >> U;
	N = S.length();
	M = T.length();
	ll L = U.length();
	map<char, ll> A;
	for (i = 0; i < N; i++) {
		A[S[i]] += 1;
	}
	for (i = 0; i < M; i++) {
		A[T[i]] += 1;
	}
	for (i = 0; i < L; i++) {
		A[U[i]] += 1;
	}
	if (A.size() > 10) {
		cout << "UNSOLVABLE" << endl;
		exit(0);
	}
	vector<char> B;
	for (auto x : A) {
		B.push_back(x.first);
	}
	while (true) {
		if (B.size() == 10) break;
		B.push_back('*');
	}
	//debug(B);
	sort(B.begin(), B.end());
	//reverse(S.begin(), S.end());
	//reverse(T.begin(), T.end());
	//reverse(U.begin(), U.end());
	do {
		map<char, ll> P;
		vl Q(26);
		//debug(B);
		for (i = 0; i < 10; i++) {
			if(B[i]!='*') Q[B[i]-'a'] = i;
		}
		ll D=0, E=0, F = 0;
		ll valid = 1;
		for (i = 0; i < N; i++) {
			D += Q[S[i]-'a'] * pow(10, N-i-1);
			if (i == 0 && Q[S[i] - 'a'] == 0) {
				valid = 0;
			}
		}
		for (i = 0; i < M; i++) {
			E += Q[T[i]-'a'] * pow(10, M-i-1);
			if (i == 0 && Q[T[i] - 'a'] == 0) {
				valid = 0;
			}
		}
		for (i = 0; i < L; i++) {
			F += Q[U[i]-'a'] * pow(10, L-i-1);
			if (i == 0 && Q[U[i]-'a'] == 0) {
				valid = 0;
			}
		}
		//debug(D, E, F);
		if (D + E == F && valid) {
			for (i = 0; i < N; i++) {
				cout << Q[S[i]-'a'];
			}
			cout << endl;
			for (i = 0; i < M; i++) {
				cout << Q[T[i]-'a'];
			}
			cout << endl;
			for (i = 0; i < L; i++) {
				cout << Q[U[i]-'a'];
			}
			cout << endl;
			exit(0);
		}
	} while (std::next_permutation(B.begin(), B.end()));
	cout << "UNSOLVABLE" << endl;
}