#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <set>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

//a x + b y = gcd(a, b)
long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long g = a; x = 1; y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

long long InvMod(long long a, long long mod) {
  long long x, y;
  if (extgcd(a, mod, x, y) == 1) { return (x + mod) % mod; }
  return 0;
}

ll powmod(ll base, ll power, ll mod) {
  base %= mod;
  ll ret = 1;
  while (power > 0) {
    if (power & 1) { ret = (ret * base) % mod; }
    base = (base * base) % mod;
    power >>= 1;
  }
  return ret;
}

//=============================================
char field[100][100];
const ll MOD = 2011;
int expr(int r, int t, int b, int &pos);

void skip(int r, int center, int &pos) {
  while (pos < r && field[center][pos] == '.') { pos++; }
}

int digit(int center, int &pos) {
  int lhs = atoi(field[center] + pos) % MOD;
  while (isdigit(field[center][pos])) { pos++; }
  return lhs;
}

int primary(int r, int t, int b, int center, int &pos) {
  int lhs = -1;
  if (field[center][pos] != '(') {
    lhs = digit(center, pos);
  } else {
    pos++;
    skip(r, center, pos);
    lhs = expr(r, t, b, pos);
    skip(r, center, pos);
    assert(field[center][pos] == ')');
    pos++;
  }
  return lhs;
}

int powexpr(int r, int t, int b, int center, int &pos) {
  int lhs = primary(r, t, b, center, pos);
  if (isdigit(field[center - 1][pos])) {
    int rhs = digit(center - 1, pos);
    lhs = powmod(lhs, rhs, MOD);
  }
  return lhs;
}

int fraction(int r, int t, int b, int center, int &pos) {
  int left = pos + 1;
  while (field[center][pos] == '-') { pos++; }
  int right = pos - 1;
  int npos1 = left;
  int npos2 = left;
  int lhs = expr(right, t, center, npos1);
  int rhs = expr(right, center + 1, b, npos2);
  lhs = (lhs * InvMod(rhs, MOD)) % MOD;
  return lhs;
}

int factor(int r, int t, int b, int center, int &pos) {
  if (field[center][pos] == '-' && field[center][pos + 1] == '-') {
    return fraction(r, t, b, center, pos);
  } else if (field[center][pos] == '-') {
    pos++;
    skip(r, center, pos);
    return (MOD - factor(r, t, b, center, pos)) % MOD;
  } else {
    return powexpr(r, t, b, center, pos);
  }
}

int term(int r, int t, int b, int center, int &pos) {
  int lhs = factor(r, t, b, center, pos);
  skip(r, center, pos);
  while (pos < r && field[center][pos] == '*') {
    pos++;
    skip(r, center, pos);
    int rhs = factor(r, t, b, center, pos);
    lhs = (lhs * rhs) % MOD;
    skip(r, center, pos);
  }
  return lhs;
}

int expr(int r, int t, int b, int &pos) {
  int center = -1;
  while (center == -1) {
    FOR(y, t, b) {
      if (field[y][pos] != '.') { center = y; break; }
    }
    if (center == -1) {
      pos++;
      assert(pos < r);
    }
  }
  int lhs = term(r, t, b, center, pos);
  skip(r, center, pos);
  while (pos < r && (field[center][pos] == '+' || field[center][pos] == '-')) {
    int s = 0;
    if (field[center][pos] == '+') { s = 1; }
    if (field[center][pos] == '-') { s = -1; }
    pos++;
    assert(field[center][pos] == '.');
    pos++;
    int rhs = term(r, t, b, center, pos);
    lhs = (lhs + s * rhs + MOD) % MOD;
    skip(r, center, pos);
  }
  return lhs;
}

int main() {
  int h;
  while (scanf("%d", &h), h) {
    MEMSET(field, '.');
    REP(y, h) {
      scanf("%s", field[y + 1]);
    }
    int w = strlen(field[0]);
    REP(y, h) { field[y][w] = '.'; }
    REP(y, h) { field[y][90] = 0; }
    w++;
    h += 2;
    int pos = 0;
    int ans = expr(w, 0, h, pos);
    cout << ans << endl;
  }
}