I have an array of arrays. Within each subarray, if two or more elements share a prefix whose length equals to or is greater than eight, then I want to replace those elements by their longest prefix. For this array:

```
m = [
  ["A", "97455589955", "97455589920", "97455589921"],
  ["B", "2348045101518", "2348090001559"]
]

```

I expect an output like this:

```
n = [
  ["A", "974555899"],
  ["B", "2348045101518", "2348090001559"]
]

```

For first subarray in `m`, the longest prefix is `"974555899"` of length nine.

```
974555899-55
974555899-20
974555899-21

```

For the second subarray, the longest prefix is `"23480"` of length five, and that is shorter than eight. In this case, the second subarray is left as is.

```
23480-45101518
23480-90001559

```

For this input:

```
m = [
  ["A", "2491250873330", "249111222333", "2491250872214", "2491250872213"],
  ["B", "221709900000"],
  ["C", "6590247968", "6590247969", "6598540040", "65985400217"]
]

```

The output should be like this:

```
[
  ["A", "2491250873330", "249111222333", "249125087221"],
  ["B", "221709900000"],
  ["C", "659024796", "65985400"]
]

```

For `array m[0]`, there is no prefix long enough between its four numbers, but there is a prefix `249125087221` of length twelve between `m[0][2]` and `m[0][3]`. For `array m[2]`, there is prefix `"659024796"` of length nine between `m[2][0]` and `m[2][1]`, and there is another prefix `"65985400"` of length eight between `m[2][2]` and `m[2][3]`.

I constructed the code below:

```
m.map{|x, *y|
  [x, y.map{|z| z[0..7]}.uniq].flatten
}

```

With my code with the first input, I get this output.

```
[
  ["A", "97455589"],
  ["B", "23480451", "23480900"]
]

```

I'm stuck on how to get dynamically the common prefix without setting a fixed length.