#include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

const int mod = 2011;

struct Mod {
  int num;
  Mod () : num(0) {;}
  Mod (int n) : num(n%mod) {;}
  operator int() { return num; }
};

Mod operator+(Mod a, Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator-(Mod a, Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator*(Mod a, Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator+=(Mod &a, Mod b) { return a = a + b; }
Mod operator-=(Mod &a, Mod b) { return a = a - b; }
Mod operator*=(Mod &a, Mod b) { return a = a * b; }
Mod operator^(Mod a, int n) {
  if (n == 0) return Mod(1);
  Mod res = (a * a) ^ (n / 2);
  if (n % 2) res = res * a;
  return res;
}
Mod inv(Mod a) { return a ^ (mod - 2); }
Mod operator/(Mod a, Mod b) { return a * inv(b); }

pair<int, int> get_mid(const vector<string> &s, int is, int it, int js, int jt) {
  for (int j=js; j<jt; ++j)
    for (int i=is; i<it; ++i)
      if (s[i][j] != '.')
        return make_pair(i, j);
  throw "bad";
}

pair<Mod, int> expr(const vector<string> &s, int is, int it, int js, int jt, int mid);

pair<Mod, int> primary(const vector<string> &s, int is, int it, int js, int jt, int mid) {
  //cerr<<"prim: "<<is<<' '<<mid<<' '<<js<<endl;
  if (isdigit(s[mid][js])) {
    //cerr<<"qqq"<<s[mid][js]<<endl;
    return make_pair(Mod(s[mid][js] - '0'), js + 1);
  } else {
    auto res = expr(s, is, it, js+2, jt, mid);
    res.second += 2;
    return res;
  }
}

pair<Mod, int> powexpr(const vector<string> &s, int is, int it, int js, int jt, int mid) {
  //cerr<<"powexpr: "<<is<<' '<<mid<<' '<<js<<endl;
  Mod res;
  tie(res, js) = primary(s, is, it, js, jt, mid);
  if (mid > 0 && js < jt && isdigit(s[mid-1][js])) {
    res = res ^ (s[mid-1][js] - '0');
    ++js;
  }
  return make_pair(res, js);
}

pair<Mod, int> factor(const vector<string> &s, int is, int it, int js, int jt, int mid) {
  //cerr<<"factor: "<<is<<' '<<mid<<' '<<js<<endl;
  if (s[mid][js] == '-') {
    if (s[mid][js+1] == '-') {
      int ed = js+1;
      while(ed < jt && s[mid][ed] == '-') ++ed;
      auto p1 = get_mid(s, is, mid, js, ed);
      auto p2 = get_mid(s, mid+1, it, js, ed);
      Mod m1 = expr(s, is, mid, p1.second, ed, p1.first).first;
      Mod m2 = expr(s, mid+1, it, p2.second, ed, p2.first).first;
      return make_pair(m1 / m2, ed);
    } else {
      auto p = factor(s, is, it, js+2, jt, mid);
      p.first = (int)mod-(int)p.first;
      return p;
    }
  } else {
    return powexpr(s, is, it, js, jt, mid);
  }
}

pair<Mod, int> term(const vector<string> &s, int is, int it, int js, int jt, int mid) {
  //cerr<<"term: "<<is<<' '<<mid<<' '<<js<<endl;
  Mod res;
  tie(res, js) = factor(s, is, it, js, jt, mid);
  while(js+1 < jt && s[mid][js+1] == '*') {
    ++js;
    auto rhs = factor(s, is, it, js + 2, jt, mid);
    res *= rhs.first;
    js = rhs.second;
  }
  return make_pair(res, js);
}

pair<Mod, int> expr(const vector<string> &s, int is, int it, int js, int jt, int mid) {
  //cerr<<"expr: "<<is<<' '<<mid<<' '<<js<<endl;
  Mod res;
  tie(res, js) = term(s, is, it, js, jt, mid);
  while(js+1 < jt && (s[mid][js+1] == '-' || s[mid][js+1] == '+')) {
    ++js;
    auto rhs = term(s, is, it, js + 2, jt, mid);
    if (s[mid][js] == '+') res += rhs.first;
    else res -= rhs.first;
    js = rhs.second;
  }
  return make_pair(res, js);
}

int main() {
  while(1){
    int n;
    cin>>n;
    if(!n)break;
    vector<string> s(n);
    REP(i,n)cin>>s[i];
    auto mid = get_mid(s, 0, n, 0, s[0].size());
    auto res = expr(s, 0, n, mid.second, s[0].size(), mid.first);
    cout << res.first << endl;
  }
  return 0;
}

Measured memory_bytes: 1216