$inv = (0..2011).map {0}
(1..2010).map do |x|
    (1..2010).each do |y|
        if x * y % 2011 == 1
            $inv[x] = y
            break
        end
    end
end

def trim(a, i1, i2, j1, j2)
    i1 += 1 while i1 < i2 && (j1..j2).all? {|j| a[i1][j] == ?.}
    i2 -= 1 while i1 < i2 && (j1..j2).all? {|j| a[i2][j] == ?.}
    j1 += 1 while j1 < j2 && (i1..i2).all? {|i| a[i][j1] == ?.}
    j2 -= 1 while j1 < j2 && (i1..i2).all? {|i| a[i][j2] == ?.}
    return [a, i1, i2, j1, j2]
end

def index_single(a, i1, i2, j, c)
    x = nil
    (i1..i2).each do |i|
        next if a[i][j] == ?.
        return nil unless a[i][j] === c
        return nil if x
        x = i
    end

    return x
end

def exp(a, i1, i2, j1, j2)
    return nil if i1 > i2 || j1 > j2
    a, i1, i2, j1, j2 = trim(a, i1, i2, j1, j2)
    x = term(a, i1, i2, j1, j2)
    return x if x

    (j2-1).downto(j1+1).each do |j|
        op = (i1..i2).map {|i| a[i][j]} - [?.]
        next unless op.size == 1
        next unless op[0] == ?+ || op[0] == ?-
        next unless (i1..i2).all? {|i| a[i][j+1] == ?.}
        next unless (i1..i2).all? {|i| a[i][j-1] == ?.}
        x = exp(a, i1, i2, j1, j-2)
        next unless x
        y = term(a, i1, i2, j+2, j2)
        next unless y
        if op[0] == ?+
            return (x + y) % 2011
        else
            return (x - y) % 2011
        end
    end

    nil
end

def term(a, i1, i2, j1, j2)
    return nil if i1 > i2 || j1 > j2
    a, i1, i2, j1, j2 = trim(a, i1, i2, j1, j2)
    x = factor(a, i1, i2, j1, j2)
    return x if x

    (j1+1..j2-1).each do |j|
        next unless index_single(a, i1, i2, j, ?*)
        next unless (i1..i2).all? {|i| a[i][j+1] == ?.}
        next unless (i1..i2).all? {|i| a[i][j-1] == ?.}
        x = factor(a, i1, i2, j1, j-2)
        next unless x
        y = term(a, i1, i2, j+2, j2)
        next unless y
        return x * y % 2011
    end

    nil
end

def factor(a, i1, i2, j1, j2)
    return nil if i1 > i2 || j1 > j2
    a, i1, i2, j1, j2 = trim(a, i1, i2, j1, j2)
    x = powexpr(a, i1, i2, j1, j2)
    return x if x

    x = fraction(a, i1, i2, j1, j2)
    return x if x

    return nil unless index_single(a, i1, i2, j1, ?-)
    return nil unless (i1..i2).all? {|i| a[i][j1+1] == ?.}

    x = factor(a, i1, i2, j1+2, j2)
    return -x % 2011 if x

    nil
end

def powexpr(a, i1, i2, j1, j2)
    return nil if i1 > i2 || j1 > j2
    a, i1, i2, j1, j2 = trim(a, i1, i2, j1, j2)
    x = primary(a, i1, i2, j1, j2)
    return x if x

    return nil unless (i1..i2).select {|i| a[i][j2] != ?.}.size == 1
    i = (i1..i2).find {|i| a[i][j2] != ?.}
    return nil unless a[i][j2] =~ /\d/

    x = primary(a, i1, i2, j1, j2-1, i+1)
    return x ** a[i][j2].to_i % 2011 if x

    nil
end

def fraction(a, i1, i2, j1, j2)
    return nil if i1 > i2 || j1 > j2
    a, i1, i2, j1, j2 = trim(a, i1, i2, j1, j2)

    return nil unless (i1..i2).map {|i| a[i][j1]} - [?.] == [?-]
    return nil unless (i1..i2).map {|i| a[i][j2]} - [?.] == [?-]
    base_l = (i1..i2).find {|i| a[i][j1] == ?-}
    base_r = (i1..i2).find {|i| a[i][j2] == ?-}
    return nil unless base_l == base_r
    return nil unless (j1..j2).all? {|j| a[base_l][j] == ?-}

    x = exp(a, i1, base_l-1, j1+1, j2-1)
    return nil unless x
    y = exp(a, base_l+1, i2, j1+1, j2-1)
    return nil unless y
    return nil if y == 0

    x * $inv[y] % 2011
end

def primary(a, i1, i2, j1, j2, base = nil)
    return nil if i1 > i2 || j1 > j2
    a, i1, i2, j1, j2 = trim(a, i1, i2, j1, j2)

    if i1 == i2 && j1 == j2
        if a[i1][j1] =~ /\d/ && (base == nil || i1 == base)
            return a[i1][j1].to_i
        else
            return nil
        end
    end

    l = index_single(a, i1, i2, j1, ?()
    return nil unless l
    r = index_single(a, i1, i2, j2, ?))
    return nil unless r
    return nil unless l == r
    return nil unless base == nil || l == base

    return exp(a, i1, i2, j1+1, j2-1)
end

loop do
    n = gets.to_i
    break if n == 0
    a = (1..n).map { gets.chomp }

    p exp(a, 0, n-1, 0, a[0].size-1)
end

Measured memory_bytes: 8948