#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int (i) = (0);(i) < (n);++i)
#define REV(i,n) for(int (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define ALL(v) v.begin(),v.end()
#define MOD 2011

const int PLUS = -1;
const int MINUS = -2;
const int MULTI = -3;

vector<string> mp(20);

int calPolynomial(int y1, int x1, int y2, int x2);

void DBGout(int y1, int x1, int y2, int x2){
    for(int y = y1; y <= y2;y++){
        for(int x = x1;x <= x2;x++){
            cerr << mp[y][x];
        }
        cerr << endl;
    }
    cerr << endl;
}

int mod_div(int a,int b){
    int tmp = MOD - 2,c = b,ret = 1;
    while(tmp > 0){
        if(tmp & 1){
            ret *= c;ret %= MOD;
        }
        c *= c;c %= MOD;tmp >>= 1;
    }
    return a*ret%MOD;
}

int mod_pow(int a,int b){
    int ret = 1;
    int c = a;
    for(int i = 0;i <= 60;i++){
        if(b & (1LL << i))ret = (ret * c) % MOD;
        c = (c * c) % MOD;
    }
    return ret;
}

char columnChar(int y, int x1, int x2) {
	int count = 0;
	char last_c = '.';
    for(int x = x1;x <= x2;x++){
        if(mp[y][x] != '.') {
			count++;
			last_c = mp[y][x];
		}
    }
	if(count > 1)last_c = '#';
    return last_c;
}

char rowChar(int y1, int y2, int x) {
	int count = 0;
	char last_c = '.';
    for(int y = y1;y <= y2;y++){
        if(mp[y][x] != '.') {
			count++;
			last_c = mp[y][x];
		}
    }
	if(count > 1)last_c = '#';
    return last_c;
}

void packSquare(int &y1, int &x1, int &y2, int &x2) {
    while(columnChar(y1, x1, x2) == '.')y1++;
    while(columnChar(y2, x1, x2) == '.')y2--;
    while(rowChar(y1, y2, x1) == '.')x1++;
    while(rowChar(y1, y2, x2) == '.')x2--;
}

int calMonomial(int y1, int x1, int y2, int x2){
    packSquare(y1, x1, y2, x2);
    //cerr << "calMonomial " << y1 << " " << x1 << " " << y2 << " " << x2 << endl;
    //DBGout(y1, x1, y2, x2);

    if(y1 == y2 && x1 == x2)return mp[y1][x1] - '0';
    for(int y = y1;y <= y2;y++){
        if(mp[y][x1] == '-'){
            return mod_div(calPolynomial(y1, x1, y-1, x2), calPolynomial(y + 1, x1, y2, x2));
        }
    }
    return mod_pow(mp[y2][x1] - '0', mp[y1][x2] - '0');
}

vector<int> uniqueNegative(vector<int> v) {
    vector<int> ret;
    REV(i, v.size()){
		if(v[i] == MINUS){
			ret.back() = MOD - ret.back();
			continue;
		}
		if(v[i] >= 0){
			if(!ret.empty() && ret.back() >= 0){
				ret.PB(PLUS);
			}
			ret.PB(v[i]);
			continue;
		}
		ret.PB(v[i]);
    }
	reverse(ALL(ret));
    return ret;
}

vector<int> multiplication(vector<int> v) {
    vector<int> ret;
    ret.PB(v[0]);
    for(int i = 1;i < v.size();i += 2){
        if(v[i] == MULTI){
            ret.back() = ret.back() * v[i+1] % MOD;
            continue;
        }

        ret.PB(v[i]);
        ret.PB(v[i+1]);
    }

    return ret;
}

int additionSubtraction(vector<int> v) {
     int ret = v[0];
     for(int i = 1;i < v.size();i += 2){
         if(v[i] == PLUS){
             ret = (ret + v[i+1]) % MOD;
         }
         if(v[i] == MINUS){
             ret = (ret + MOD - v[i+1]) % MOD;
         }
     }

     return ret;
}

int searchClose(int y1, int x1, int y2, int x2) {
    int num = 0;
    for(int x = x1;x <= x2;x++){
        char now = rowChar(y1, y2, x);

        if(now == '(')num++;
        if(now == ')')num--;

        if(num == 0){
            return x;
        }
    }
    return x2;
}

//　(y1,x1)から(y2,x2)で表される数式の値
int calPolynomial(int y1, int x1, int y2, int x2){
    packSquare(y1, x1, y2, x2);
    //cerr << "calPolynomial " << y1 << " " << x1 << " " << y2 << " " << x2 << endl;
    //DBGout(y1, x1, y2, x2);

    vector<int> v;
	int last_x = x1;
    for(int x = x1;x <= x2;x++){
		char now = rowChar(y1, y2, x);

		if(now == '#' || isdigit(now))continue;
		if(x < x2 && rowChar(y1, y2, x+1) == '#')continue;
		if(x > x1 && rowChar(y1, y2, x-1) == '#')continue;

		if(now == '.') {
			if(last_x != x){
                v.PB(calMonomial(y1, last_x, y2, x-1));
            }
            last_x = x+1;
            continue;
		}

        if(now == '('){
            int tmp_x = searchClose(y1, x, y2, x2);
            v.PB(calPolynomial(y1, x + 1, y2, tmp_x - 1));
            x = tmp_x;
			last_x = tmp_x + 1;
			if(x == x2)continue;
			char nxt_c = rowChar(y1, y2, x + 1);
			if(isdigit(nxt_c)){
				v.back() = mod_pow(v.back(), nxt_c - '0');
				x++;
				last_x++;
			}
			continue;
        }

        if(now == '+'){
            v.PB(PLUS);
        }
        if(now == '-'){
            v.PB(MINUS);
        }
        if(now == '*'){
            v.PB(MULTI);
        }

        last_x = x + 1;
    }
    if(last_x != x2 + 1)v.PB(calMonomial(y1, last_x, y2, x2));

    v = uniqueNegative(v);
    v = multiplication(v);
    return additionSubtraction(v);
}

int main(){
    cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);

    int n;
    while(cin >> n, n){
        REP(i, n){
            cin >> mp[i];
        }
        cout << calPolynomial(0, 0, n-1, mp[0].size() - 1) << endl;
    }

    return 0;
}



Measured memory_bytes: 3064