#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <assert.h>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

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 (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 2011;


int n;
char s[25][100];
int inv[2020];

ll mod_pow(ll a,ll b){
	if(b==0)return 1;
	ll res=mod_pow((a*a)%MOD,b/2);
	if(b&1)res=(res*a)%MOD;
	return res;
}


int expr(int &p, int u, int d, int r);

int primary(int &p, int base, int u, int d, int r){
	if(isdigit(s[base][p])){
		return s[base][p++] - '0';
	}else if(s[base][p] == '('){
		p+=2;
		int res = expr(p, u, d, r);
		if(s[base][p] != '.' || s[base][p+1] != ')') assert(false);
		p+= 2;
		return res;
	}else assert(false);
}

int fact(int &p, int base, int u, int d, int r){
	if(s[base][p] == '-'){
		if(s[base][p+1] == '.'){
			p+=2;
			return (-fact(p, base, u, d, r) + MOD) % MOD;
		}else{
			int p1 = p+1, p2 = p+1;
			while(s[base][p] == '-') ++ p;
			return expr(p1, u, base, p-1) * inv[expr(p2, base+1, d, p-1)] % MOD;
		}
	}else if(isdigit(s[base][p]) || s[base][p] == '('){
		int res = primary(p, base, u, d, r);
		if(base - 1 >= u && isdigit(s[base-1][p]))
			res = mod_pow(res, s[base-1][p++]-'0');
		return res;
	}
	assert(false);
}

int term(int &p, int base, int u, int d, int r){
	int res = fact(p, base, u, d, r);
	while(1){
		if(p+1 < r && s[base][p+1] == '*'){
			p+=3;
			res = res * fact(p, base, u, d, r) % MOD;
		}else break;
	}
	return res;
}

int expr(int &p, int u, int d, int r){
	int base = -1;
	while(1){
		for(int i=u;i<d;i++) if(s[i][p] != '.') base = i;
		if(base!=-1) break;
		p ++;
	}
	int res = term(p, base, u, d, r);
	while(1){
		if(p+1 < r && s[base][p+1] == '+'){
			p+=3;
			res = (res + term(p, base, u, d, r)) % MOD;
		}else if(p+1 < r && s[base][p+1] == '-'){
			p+=3;
			res = (MOD + res - term(p, base, u, d, r)) % MOD;
		}else{
			break;
		}
	}
	return res;
}

int main(){
	ios::sync_with_stdio(false);
	inv[1]=1;
	for(int i=2;i<MOD;i++) inv[i] = (MOD+((long long)-inv[MOD%i]*(MOD/i))%MOD)%MOD;

	while(cin >> n, n){
		memset(s, 0, sizeof(s));
		REP(i, n) cin >> s[i];
		int p = 0;
		cout << expr(p, 0, n, strlen(s[0])) << endl;
	}
	return 0;
}

Measured memory_bytes: 1216