#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int mod = 2011;
int invtable[2011];

struct Area{
    int x[2];
    int y[2];
    Area(int x1, int x2, int y1, int y2){
        x[0]=x1; x[1]=x2; y[0]=y1; y[1]=y2;
    }
};

int normalize(int a){
    a %= mod;
    return (a+mod) %mod;
}

Area trim(Area a, vector<string> &s){
    int diff[2] = {1, -1};
    for(int i=0; i<2; i++){
        for(; ; a.x[i]+=diff[i]){
            bool empty = true;
            for(int y=a.y[0]; y<=a.y[1]; y++){
                if(s[y][a.x[i]] != '.') empty = false;
            }
            if(!empty) break;
        }
        for(; ; a.y[i]+=diff[i]){
            bool empty = true;
            for(int x=a.x[0]; x<=a.x[1]; x++){
                if(s[a.y[0]][x] != '.') empty = false;
            }
            if(!empty) break;
        }
    }
    return a;
}


int parse_expr(Area a, vector<string> &s);
int parse_term(Area a, vector<string> &s);

int parse_expr(Area a, vector<string> &s){
    int base = -1;
    for(int y=a.y[0]; y<=a.y[1]; y++){
        if(s[y][a.x[0]] != '.') base = y;
    }
    vector<int> operands;
    vector<char> operators;
    int sgn = 1;
    for(int x=a.x[0], prev=a.x[0], depth=0; x<=a.x[1]+1; x++){
        if(x==a.x[1]+1){
            auto ret = parse_term(trim(Area(prev, x-1, a.y[0], a.y[1]), s), s);
            operands.push_back((sgn*ret +mod) %mod);
        }else if(depth==0 and (x<=a.x[0] or s[base][x-1]=='.') and (x>=a.x[1] or s[base][x+1]=='.')
                and (s[base][x]=='+' or s[base][x]=='-' or s[base][x]=='*')){
            if(x == prev){
                sgn *= -1;
            }else{
                auto ret = parse_term(trim(Area(prev, x-2, a.y[0], a.y[1]), s), s);
                operands.push_back((sgn*ret +mod) %mod);
                operators.push_back(s[base][x]);
                sgn = 1;
            }
            prev = x+2;
        }else if(s[base][x] == '('){
            depth++;
        }else if(s[base][x] == ')'){
            depth--;
        }
    }
    
    {
        vector<int> new_operands;
        vector<char> new_operators;
        new_operands.push_back(operands[0]);
        for(int i=0; i<(int)operators.size(); i++){
            if(operators[i] == '*'){
                new_operands.back() *= operands[i+1];
                new_operands.back() %= mod;
            }else{
                new_operands.push_back(operands[i+1]);
                new_operators.push_back(operators[i]);
            }
        }
        swap(operators, new_operators);
        swap(operands, new_operands);
    }
    int res = operands[0];
    for(int i=0; i<(int)operators.size(); i++){
        if(operators[i] == '+'){
            res += operands[i+1];
        }else{
            res -= operands[i+1];
        }
    }
    return (res%mod +mod) %mod;
}

int parse_term(Area a, vector<string> &s){
    int base = -1;
    for(int y=a.y[0]; y<=a.y[1]; y++){
        if(s[y][a.x[0]] != '.') base = y;
    }
    if(s[base][a.x[0]] == '-'){
        int numerator = parse_expr(trim(Area(a.x[0], a.x[1], a.y[0], base-1), s), s);
        int denominator = parse_expr(trim(Area(a.x[0], a.x[1], base+1, a.y[1]), s), s);
        return (numerator *invtable[denominator]) %mod;
    }
    if(s[base][a.x[1]] == '.'){
        int p = s[base-1][a.x[1]] -'0';
        int term = parse_term(trim(Area(a.x[0], a.x[1]-1, a.y[0], a.y[1]), s), s);
        int res = 1;
        for(int i=0; i<p; i++){
            res *= term;
            res %= mod;
        }
        return res;
    }
    if(s[base][a.x[0]] == '('){
        return parse_expr(trim(Area(a.x[0]+1, a.x[1]-1, a.y[0], a.y[1]), s), s);
    }
    return s[base][a.x[0]] -'0';
}

int main(){
    for(int i=1; i<mod; i++){
        for(int j=1; j<mod; j++){
            if(i*j%mod == 1){
                invtable[i] = j;
            }
        }
    }
    while(1){
        int n;
        cin >> n;
        if(n == 0) break;

        vector<string> s(n);
        for(int i=0; i<n; i++){
            cin >> s[i];
        }
        cout << parse_expr(Area(0, (int)s[0].length()-1, 0, n-1), s) << endl;
    }
    return 0;
}


Measured memory_bytes: 3096