#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

typedef int Int;
// a x + b y = gcd(a, b)
Int extgcd(Int a, Int b, Int &x, Int &y) {
  Int g = a; x = 1; y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}
Int invMod(Int a, Int m) {
  Int x, y;
  if (extgcd(a, m, x, y) == 1) return (x + m) % m;
  else                         return 0; // unsolvable
}

int mod(int a) {
  return (a%2011+2011)%2011;
}

int rec(vector<string> vs) {
  int h = vs.size();
  int w = vs[0].size();

  // REP(y, h)
  //   cout<< vs[y] << endl;
  // cout << endl;
  
  int base;                     
  int start;
  REP(x, w) {
    REP(y, h) {
      if (vs[y][x] != '.') {
        base = y;
        start = x;
        goto next;
      }
    }
  }
next:;
  char c = vs[base][start];

  int nest = 0;
  for (int x=w-1; x>=start; --x) {
    if (vs[base][x] == '(') nest++;
    if (vs[base][x] == ')') nest--;
    if (vs[base][x] == '+' && nest == 0) {
      vector<string> a,b;
      REP(i, vs.size()) {
        a.push_back(vs[i].substr(0, x-1));
        b.push_back(vs[i].substr(x+2));
      }
      return mod(rec(a) + rec(b));
    }
    if (x != start && vs[base][x] == '-' && vs[base][x+1] != '-' && vs[base][x-1] != '-' && nest == 0) {
      if ((x>=3 && vs[base][x-3] == '.' || x==2) && (vs[base][x-2] =='+' || vs[base][x-2] =='-' || vs[base][x-2] =='*')) continue;
      vector<string> a,b;
      REP(i, vs.size()) {
        a.push_back(vs[i].substr(0, x-1));
        b.push_back(vs[i].substr(x+2));
      }
      return mod(rec(a) - rec(b));
    }
  }
  nest = 0;
  for (int x=start; x<w; ++x) {
    if (vs[base][x] == '(') nest++;
    if (vs[base][x] == ')') nest--;
    if (vs[base][x] == '*' && nest == 0) {
      vector<string> a,b;
      REP(i, vs.size()) {
        a.push_back(vs[i].substr(0, x-1));
        b.push_back(vs[i].substr(x+2));
      }
      return mod(rec(a) * rec(b));
    }
  }
  
  if (c == '-' && vs[base][start+1] != '-') {               // unary
    // cout << "unary" << endl;
    vector<string> a;
    FOR(it, vs)
      a.push_back(it->substr(start+2));
    return mod(-rec(a));
  }

  if (c == '-' && vs[base][start+1] == '-') {               // fraction
    vector<string> a,b;
    REP(y, base) {
      a.push_back(vs[y]);
    }
    for (int y=base+1; y<h; ++y) {
      b.push_back(vs[y]);
    }
    int p = rec(a);
    int q = rec(b);
    // cout << p << " " << q << " " << invMod(q, 2011) << endl;
    // cout << p*invMod(q, 2011)%2011 << endl;
    return mod(p*invMod(q, 2011));
  }
    
  nest = 0;
  for (int x=start; x<w; ++x) {
    if (vs[base][x] == '(') nest++;
    if (vs[base][x] == ')') nest--;
    if (base&&isdigit(vs[base-1][x]) && nest == 0) {
      vector<string> a;
      REP(i, vs.size()) {
        a.push_back(vs[i].substr(0, x));
      }
      int res = rec(a);
      int ans = 1;
      REP(i, vs[base-1][x]-'0')
        ans = mod(ans*res);
      return ans;
    }
  }
  
  if (c == '(') {
    nest = 0;
    for (int x=start; x<w; ++x) {
      if (vs[base][x] == '(') nest++;
      if (vs[base][x] == ')') nest--;
      if (nest == 0) {
        vector<string> a;
        FOR(it, vs) {
          a.push_back(it->substr(start+1, x-start-1));
        }
        return rec(a);
      }
    }
  }

  return c - '0';
}

int main() {
  int n;
  while(cin>>n,n) {
    vector<string> input(n);
    REP(i,n)
      cin>>input[i];
    int res = rec(input);

    cout << res << endl;
  }
}

Measured memory_bytes: 0