#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cassert>
using namespace std;

typedef pair<int,int> result;

result expression(int x,int y,int h,vector<string> &s);

result primary(int x,int y,int h,vector<string> &s) {
    // find base line
    int base = y;
    for(;base < y+h; ++base) if(s[base][x] != '.') break;

    // digit
    if(s[base][x] <= '9' && '0' <= s[base][x])
        return result(s[base][x]-'0',x+1);

    // paren
    if(s[base][x] == '(') {
        result r = expression(x+2,y,h,s);
        if(s[base][r.second+1] == ')') {
            r.second = r.second+2;
            return r;
        }
        assert(false);
        return r;
    }
    assert(false);

    result r;
    return r;
}

int powi(int x,int p) {
    if(p == 0) return 1;
    int ret = 1;
    for(int i=0; i<p; ++i) {
        ret *= x;
        ret %= 2011;
    }
    return ret;
}

result powexpr(int x,int y,int h,vector<string> &s) {
    result r = primary(x,y,h,s);
    for(int i=y; i<y+h; ++i) {
        if(s[i][r.second] == '^') {
            r.first = powi(r.first,s[i-1][r.second]-'0');
            r.second++;
            return r;
        }
    }
    return r;
}

result factor(int x,int y,int h,vector<string> &s) {
    int base = y;
    for(; base <y+h; ++base) if(s[base][x] != '.') break;

    if(s[base][x] == '-' && (x+1 < s[base].length() && s[base][x+1] == '-')) {
        // fraction
        result r1 = expression(x,y,base-y,s);
        result r2 = expression(x,base+1,h-(base-y)-1,s);

        int k=x;
        while(k < s[0].length() && s[base][k] == '-') k++;
        
        for(int i=1; i<2011; ++i)
            if((r2.first * i)%2011 == 1)
                return result((r1.first*i)%2011,k);
    }

    if(s[base][x] == '-') {
        result r1 = factor(x+2,y,h,s);
        r1.first *= -1;
        r1.first += 2011;
        r1.first %= 2011;
        return r1;
    }
    return powexpr(x,y,h,s);
}

result term(int x,int y,int h,vector<string> &s) {
    result r1 = factor(x,y,h,s);

    int base = y;
    for(; base<y+h; ++base) if(s[base][x] != '.') break;

    int k = r1.second;
    int ret = r1.first;
    while(true) {
        if(k == s[0].length()) return result(ret,k);
        if(s[base][k] == '.' && s[base][k+1] == '*') {
            result r2 = factor(k+3,y,h,s);
            ret = (ret*r2.first+2011)%2011;
            k = r2.second;
            continue;
        }
        return result(ret,k);
    }
}

result expression(int x,int y,int h,vector<string> &s) {
    int base = y;
    for(; base<y+h; ++base) if(s[base][x] != '.') break;
    if(base == y+h) return expression(x+1,y,h,s);

    result r1 = term(x,y,h,s);
    if(r1.second == s[0].length()) return r1;

    int k = r1.second;
    int ret = r1.first;
    while(true) {
        if(k == s[0].length()) return result(ret,k);
        if(s[base][k] == '.' && s[base][k+1] == '+'
           && s[base][k+2] == '.') {
            result r2 = term(k+3,y,h,s);
            ret += r2.first;
            k = r2.second;
        }else if(s[base][k] == '.' && s[base][k+1] == '-') {
            result r2 = term(k+3,y,h,s);
            ret -= r2.first;
            k = r2.second;
        }else return result(ret,k);
        ret = (ret+2011)%2011;
    }
}

int main() {
    int n;
    while(cin>>n, n) {
        vector<string> input(n, "");
        for(int i=0; i<n; ++i) cin>>input[i];

        // mask power
        for(int i=1; i<n; ++i) {
            for(int j=1; j<input[0].length(); ++j) {
                if(input[i][j-1] != '.' && input[i-1][j] != '.'
                   && input[i-1][j] != '-' && input[i][j] == '.')
                    input[i][j] = '^';
            }
        }

        result f = expression(0,0,n,input);
        cout<<f.first<<endl;
    }
}

Measured memory_bytes: 0