#include <algorithm>
#include <cctype>
#include <iostream>
#include <stdexcept>
#include <string>

using namespace std;

template <unsigned M>
class Mod {
    private:
        int x_;
        int f(int x) {
            return x < 0? (M - (-x) % M) % M: x % M;
        }
    public:
        Mod(): x_(0) {}
        Mod(int x): x_(f(x)) {}
        operator int() const {
            return x_;
        }
        Mod operator+() const {
            return *this;
        }
        Mod operator-() const {
            return Mod(-x_);
        }
        Mod operator+(const Mod &m) const {
            return Mod(x_ + m.x_);
        }
        Mod operator-(const Mod &m) const {
            return Mod(M - m.x_ + x_);
        }
        Mod operator*(const Mod &m) const {
            return Mod(x_ * m.x_);
        }
        Mod operator/(const Mod &m) const {
            return Mod(x_ * m.inv().x_);
        }
        Mod &operator+=(const Mod &m) {
            x_ = f(x_ + m.x_);
            return *this;
        }
        Mod &operator-=(const Mod &m) {
            x_ = f(x_ - m.x_);
            return *this;
        }
        Mod &operator*=(const Mod &m) {
            x_ = f(x_ * m.x_);
            return *this;
        }
        Mod &operator/=(const Mod &m) {
            x_ = f(x_ * m.inv().x_);
            return *this;
        }
        Mod inv() const {
            int a[3] = {0, 1, M};
            int b[3] = {1, 0, x_};
            while (b[2] > 0) {
                int r = a[2] / b[2];
                for (int i = 0; i < 3; ++i) {
                    a[i] = a[i] - b[i] * r;
                }
                swap(a, b);
            }
            if (a[2] != 1) {
                throw logic_error("no inverse");
            }
            return Mod(a[0]);
        }
        Mod pow(int p) const {
            Mod x = 1;
            Mod b = x_;
            while (p > 0) {
                if (p % 2 > 0) {
                    x *= b;
                }
                b *= b;
                p /= 2;
            }
            return x;
        }
};

typedef Mod<2011> M;

class Solver {
    private:
        unsigned find_base(unsigned &cur, int begin, int end) {
            while (cur < str[0].length()) {
                for (int i = begin; i < end; ++i) {
                    if (str[i][cur] != '.') {
                        return i;
                    }
                }
                ++cur;
            }
            throw logic_error("base is not found");
        }
        M digit(unsigned &cur, const unsigned begin, const unsigned base, const unsigned end) {
            ++cur;
            return M(str[base][cur - 1] - '0');
        }
        M fraction(unsigned &cur, const unsigned begin, const unsigned base, const unsigned end) {
            unsigned cur1 = cur + 1;
            unsigned cur2 = cur + 1;
            M m1 = expr(cur1, begin, find_base(cur1, begin, base), base);
            M m2 = expr(cur2, base + 1, find_base(cur2, base + 1, end), end);
            cur = max(cur1, cur2) + 1;
            return m1 / m2;
        }
        M primary(unsigned &cur, const unsigned begin, const unsigned base, const unsigned end) {
            M m;
            if (isdigit(str[base][cur])) {
                return digit(cur, base, base, base + 1);
            }
            cur += 2;
            m = expr(cur, begin, base, end);
            cur += 2;
            return m;
        }
        M powexpr(unsigned &cur, const unsigned begin, const unsigned base, const unsigned end) {
            M m = primary(cur, begin, base, end);
            if (base > 0 && isdigit(str[base - 1][cur])) {
                m = m.pow(digit(cur, base - 1, base - 1, base));
            }
            return m;
        }
        M factor(unsigned &cur, const unsigned begin, const unsigned base, const unsigned end) {
            M m;
            bool is_negative = false;
            while (str[base].substr(cur, 2) == "-.") {
                is_negative = !is_negative;
                cur += 2;
            }
            if (str[base][cur] == '-') {
                m = fraction(cur, begin, base, end);
            } else {
                m = powexpr(cur, begin, base, end);
            }
            return is_negative? -m: m;
        }
        M term(unsigned &cur, const unsigned begin, const unsigned base, const unsigned end) {
            M m = factor(cur, begin, base, end);
            while (true) {
                string op = str[base].substr(cur, 3);
                if (op == ".*.") {
                    cur += 3;
                    m *= factor(cur, begin, base, end);
                } else {
                    break;
                }
            }
            return m;
        }
        M expr(unsigned &cur, const unsigned begin, const unsigned base, const unsigned end) {
            M m = term(cur, begin, base, end);
            while (true) {
                string op = str[base].substr(cur, 3);
                if (op == ".+.") {
                    cur += 3;
                    m += term(cur, begin, base, end);
                } else if (op == ".-.") {
                    cur += 3;
                    m -= term(cur, begin, base, end);
                } else {
                    break;
                }
            }
            return m;
        }
    public:
        int n;
        string str[20];
        int solve() {
            unsigned cur = 0;
            return expr(cur, 0, find_base(cur, 0, n), n);
        }
};

int main() {
    Solver s;
    while (true) {
        cin >> s.n;
        if (s.n == 0) {
            return 0;
        }
        for (int i = 0; i < s.n; ++i) {
            cin >> s.str[i];
        }
        cout << s.solve() << endl;
    }
}

Measured memory_bytes: 1244