(repeating from comment)
If you look for cells in a matrix which are not there, you need a "numbers" table since SQL can't create something out of nothing.
create table numbers (nr int(11) not null primary key auto_increment);
insert into numbers values (1),(2),(3),(4),(5);
(and so on until you maximal ever possible or thinkable dimension).
Then you need the row number and column number of your data_on_matrix entry:
alter table data_on_matrix add column row_id int(11) not null, add column col_id int(11) not null;
If you want to read the cells of a matrix which are already there, you just read
select row_id, col_id, text_data from data_on_matrix where refid = {$refid};
If you look for free cells, just select
select r.nr as row_id, c.nr as col_id
from numbers r, numbers c
left join data_on_matrix d on d.col_id = c.id and d.row_id = r.id and d.refid = {$refId}
where d.ref_id is null and r.id <= {$rows} and c.id <= {$columns}
with refid, rows and columns coming out of table container
. It would also be easy to join container directly, but probably you already have a list of containers with their properties in php.