#include <iostream>
#include <string>
#include <vector>

using namespace std;

static int INV[2011] = { 0 };

int mod(int x){
	while(x < 0){ x += 2011; }
	return x % 2011;
}

struct Result {
	int p, v;
};

#define PARSER_DECL(name) \
	Result name ( \
		const vector<string> &s, int left, int top, int right, int bottom)

PARSER_DECL(expr);
PARSER_DECL(term);
PARSER_DECL(factor);
PARSER_DECL(powexpr);
PARSER_DECL(primary);
PARSER_DECL(fraction);
PARSER_DECL(digit);

int calculateBase(
	const vector<string> &s, int left, int top, int right, int bottom)
{
	for(int i = left; i < right; ++i){
		for(int j = top; j < bottom; ++j){
			if(s[j][i] != '.'){ return j; }
		}
	}
	return -1;
}

PARSER_DECL(expr){
	int base = calculateBase(s, left, top, right, bottom);
	Result r = term(s, left, top, right, bottom);
	int next = r.p + 1;
	while(next < right && (s[base][next] == '+' || s[base][next] == '-')){
		r.p += 3;
		Result q = term(s, r.p, top, right, bottom);
		if(s[base][next] == '+'){
			r.v = mod(r.v + q.v);
		}else{
			r.v = mod(r.v - q.v);
		}
		r.p = q.p;
		next = q.p + 1;
	}
	return r;
}
PARSER_DECL(term){
	int base = calculateBase(s, left, top, right, bottom);
	Result r = factor(s, left, top, right, bottom);
	int next = r.p + 1;
	while(next < right && s[base][next] == '*'){
		r.p += 3;
		Result q = factor(s, r.p, top, right, bottom);
		r.v = mod(r.v * q.v);
		r.p = q.p;
		next = q.p + 1;
	}
	return r;
}
PARSER_DECL(factor){
	int base = calculateBase(s, left, top, right, bottom);
	int numNeg = 0, p = left;
	while(s[base][p] == '-' && s[base][p + 1] == '.'){
		++numNeg;
		p += 2;
	}
	if(s[base][p] == '-'){
		Result r = fraction(s, p, top, right, bottom);
		if(numNeg % 2 == 1){ r.v = mod(-r.v); }
		return r;
	}else{
		Result r = powexpr(s, p, top, right, bottom);
		if(numNeg % 2 == 1){ r.v = mod(-r.v); }
		return r;
	}
}
PARSER_DECL(powexpr){
	int base = calculateBase(s, left, top, right, bottom);
	Result r = primary(s, left, top, right, bottom);
	if(base > 0 && r.p < right && isdigit(s[base - 1][r.p])){
		Result q = digit(s, r.p, top, right, bottom);
		int v = 1;
		for(int i = 0; i < q.v; ++i){ v = mod(v * r.v); }
		r.v = v;
		r.p = q.p;
	}
	return r;
}
PARSER_DECL(primary){
	int base = calculateBase(s, left, top, right, bottom);
	if(isdigit(s[base][left])){
		Result r = digit(s, left, top, right, bottom);
		return r;
	}else{
		Result r = expr(s, left + 2, top, right, bottom);
		r.p += 2;
		return r;
	}
}
PARSER_DECL(fraction){
	int base = calculateBase(s, left, top, right, bottom);
	int length = 0;
	for(int i = left; i < right; ++i, ++length){
		if(s[base][i] != '-'){ break; }
	}
	int upperBegin = left;
	for(int i = 0; i < length; ++i, ++upperBegin){
		bool flag = false;
		for(int j = top; j < base; ++j){
			flag = flag || (s[j][upperBegin] != '.');
		}
		if(flag){ break; }
	}
	int lowerBegin = left;
	for(int i = 0; i < length; ++i, ++lowerBegin){
		bool flag = false;
		for(int j = base + 1; j < bottom; ++j){
			flag = flag || (s[j][lowerBegin] != '.');
		}
		if(flag){ break; }
	}
	Result upper = expr(s, upperBegin, top, right, base);
	Result lower = expr(s, lowerBegin, base + 1, right, bottom);
	Result r;
	r.v = mod(upper.v * INV[lower.v]);
	r.p = left + length;
	return r;
}
PARSER_DECL(digit){
	int base = calculateBase(s, left, top, right, bottom);
	Result r;
	r.v = s[base][left] - '0';
	r.p = left + 1;
	return r;
}

int main(){
	for(int y = 1; y < 2011; ++y){
		for(int z = 1; z < 2011; ++z){
			if(z * y % 2011 == 1){ INV[y] = z; }
		}
	}
	while(true){
		int n;
		cin >> n;
		if(n == 0){ break; }
		vector<string> s(n);
		for(int i = 0; i < n; ++i){ cin >> s[i]; }
		Result r = expr(s, 0, 0, s[0].size(), n);
		cout << r.v << endl;
	}
	return 0;
}

Measured memory_bytes: 0