Question #3 -- Renaming and permuting elements in a grid

A function on a finite set can be defined by using a two dimensional grid to specify the results.
For example, if the set were {0, 1} we could define a function f as:

     f | 0   1
    ---|------
      0| 0   1
       |      
      1| 1   1

Note that if we renamed '0' and '1' to 'a' and 'b', we'd essentially have the same set and the same function:

     f | a   b
    ---|------
      a| a   b
       |      
      b| b   b

Or if we changed the order in which we listed the elements, we'd still have the same system:

     f | b   a
    ---|------
      b| b   b
       |      
      a| b   a

Write a program which will read in two specifications for operations on a set of size N, and determine if they are two distinct operations, or simply a renaming and/or reordering of the specification.

Input will be in the form of two (N+1) by (N+1) grids of character strings, similar to the above without the dividing lines. An empty line will appear between the two grids.

Output should be a single line containing either the word 'same', if the grids represent the same function, or else the word 'different'.

For example:

   f          true     false
   true     false    true
   false    true     true

   f        1        0
   1        0        0
   0        0        1

This input would yield the output 'same', because relabeling 'true' as '0', 'false' as '1' and reordering the rows and columns yields the same function.