#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)

typedef long long ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vc> vvc;
typedef pair<int, int> pii;

struct Indices {
    int x, y;
    int lx, ly, rx, ry;
};

int expr(Indices& idx);

int W, H;
char field[20][80];

bool wrap(Indices& idx, int x, int y) {
    return idx.lx <= x && x <= idx.rx && idx.ly <= y && y <= idx.ry;
}

bool check2(int x, int y, char c) {
    if (0 <= x && x < W && 0 <= y && y < H) {
        if (field[y][x] == c) {
            return true;
        }
    }
    return false;
}

void skip(Indices& idx, char c) {
    assert(field[idx.y][idx.x] == c);
    idx.x++;
}

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

void set_base(Indices& idx) {
    vi candidate;
    int ma = 0;
    FOR(y, idx.ly, idx.ry+1) {
        int cnt = 0;
        FOR(x, idx.lx, idx.rx+1) {
            if (field[y][x] != '.') cnt++;
        }
        if (ma < cnt) {
            ma = cnt;
            candidate.clear();
        }
        if (ma == cnt) {
            candidate.push_back(y);
        }
    }
    int y = candidate[SZ(candidate)-1];
    idx.y = y;
}

int digit(Indices& idx) {
    assert(isdigit(field[idx.y][idx.x]));
    int n = field[idx.y][idx.x] - '0';
    idx.x++;
    return n;
}

int fraction(Indices& idx) {
    int width = 0;
    FOR(x, idx.x, W) {
        if (check2(x, idx.y, '-')) {
            width++;
        } else {
            break;
        }
    }

    Indices idx_top = idx;
    idx_top.lx = idx_top.x + 1;
    idx_top.rx = idx_top.x + width - 1;
    idx_top.ry = idx.y-1;
    set_base(idx_top);
    idx_top.x++;
    FOR(x, idx_top.x, idx_top.rx+1) {
        if (field[idx_top.y][x] == '.') {
            idx_top.x++;
        } else {
            break;
        }
    }
    Indices idx_bottom = idx;
    idx_bottom.lx = idx_bottom.x + 1;
    idx_bottom.rx = idx_bottom.x + width - 1;
    idx_bottom.ly = idx.y+1;
    set_base(idx_bottom);
    idx_bottom.x++;
    FOR(x, idx_bottom.x, idx_bottom.rx+1) {
        if (field[idx_bottom.y][x] == '.') {
            idx_bottom.x++;
        } else {
            break;
        }
    }

    idx.x += width;

//    LOG("top--> ");
//    REP(x, idx_top.rx+1) {
//        if (x < idx_top.lx) {
//            LOG(" ");
//        } else {
//            LOG("%c", field[idx_top.y][x]);
//        }
//    }
//    LOG("\n");
//    LOG("btm--> ");
//    REP(x, idx_bottom.rx+1) {
//        if (x < idx_bottom.lx) {
//            LOG(" ");
//        } else {
//            LOG("%c", field[idx_bottom.y][x]);
//        }
//    }
//    LOG("\n");
    int x = expr(idx_top);
    int y = expr(idx_bottom);
    int inv_y;
    REP(z, 2011) {
        if ((z * y) % 2011 == 1) {
            inv_y = z;
        }
    }
    return mod(x * inv_y);
}

int primary(Indices& idx) {
    if (check2(idx.x, idx.y, '(')) {
        skip(idx, '(');
        skip(idx, '.');
        int n = expr(idx);
        skip(idx, '.');
        skip(idx, ')');
        return n;
    } else {
        return digit(idx);
    }
}

int powexpr(Indices& idx) {
    int n = primary(idx);
    int y = idx.y-1;
    if (wrap(idx, idx.x, y) && isdigit(field[y][idx.x])) {
        Indices idx_in = idx;
        idx_in.y = y;
        int top = digit(idx_in);
        skip(idx, '.');
        if (top == 0) {
            n = 1;
        } else {
            int m = n;
            REP(i, top-1) {
                n = mod(n*m);
            }
        }
    }
    return n;
}

int factor(Indices& idx) {
    if (check2(idx.x, idx.y, '-')) {
        if (check2(idx.x+1, idx.y, '-')) {
            return fraction(idx);
        } else if (check2(idx.x+1, idx.y, '.')) {
            skip(idx, '-');
            skip(idx, '.');
            return mod(-factor(idx));
        }
    }
    return powexpr(idx);
}

int term(Indices& idx) {
    int n = factor(idx);
    while (1) {
        if (check2(idx.x+1, idx.y, '*')) {
            skip(idx, '.');
            skip(idx, '*');
            skip(idx, '.');
            n = mod(n * factor(idx));
        } else {
            break;
        }
    }
    return mod(n);
}

int expr(Indices& idx) {
    int n = term(idx);
    while (1) {
        if (check2(idx.x+1, idx.y, '+')) {
            skip(idx, '.');
            skip(idx, '+');
            skip(idx, '.');
            n += term(idx);
        } else if (check2(idx.x+1, idx.y, '-')) {
            skip(idx, '.');
            skip(idx, '-');
            skip(idx, '.');
            n = mod(n - term(idx));
        } else {
            break;
        }
    }
    return mod(n);
}

int main() {
    int n;
    while (cin >> n, n) {
        cin.ignore();
        string line;
        REP(y, n) {
            getline(cin, line);
            W = line.length();
            REP(x, W) {
                field[y][x] = line[x];
            }
        }
        H = n;
        Indices idx = {0,0,0,0,W-1,H-1};
        set_base(idx);
        cout << expr(idx) << endl;
    }
}

Measured memory_bytes: 3108