#include<bits/stdc++.h>
#define FOR(i,a,b) for(size_t i=(a);i<(size_t)(b);i++)
using namespace std;

const int mod = 2011;
vector<string> s;
vector<int> inv(mod,1);

inline void remove_space(size_t &l, size_t &t, size_t &r, size_t &b){
  for(bool f=true;l<r;l++){FOR(i,t,b)f &= (s[i][l]=='.');   if(!f)break;}
  for(bool f=true;l<r;r--){FOR(i,t,b)f &= (s[i][r-1]=='.'); if(!f)break;}
  for(bool f=true;t<b;t++){FOR(i,l,r)f &= (s[t][i]=='.');   if(!f)break;}
  for(bool f=true;t<b;b--){FOR(i,l,r)f &= (s[b-1][i]=='.'); if(!f)break;}
}

int expr(size_t &l, size_t &t, size_t &r, size_t &b);

int fraction(size_t &l, size_t &t, size_t &r, size_t &b){
  //cout << "fraction: " <<  l << " " << t << " " << r << " " << b << endl;
  size_t base = t;
  FOR(i,t,b)if(s[i][l]!='.')base = i;

  size_t ll = l+1, rr = l+1;
  while(++rr < r && s[base][rr]=='-');
  l = rr; rr--;

  size_t ul = ll, ur = rr, dl = ll, dr = rr;
  size_t ut = t, ub = base, dt = base+1, db = b;
  int A = expr(ul,ut,ur,ub), B = expr(dl,dt,dr,db);
  //cout << A << " " << B << endl;
  int tmp = A * inv[B] % mod; 
  //cout << tmp << endl;
  return tmp;
}

int primary(size_t &l, size_t &t, size_t &r, size_t &b){
  //cout << "primary: " <<  l << " " << t << " " << r << " " << b << endl;
  size_t base = t;
  FOR(i,t,b)if(s[i][l]!='.')base = i;
  if(isdigit(s[base][l]))return s[base][l++]-'0';

  l+=2;
  int res = expr(l,t,r,b);
  l+=2;
  return res;
}


int powexpr(size_t &l, size_t &t, size_t &r, size_t &b){
  //cout << "powexpr: " <<  l << " " << t << " " << r << " " << b << endl;
  int res = primary(l,t,r,b);
  size_t base = t;
  FOR(i,t,b)if(s[i][l]!='.')base = i;

  if(l<r && isdigit(s[base][l])){
    int p = s[base][l]-'0';
    l++;
    int tmp = res; res = 1;
    while(p--) (res *= tmp) %= mod;
  }
  return res;
}

int factor(size_t &l, size_t &t, size_t &r, size_t &b){
  //cout << "factor: " <<  l << " " << t << " " << r << " " << b << endl;
  //remove_space(l,t,r,b);
  size_t base = t;
  FOR(i,t,b)if(s[i][l]!='.')base = i;

  if(s[base][l] == '-'){
    if(s[base][l+1] == '.'){
      l+=2;
      return mod - factor(l,t,r,b);
    }else return fraction(l,t,r,b);
  }

  return powexpr(l,t,r,b);
}

int term(size_t &l, size_t &t, size_t &r, size_t &b){
  //cout << "term: " <<  l << " " << t << " " << r << " " << b << endl;
  //remove_space(l,t,r,b);
  size_t base = t;
  FOR(i,t,b)if(s[i][l]!='.')base = i;

  int res = factor(l,t,r,b);
  //cout << "term " << l << endl;
  while(l+1<r && s[base][l+1]=='*'){
    l+=3;
    res = (res * factor(l,t,r,b)) % mod;
    //cout << "term " << l << endl;
  }
  return res;
}

int expr(size_t &l, size_t &t, size_t &r, size_t &b){
  //cout << "expr: " <<  l << " " << t << " " << r << " " << b << endl;
  assert(l<r);
  remove_space(l,t,r,b);
  size_t base = t;
  FOR(i,t,b)if(s[i][l]!='.')base = i;

  int res = term(l,t,r,b);
  //cout << l << endl;
  while(l+1<r && (s[base][l+1]=='+' || s[base][l+1]=='-')){
    l+=3;
    if(s[base][l-2]=='+')res = (res + term(l,t,r,b)) % mod;
    else if(s[base][l-2]=='-')res = (res - term(l,t,r,b) + mod) % mod;
    //cout << l << endl;
  }
  return res;
}

int main(){
  cin.tie(); ios::sync_with_stdio(0);
  FOR(i,2,mod)inv[i] = (mod - mod/i) * inv[mod%i] % mod;
  //cout << inv[4] << endl;

  int n;
  while(cin>>n,n){
    s.resize(n);
    FOR(i,0,n)cin >> s[i];

    size_t l = 0, t = 0, r = s[0].size(), b = n;
    cout << expr(l,t,r,b) << endl;
  }
}

Measured memory_bytes: 1236